views:

106

answers:

4

I want to use type returned by PropertyType to create a typed function. I found this similiar http://stackoverflow.com/questions/914578/using-type-returned-by-type-gettype-in-c but this mentions how to create a list but does not mention how we can create a Func<>. Please help me out.

Pseudocode:

PropertyInfo inf = typeof(SomeClass).GetProperty("PropertyName");
Type T=inf.PropertyType;
Expression<Func<SomeClass,T>> le = GetPropertyOrFieldByName<SomeClass,T>("PropertyName");

static Expression<Func<TSource, TResult>> GetPropertyOrFieldByName<TSource,TResult>(string propertyOrFieldName)
{ 
ParameterExpression item = Expression.Parameter(typeof(TSource), "expr");MemberExpression prop = LambdaExpression.PropertyOrField(item, propertyOrFieldName);
var expr = Expression.Lambda<Func<TSource, TResult>>(prop, new ParameterExpression[] { item });
expr.Compile();
return expr;
}
+1  A: 

Simply use MakeGenericType(new Type[] { SomeClass, T }).

EDIT More detail:

Type T2 = typeof(Excpression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(new Type[] { SomeClass, T }));
... Activator.CreateInstance(T2);

,

Eric Mickelsen
+1  A: 

You can create the expression tree, but you can't declare its type in the code. Where you've got this:

Expression<Func<SomeClass,T>> le = ...

T has to be known at compile time (or be a type parameter). Instead, your T is a variable whose value is only known at execution time.

Now, the next question is whether you really need that anyway - or in what way you need it. What are you trying to do with the expression? Could you just use the nongeneric Expression class instead as the variable type?

If you genuinely want an expression tree, you don't need to worry about MakeGenericType etc - use the Expression.Property method.

Jon Skeet
+1  A: 
Type parameter = ...;
Type result = ...;
Type your_type = typeof(Func<,>).MakeGenericType(new Type[] {parameter, result});
mfeingold
+1  A: 

If you want to call the GetPropertyOrFieldByName with the PropertyType, this should work:

PropertyInfo inf = typeof(SomeClass).GetProperty("PropertyName");
Type T = inf.PropertyType;

object le =
    typeof([TypeThatDeclaresMethod]).GetMethod("GetPropertyOrFieldByName")
    .MakeGenericMethod(typeof(SomeClass), T)
    .Invoke(null, new object[] { "PropertyName" });

Assuming that the GetPropertyOrFieldByName method is a public static method.

Steven
Hi I tried to use the above method but get an null reference exception...I want to get a list from the method and TypeThatDeclaresMethod means the class that has the method or return type?.sorry I am new to reflection..so I have added the GetPropertyOrFieldByName to the code above in the question..please help me out
Misnomer
`[TypeThatDeclaresMethod]` is the name of the class that has the method. So if you have a class called `Program` that contains `GetPropertyOrFieldByName`, than your call would be: `typeof(Program).GetMethod("GetPropertyOrFieldByName")`.
Steven
Hi,Thanks..actually I didnt have the method public so it returned null now I have it an Object variable that has the exression value so I convert it tostring so how do I use this string variable as a Expression ?
Misnomer
Could you rephrase your question? I don't understand what you'd like to know.
Steven
Well I have a string now that has the expression value to be evaluated..like expr=>expr.FieldName so I want to use this string as Linq.Expression or any other way to query the DB.
Misnomer
Perhaps you could start a new thread here at SO. Please describe as clearly as possible what your intent is. There clearer the better.
Steven