A common protected generic method through .NET reflection in a derived class

I recently tried to do something similar in a WP7 application

I have a class

abstract class A {
//this method has an implementation
protected void DoSomething<T, TKey>(Func<T, TKey> func) { //impl here }
};

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);
// 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 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!

+3
source share
1 answer

From http://msdn.microsoft.com/en-us/library/system.methodaccessexception.aspx

This exception is thrown in situations for example:

  • , , , .

  • .

  • , , .

WP7 , , (NonPublic) - ​​ WP7 , , .

+4

All Articles