访问非公共成员-ReflectionAttribute [英] Access non-public members - ReflectionAttribute

查看:57
本文介绍了访问非公共成员-ReflectionAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从程序集A加载程序集B.我试图枚举位于程序集B中的私有成员.

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

如何使用ReflectionPermission完成此任务?我在MSDN上找不到任何有用的东西.

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

亲切的问候PK

推荐答案

除非您在部分信任环境中运行,否则不需要 ReflectionPermission .我怀疑您的问题是您没有指定静态/实例.试试这个:

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

这篇关于访问非公共成员-ReflectionAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆