J'ai eu une idée et je veux savoir si cela peut marcher. J'ai des classes simples avec des propriétés et je veux générer des accesseurs avec des expressions. Mais à la fin je dois obtenir un Func<Test, string>
mais je ne connais pas les types quand je les utilise. Un petit exempleCréer un arbre d'expressions de manière dynamique
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.TestString = "Blubb";
var actionStub = typeof(Helper).GetMethod("CreatePropertyGetter").MakeGenericMethod(new Type[] { test.GetType(), typeof(string)});
dynamic action = actionStub.Invoke(null, new object[] {test.GetType(), "TestString"});
var x = action(test);
}
}
public class Test
{
public string TestString { get; set; }
}
public static class Helper
{
public static Func<TType, TPropValueType> CreatePropertyGetter<TType, TPropValueType>(Type type,
string propertyName)
{
PropertyInfo fieldInfo = type.GetProperty(propertyName);
ParameterExpression targetExp = Expression.Parameter(typeof(TType), "target");
MemberExpression fieldExp = Expression.Property(targetExp, fieldInfo);
UnaryExpression assignExp = Expression.Convert(fieldExp, typeof(TPropValueType));
Func<TType, TPropValueType> getter =
Expression.Lambda<Func<TType, TPropValueType>>(assignExp, targetExp).Compile();
return getter;
}
}
Le problème est que je ne peux pas appeler l'expression sans dynamique, parce que je ne peux pas coulé simple, à Func<object, object>
Merci pour le pouvez-vous s'il vous plaît expliquer un peu plus? – Sebastian
Vous cherchez à créer un 'Func