类的受保护成员的反映

发布于 2024-12-19 17:31:29 字数 874 浏览 2 评论 0原文

using System;
using System.Reflection;

namespace Reflection

{
    class Test
    {
        protected void methodname()
        {
            Console.WriteLine(("in the world of the reflection"));
            Console.Read();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           // BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
            BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
            Test aTest = new Test();
            MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
            mInfoMethod.Invoke(aTest, null);

        }
    }
}

根据 msdn BindingFlags.Nonpublic 用于访问非私有成员。如果我仅使用此枚举,Get 方法将返回 null 值。但如果使用枚举 - 实例和非公共,则调用所需的方法。这两者有什么区别呢。当我必须使用实例和公共/非公共组合时。

using System;
using System.Reflection;

namespace Reflection

{
    class Test
    {
        protected void methodname()
        {
            Console.WriteLine(("in the world of the reflection"));
            Console.Read();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           // BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
            BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
            Test aTest = new Test();
            MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
            mInfoMethod.Invoke(aTest, null);

        }
    }
}

As per the msdn BindingFlags.Nonpublic is used to access the non private members. If I use only this enum the Getmethod returns null value. But if use the enum - Instance and nonpublic the required method is called. What is the difference between these two. When I have to use instance and public/nonpublic combination.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你对谁都笑 2024-12-26 17:31:29

根据 GetMethod() 的文档:

您必须指定 BindingFlags.InstanceBindingFlags.Static 才能获得返回。

Instance/StaticPublic/NonPublic 指定两个不同的内容,您必须指定两者才能获得结果。

Per the documentation of GetMethod():

You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

Instance/Static and Public/NonPublic specify two different things and you have to specify both in order to get the result.

摘星┃星的人 2024-12-26 17:31:29

如果不指定枚举,则使用默认值。如果这样做,则必须指定两者:

  • Public 或 NonPublic(或两者)、
  • Static 或 Instance(或两者)

(请参阅 http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx)

If you don't specify the enum, the default is used. If you do, you have to specify both:

  • Public or NonPublic (or both)
  • Static or Instance (or both)

(See the note in the remarks section on http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文