使用反射获取基类的字段信息 [英] Get field info of base class with reflection

查看:82
本文介绍了使用反射获取基类的字段信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的UI程序集中存在一个类,它继承自这样的引用程序集中的类




类EmployeesDC

{

私人字符串mstrEmployeeNumber;




}


类EmployeesBL:EmployeesDC

{

}


然后在UI组装


班员工:员工BL

{

}


有人可以给我代码,这样我就可以在EmployeesDC上使用反射获取私人领域的信息




我用google搜索但我还不清楚??


谢谢

rotsey

Hi,

I have a class that exist in my UI assembly that inherits from a class
in a referenced assembly like this.

class EmployeesDC
{
private string mstrEmployeeNumber;

etc
}

class EmployeesBL : EmployeesDC
{
}

Then in UI Assembly

class Employees : EmployeesBL
{
}

Can someone give me the code so I can get info on the private fields
in EmployeesDC using reflection.

I have googled but I am still not clear how??

thanks
rotsey


推荐答案

Rotsey写道:
Rotsey wrote:




我的UI程序集中存在一个继承自a在这样的引用程序集中,类是




class Emplo yeesDC

{

私人字符串mstrEmployeeNumber;




}


类EmployeesBL:EmployeesDC

{

}


然后在UI程序集中


类员工:员工BL

{

}


有人可以给我代码,这样我就能得到关于私人领域的信息
雇员DC中的
使用反射。


我用谷歌搜索但我还不清楚如何?


谢谢

rotsey
Hi,

I have a class that exist in my UI assembly that inherits from a class
in a referenced assembly like this.

class EmployeesDC
{
private string mstrEmployeeNumber;

etc
}

class EmployeesBL : EmployeesDC
{
}

Then in UI Assembly

class Employees : EmployeesBL
{
}

Can someone give me the code so I can get info on the private fields
in EmployeesDC using reflection.

I have googled but I am still not clear how??

thanks
rotsey



你好rotsey,


它不是太复杂了,但我可以问你之后是什么类型的信息?b
如果你是在该领域的内容之后,那么有没有理由

为什么你不能宣称它是受保护的?


如果你在其他事情之后,让我们知道,我们会指出你正确的方向

方向。


-

Tom Spink

爱丁堡大学

Hi rotsey,

It''s not too complicated, but may I ask what type of information you''re
after? If you''re after the contents of the field, then is there a reason
why you can''t declare it as protected?

If you''re after something else, let us know and we''ll point you in the right
direction.

--
Tom Spink
University of Edinburgh


谢谢汤姆


我想要的只是获得所有私人领域的名称。


我将从前缀知道做其他事情


rotsey


" Tom Spink" < ts **** @ gmail.com写信息

新闻:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
thanks Tom

All I want is to get the name of all the private fields.

I will then know from the prefix to do something else

rotsey

"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

Rotsey写道:
Rotsey wrote:

>

我有一个存在于我的UI程序集中的类,它继承自类似引用程序集中的类


类EmployeesDC
{
私有字符串mstrEmployeeNumber;




类EmployeesBL:EmployeesDC
{
}
然后在UI组装中

class Employees:EmployeesBL


有人可以给我代码,以便我可以使用反射在EmployeesDC中获取私人领域的信息。 />
我用谷歌搜索但我还不清楚怎么样?
谢谢
rotsey
>Hi,

I have a class that exist in my UI assembly that inherits from a class
in a referenced assembly like this.

class EmployeesDC
{
private string mstrEmployeeNumber;

etc
}

class EmployeesBL : EmployeesDC
{
}

Then in UI Assembly

class Employees : EmployeesBL
{
}

Can someone give me the code so I can get info on the private fields
in EmployeesDC using reflection.

I have googled but I am still not clear how??

thanks
rotsey



你好rotsey ,


这不是太复杂,但我可以问你之后是什么类型的信息?b
$ b如果你是在该领域的内容之后,那么有没有理由

为什么你不能宣称它是受保护的?


如果你在其他事情之后,请告诉我们,我们会在

右边

方向指出你。


-

Tom Spink

爱丁堡大学


Hi rotsey,

It''s not too complicated, but may I ask what type of information you''re
after? If you''re after the contents of the field, then is there a reason
why you can''t declare it as protected?

If you''re after something else, let us know and we''ll point you in the
right
direction.

--
Tom Spink
University of Edinburgh



Rotsey写道:
Rotsey wrote:

感谢Tom


我想要的只是获取所有私人字段的名称。

我会从前缀知道做其他事情


rotsey


汤姆斯宾克 < ts **** @ gmail.com写信息

新闻:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
thanks Tom

All I want is to get the name of all the private fields.

I will then know from the prefix to do something else

rotsey

"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

> Rotsey写道:
>Rotsey wrote:

>>

我的UI程序集中存在一个类,它继承自这样的引用程序集中的类


类EmployeesDC
{private string mstrEmployeeNumber;




类EmployeesBL:EmployeesDC
{
}
然后在UI汇编中/>
班级员工:员工BL
{
}

有人可以给我代码,以便我可以在EmployeesDC中使用私人领域获取信息
反思。

我用谷歌搜索但我还不清楚怎么样?
感谢
rotsey
>>Hi,

I have a class that exist in my UI assembly that inherits from a class
in a referenced assembly like this.

class EmployeesDC
{
private string mstrEmployeeNumber;

etc
}

class EmployeesBL : EmployeesDC
{
}

Then in UI Assembly

class Employees : EmployeesBL
{
}

Can someone give me the code so I can get info on the private fields
in EmployeesDC using reflection.

I have googled but I am still not clear how??

thanks
rotsey


你好rotsey ,

这不是太复杂,但我可以问你之后是什么类型的信息?如果你是在该领域的内容之后,那么有一个理由
为什么你不能宣称它是受保护的?

如果你在追求别的东西,让我们我们知道,我们会指出你的

方向。

-
Tom Spink
爱丁堡大学


Hi rotsey,

It''s not too complicated, but may I ask what type of information you''re
after? If you''re after the contents of the field, then is there a reason
why you can''t declare it as protected?

If you''re after something else, let us know and we''ll point you in the
right
direction.

--
Tom Spink
University of Edinburgh



嗨Rotsey,



///

使用System;

使用System.Reflection;


公共舱A

{

私人字符串pfa_A;

私人字符串pfb_A;

私人字符串pfc_A;

}


公共舱B:A

{

private string pfa_B;

private string pfb_B;

private string pfc_B;

}


public C级:B

{

public void Foo()

{

类型t = this.GetType( );

BindingFlags bf = BindingFlags.Default;


bf | = BindingFlags.NonPublic;

bf | = BindingFlags.FlattenHierarchy ;

bf | = BindingFlags.Instance;


foreach(t.GetFields(bf)中的FieldInfo字段)

{

Console.WriteLine(字段);

}

}

}


公共课E

{

public static void Main()

{

C c = new C();

c.Foo();

}

}

///


这里发生的事情最有趣的是C的Foo()方法。

继承层次结构是C -B -A,当你在C中调用Foo()时,它将打印出它找到的字段列表。


修改bf(BindingFlags参数)将有助于优化/扩展

搜索。


字段变量(在foreach语句的每次迭代中) )(最可能是
)包含你需要知道的关于这个领域的一切。


-

Tom Spink

爱丁堡大学

Hi Rotsey,

Cool. Take a look at the following example:

///
using System;
using System.Reflection;

public class A
{
private string pfa_A;
private string pfb_A;
private string pfc_A;
}

public class B : A
{
private string pfa_B;
private string pfb_B;
private string pfc_B;
}

public class C : B
{
public void Foo ()
{
Type t = this.GetType();
BindingFlags bf = BindingFlags.Default;

bf |= BindingFlags.NonPublic;
bf |= BindingFlags.FlattenHierarchy;
bf |= BindingFlags.Instance;

foreach (FieldInfo field in t.GetFields(bf))
{
Console.WriteLine(field);
}
}
}

public class E
{
public static void Main ()
{
C c = new C();
c.Foo();
}
}
///

What''s going on here is most interesting in the Foo() method of C. The
inheritance hierarchy is C -B -A, and when you call Foo() in C, it will
print out a list of fields it finds.

Modifying bf (the BindingFlags parameter) will help to refine/expand the
search.

The field variable (on each iteration of the foreach statement) will (most
likely) contain everything you need to know about the field.

--
Tom Spink
University of Edinburgh


这篇关于使用反射获取基类的字段信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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