I recently tried to do something similar in a WP7 application
I have a class
abstract class A {
protected void DoSomething<T, TKey>(Func<T, TKey> func) {
};
and I want to call this protected method through reflection in a derived class:
public class B : A {
void SomeMethod(Type tableType, PropertyInfo keyProperty){
MethodInfo mi = this.GetType()
.GetMethod("DoSomething", BindingFlags.Instance | BindingFlags.NonPublic)
.MakeGenericMethod(new Type[] { tableType, keyProperty.GetType() });
LambdaExpression lambda = BuildFuncExpression(tableType, keyProperty);
mi.Invoke(this, new object[] { lambda });
}
private System.Linq.Expressions.LambdaExpression BuildFuncExpression(Type paramType, PropertyInfo keyProperty)
{
ParameterExpression parameter = System.Linq.Expressions.Expression.Parameter(paramType, "x");
MemberExpression member = System.Linq.Expressions.Expression.Property(parameter, keyProperty);
return System.Linq.Expressions.Expression.Lambda(member, parameter);
}
}
};
and I get a MethodAccessException. I understand that this is a security exception, but I can normally call this method from this place, so I would have to call it with reflection too.
What could be wrong? Thank!
source
share