Compile time type check in C#

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?
 
F

Flinky Wisty Pomm

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)
{ }
 
N

n.net

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top