pkolodziej
pkolodziej

Reputation: 1367

Access non-public members - ReflectionAttribute

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

Upvotes: 0

Views: 3902

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503469

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

Upvotes: 3

Related Questions