如何访问基类的私有成员 [英] How to access private members of a base class

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

问题描述



标题总结了这个问题。

我想访问一个类的所有私人成员,包括私人

基类的成员。(我已经有了ReflectionPermission)


有没有办法获取这些信息?


Thnaks in提前

Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance

推荐答案

Abelardo Vacca写道:
Abelardo Vacca wrote:

标题总结了这个问题。
我想访问一个类的所有私有成员,包括其基类的私有
成员。(我已经有了ReflectionPermission)

有没有办法获取这些信息?

预先提醒
Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance




从理论上说,不应该。这就是为什么他们是私人的。但是,如果.NET提供了一些方法,那么
不会感到惊讶。我有兴趣找到它。



Theoretically, there shouldn''t be. That''s why they''re private. But I
wouldn''t be surprised if .NET provided some way to do it. I''d be
interested in finding out.


Abelardo,


您将只能通过反射访问此信息,

获取表示私有方法的MethodInfo实例,然后

调用它们上的Invoke(使用对象)。


如果您可以访问该类的代码,您可能需要考虑将方法公开为public / internal / protected(取决于在

方法调用的地方)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Abelardo Vacca" < AB- *********** @ discussions.microsoft.com>在消息中写道

新闻:D4 ********************************** @ microsof t.com ...
Abelardo,

You will only be able to access this information through reflection,
getting the MethodInfo instances representing the private methods, and then
calling Invoke on them (using the instance of the object).

If you have access to the code of the class, you might want to consider
exposing the methods as public/internal/protected (depending on where the
method calls are taking place).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Abelardo Vacca" <Ab***********@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...

标题总结了这个问题。
我想访问一个班级的所有私人成员,包括
私人
其基类成员。(我已经有了ReflectionPermission)

有没有办法获取这些信息?

提前提醒
Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the
private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance



谢谢,

也许我应该在我的问题中更加明确。


我已经使用了反射,并且它在中途工作:没有

ReflectionPermission你无法访问一个类的非公开成员(

,这就是为什么我写的我有那个许可不在了)

现在,有了ReflectionPermission,我可以访问所有成员,但只能在

当前班级。但我无法获得基地的私人会员

班级。 (这是正常的,因为基类的私有成员不是派生类的
成员) - 保护成员可用,即使他们在基类上声明了

-


我想知道是否有办法解决这个问题。


谢谢


Nicholas Paldino [.NET / C#MVP]"写道:
Thank you,
Perhaps i should have been more explicit in my question.

I already use reflection, and it works half-way: Without
ReflectionPermission you cannot access the non-public members of a class (
that''s why I wrote I had that permission out of the way)
Now, WITH ReflectionPermission, I can access ALL members, but ONLY on the
current class level. But I CANNOT obtain the PRIVATE members of the base
classes. (That is normal, because private members of a base class are NOT
members of the derived classes) - PROTECTED members are available, even if
they are declared on base classes -

I would like to know if there is a way around this.

Thank you

"Nicholas Paldino [.NET/C# MVP]" wrote:
Abelardo,

您将只能通过反射访问此信息,获取表示私有方法的MethodInfo实例,以及然后在它们上面调用Invoke(使用对象的实例)。

如果你有权访问类的代码,你可能要考虑将方法暴露为public / internal / protected(取决于
方法调用的位置)。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
< br> Abelardo Vacca" < AB- *********** @ discussions.microsoft.com>在消息中写道
新闻:D4 ********************************** @ microsof t.com。 ..
Abelardo,

You will only be able to access this information through reflection,
getting the MethodInfo instances representing the private methods, and then
calling Invoke on them (using the instance of the object).

If you have access to the code of the class, you might want to consider
exposing the methods as public/internal/protected (depending on where the
method calls are taking place).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Abelardo Vacca" <Ab***********@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...

标题总结了这个问题。
我想访问一个班级的所有私人成员,包括
私人其基类的成员。(我已经有了ReflectionPermission)

有没有办法获取这些信息?

提前提醒
Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the
private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance




这篇关于如何访问基类的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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