ConstantExpression Method C# with Example



ConstantExpression Method C# with Example

ConstantExpression must be the same type of the MemberExpression. The value in this example is a string, which is 
converted before creating the ConstantExpression instance. 
private static ConstantExpression GetConstant(Type type, string value) 
{ 
// Discover the type, convert it, and create ConstantExpression 
ConstantExpression constant = null; 
if (type == typeof(int)) 
{ 
int num; 
int.TryParse(value, out num); 
constant = Expression.Constant(num); 
} 
else if(type == typeof(string)) 
{ 
 

constant = Expression.Constant(value); 
} 
else if (type == typeof(DateTime)) 
{ 
DateTime date; 
DateTime.TryParse(value, out date); 
constant = Expression.Constant(date); 
} 
else if (type == typeof(bool)) 
{ 
bool flag; 
if (bool.TryParse(value, out flag)) 
{ 
flag = true; 
} 
constant = Expression.Constant(flag); 
} 
else if (type == typeof(decimal)) 
{ 
decimal number; 
decimal.TryParse(value, out number); 
constant = Expression.Constant(number); 
} 
return constant; 
} 

0 Comment's

Comment Form

Submit Comment