I am generating compiled getter methods at runtime for a given member. Right now, my code just assumes the result of the getter method is a string (great for testing). However, I would like to make this work with the custom converter class that I wrote, see the “ConverterBase” link that I added below.
I cannot figure out how to add a call to the converter class to my expression tree.
public Func<U, string> GetGetter<U>(MemberInfo info) { Type t = null; if (info is PropertyInfo) { t = ((PropertyInfo)info).PropertyType; } else if (info is FieldInfo) { t = ((FieldInfo)info).FieldType; } else { throw new Exception("Unknown member type"); } //TODO, replace with ability to specify in custom attribute ConverterBase typeConverter = new ConverterBase(); ParameterExpression target = Expression.Parameter(typeof(U), "target"); MemberExpression memberAccess = Expression.MakeMemberAccess(target, info); //TODO here, make the expression call "typeConverter.FieldToString(fieldValue)" LambdaExpression getter = Expression.Lambda(memberAccess, target); return (Func<U, string>)getter.Compile(); }
I am looking for what to put in the second area of TODO (I can handle the first :)).
The resulting compiled lambda should take an instance of type U as a parameter, call the specified member access function, then call the "FieldToString" converter method with the result and return the resulting string.
c # lambda expression-trees
TheSoftwareJedi
source share