调用通过反射私人/受保护的方法从同一个对象实例(或底部) [英] Invoking Private / Protected Methods Via Reflection From The Same Object Instance (or Base)

查看:135
本文介绍了调用通过反射私人/受保护的方法从同一个对象实例(或底部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过反射调用protected方法。我使用这样的:

Is it possible to call a protected method via reflection. I am using this:

 Me.GetType.InvokeMember(Stages(CurrentStage), 
                         Reflection.BindingFlags.InvokeMethod, 
                         Nothing, 
                         Me, 
                         Nothing)

它总是调用没有PARAMS或回报的方法。这个想法是,我的对象(业务逻辑事务处理)的用户将继承的基类中的私有方法的负载火补充道。然后,他们这些方法名称添加到顺序列表,他们希望他们开枪,并上面的代码将采取开除他们的照顾。

Which always calls a method with no params or return. The idea is that the user of my object (transactional processing of business logic) inherits the base class adds in a load of private methods to fire. They then add these method names to a list in the order they would like them fired and the code above will take care of firing them.

它正常工作与市民的方法,但与同班私人或受保护的方法(保护,因为我有一些标准预建的方法来添加到基类)。现实我可以让这些方法公众和用它做,但我的内心书呆子不会允许我这样做...

It works fine with public methods but not with private or protected methods in the same class (Protected because I have some 'standard' pre built methods to add to the base class). Realistically I could make the methods public and be done with it but my inner nerd wont allow me to do so...

我假定这是一个安全功能。 ?有没有办法来解决这个问题或有没有人对如何继续,但让我的好吃,好吃的可视性修饰符机智任何建议。

I am assuming this is a security feature. Is there a way to get around this or does anyone have any suggestions on how to proceed but keep my tasty, tasty visibility modifiers in tact?

(注:ITS在VB .NET但C#答案是好的,如果这就是你有舒适)。

(NOTE: ITS IN VB.NET but a C# Answer is fine if that is what you are comfortable with).

推荐答案

如果要调用的方法一个实例化的对象,你也需要BindingFlags.Instance。因此,代码会看起来像(注:我还添加了的BindingFlags选项,在搜索中包含公共成员):

If you are invoking the methods on an instantiated object, you will also need BindingFlags.Instance. So the code would look like (Note: I've also added the BindingFlags option to include public members in the search):

Me.GetType.InvokeMember(Stages(CurrentStage), 
                        BindingFlags.InvokeMethod Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance, 
                        Nothing, 
                        Me, 
                        Nothing)

有关如何使用它的更多信息,请参阅 MSDN文档

For more information on how to use this, refer to the MSDN Documentation.

修改

要支持这个答案,我写了演示公共受保护的调用一个测试程序私人类方法。我已经上传所有的类引擎收录。有三类构成了一个C#控制台应用程序,所以可以被添加到Visual Studio解决方案,并运行测试。

To support this answer, I have written a test program that demonstrates the invocation of public, protected and private class methods. I have uploaded all classes to PasteBin. There are three classes that make up a C# Console Application, so can be added to a Visual Studio solution and run to test.


  • 的Program.cs - 设置要调用的方法(包含于 SomeClass的类)。执行调用并输出结果

  • SomeClass.cs - 包含每一个可见性修饰符的三种方法:公共保护私人。同时将自身注册和三载有调用由祈求类方法

  • Invoker.cs - 接受的是要调用类型和方法注册。然后列举了注册的类型,并调用方法,构建结果的枚举集合。

  • Program.cs - Sets up the methods to be invoked (contained within the SomeClass class). Executes the invocation and prints the results.
  • SomeClass.cs - Contains three methods of each of the visibility modifiers: public, protected and private. Also registers itself and the three contained methods for invocation by the Invoker class.
  • Invoker.cs - Accepts registration of types and methods that are to be invoked. Then enumerates the registered types and invokes the methods, building an enumerable collection of results.

随意查看代码,看看是否你可以找到你是如何接近它的任何差异。我很欣赏它可能不是最简单的办法,虽然。让我知道如果你有任何进一步的查询。

Feel free to look over that code and see if you can find any differences in how you are approaching it. I appreciate it may not be the simplest of approaches, though. Let me know if you have any further queries on it.

这篇关于调用通过反射私人/受保护的方法从同一个对象实例(或底部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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