B
bob_jenkins
In C# 2.0, given an arbitrary expression and the types of the
variables referenced in it, how do I find the expression type? For
example,
int x;
float y;
what is the type of x+y?
If I had values for x and y, I could do (x+y).GetType(). But I don't,
I just have their types. Default values won't work because values
that don't cause the expression to raise an error might be very hard
to come by.
I can dynamically generate
public static object f(int x, float y)
{
object o = x+y;
return o;
}
and use reflection (MethodInfo->MethodBody->LocalVariables) to find
that o is of type System.Object, and I'll get a typecheck error if x+y
isn't allowed, but I don't see how to get the type of x+y given that
it is allowed. If I had C# 3.0, I think I could do "var o = x+y;"
then use reflection to find the type of o, but I've only got C# 2.0.
It's conceivable that x+y returns different types depending on the
values of x and y, so x+y truly has no type.
variables referenced in it, how do I find the expression type? For
example,
int x;
float y;
what is the type of x+y?
If I had values for x and y, I could do (x+y).GetType(). But I don't,
I just have their types. Default values won't work because values
that don't cause the expression to raise an error might be very hard
to come by.
I can dynamically generate
public static object f(int x, float y)
{
object o = x+y;
return o;
}
and use reflection (MethodInfo->MethodBody->LocalVariables) to find
that o is of type System.Object, and I'll get a typecheck error if x+y
isn't allowed, but I don't see how to get the type of x+y given that
it is allowed. If I had C# 3.0, I think I could do "var o = x+y;"
then use reflection to find the type of o, but I've only got C# 2.0.
It's conceivable that x+y returns different types depending on the
values of x and y, so x+y truly has no type.