我想使用PropertyType返回的类型来创建类型化的函数。我发现这个类似using type returned by Type.GetType() in c#但这提到了如何创建列表,但没有提到如何创建Func 。请帮帮我。伪代码: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;} 最佳答案 如果要用GetPropertyOrFieldByName调用PropertyType,这应该可以: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" });假定GetPropertyOrFieldByName方法是公共静态方法。关于c# - 如何动态地使用PropertyType反射属性来创建各自的类型化Function <>?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2775812/
10-10 20:20