Compile time type check in C#

  • Thread starter Thread starter n.net
  • Start date Start date
N

n.net

I am working with .NET 1.1 datatypes in my project in C#.

I ended up writing code a typical waterfall if..else modal.

if ( xType == "System.String")
do this

if ( xType == "System.Integer")
do this

if ( xType == "System.Double")
do that.

If it had been in C++, I would have very well used compile time type
checking. But i don't know how easy to achieve in C# with .NET 1.1
Platform.

Is C# generics is the only solution?
 
Polymorphism is your friend. If you're switching on type (or even an
enum) chances are you're not making the best use of OO.

private void DoThis(string x)
{}

private void DoThis(int x)
{}

private void DoThat(double x)
{}


or, as you point out,

private void DoThat(double x)
{ }

private void DoThis<T>(T x)
{ }
 
Let me explain little more clearly, I am using a debugger object

DType = debugger.GetExpression(DataTypeExpression,true,3000).Value;

The DType is nothing but a string containing the data type. How do i
instantiate the function corresponding to the data type contained in
DType String.
 
Back
Top