Tuesday, March 15, 2011

Access non-public members - ReflectionAttribute

Hi

I am loading assembly B from assembly A. I am trying to enumerate private members of the type located in assembly B.

How do I use ReflectionPermission to accomplish this task? I couldn't find anything useful on the MSDN.

Assembly asm = Assembly.LoadFrom("Chapter13.exe", AppDomain.CurrentDomain.Evidence);
//AppDomain.CurrentDomain.Load("Chapter13");

Type t = asm.GetType("Chapter13.ProtectedBuffer");

MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic);

foreach (MemberInfo m in members)
{
    Console.WriteLine(m.Name);
}

Kind regards PK

From stackoverflow
  • Unless you're running in a partial-trust environment, you don't need ReflectionPermission. I suspect your problem is that you're not specifying static/instance. Try this:

    MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic | 
                                        BindingFlags.Static |
                                        BindingFlags.Instance);
    

0 comments:

Post a Comment