recently I was trying to do something like this in WP7 app
I have class
abstract class A {
//this method has an implementation
protected void DoSomething<T, TKey>(Func<T, TKey> func) { //impl here }
};
and I want to invoke that protected method via reflection in 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);
// MethodAccessException
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'm getting MethodAccessException. I understand this is a security exception but I'm able to call the method normally from that place, so I should be able to call it via reflection as well.
What might be wrong? Thanks!