反思问题 [英] reflection problem

查看:48
本文介绍了反思问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印一个类的公共属性。

我必须使用GetSubObject函数访问Properties。

请帮助我。

FieldInfo [] fields = typeof(Boolean).GetFields();


ArrayList param = new ArrayList();


param.Add( 姓名;


这不行!


foreach(在域中对象)

{

Object obj = Util.GetSubObject(param,iter);

if(obj!= null)Console.WriteLine((String)obj);

}

Console.ReadLine();


public static Object GetSubObject(ArrayList paramArray,Object

currentObj)

{

Object obj = null;

foreach(paramArray中的Object param)

{

// scorro i parametri dell''oggetto

foreach(currentOfj.GetType()中的FieldInfo字段.getFields())

{

if(field.Name.Equals(param))

{

obj = field.GetValue(currentObj);

if(obj!= null)currentObj = obj;

else返回null;

}

//否则返回假;

}

}

返回obj;

}

解决方案

Locia< ro ******** @ yahoo.it>写道:

我需要打印一个类的公共属性。
我必须使用GetSubObject函数访问Properties。
请帮帮我。

FieldInfo [] fields = typeof(布尔).GetFields();




您是否正在尝试访问属性或字段?您的问题

询问属性,但您的代码仅使用字段。


-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ ~Sibet 博客: http://www.msmvps.com/jon。双向飞碟

如果回复小组,请不要给我发邮件


我想这样做

for(int i = 0; i< fields.Length; i ++)

Console.WriteLine(fields [i] .Name);


而是使用fields [i] .Name我必须访问字段[i] .Name并使用

GetSubObject(" Name",fields [i])函数并使用反射?


对不起这是一个断言不是问题。我写了一个问号,但它是一个

错误。


I need to print Public Properties of a class.
I must access to Properties with GetSubObject function.
Please help me.
FieldInfo[] fields=typeof(Boolean).GetFields();

ArrayList param=new ArrayList();

param.Add("Name");

This don''t work!

foreach (Object iter in fields)
{
Object obj=Util.GetSubObject(param,iter);
if (obj!=null) Console.WriteLine((String) obj);
}
Console.ReadLine();

public static Object GetSubObject(ArrayList paramArray,Object
currentObj)
{
Object obj=null;
foreach (Object param in paramArray)
{
// scorro i parametri dell''oggetto
foreach( FieldInfo field in currentObj.GetType().GetFields())
{
if (field.Name.Equals(param))
{
obj=field.GetValue(currentObj);
if (obj!=null) currentObj=obj;
else return null;
}
//else return false;
}
}
return obj;
}

解决方案

Locia <ro********@yahoo.it> wrote:

I need to print Public Properties of a class.
I must access to Properties with GetSubObject function.
Please help me.

FieldInfo[] fields=typeof(Boolean).GetFields();



Are you actualy trying to access properties or fields? Your question
asks about properties, but your code only uses fields.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


I would like do it
for (int i=0;i<fields.Length;i++)
Console.WriteLine(fields[i].Name);

but rather use fields[i].Name I must access to fields[i].Name with
GetSubObject("Name",fields[i]) function and using reflection?


Sorry It''s a assertion not a question.I put a question mark but it''s a
error.


这篇关于反思问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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