Access non-public members - ReflectionAttribute

橙三吉。 提交于 2019-12-23 06:06:04

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/769634/access-non-public-members-reflectionattribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!