反思GetProperty问题 [英] Reflection GetProperty Problem

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

问题描述




我要用一些代码开始这个,因为它会更容易

后来解释.. 。


使用系统;


命名空间ConsoleApplication1

{

/// < summary>

/// Class1的摘要说明。

///< / summary>

class Class1

{

[STAThread]

static void Main(string [] args)

{

//抛出一个AmbiguousMatchException

System.Reflection.PropertyInfo oPi =

typeof(ChildClass).GetProperty(" Target");

}

}


//父类

class ParentClass

{

公共虚拟对象目标

{

get {return new object();}

}

}


//继承自Parent类并隐藏继承成员的子类

使用new关键字定位,并更改返回类型

class ChildClass:ParentClass

{

public new string String

{

get {返回"";}

}

}

}


我在尝试什么要做的是从ChildClass获取属性Target。当我执行GetProperty时,我返回两个与Target匹配的PropertyInfo对象 -

来自ParentClass,一个来自ChildClass。发生这种情况是因为我将
在TargetClass中将Target的返回类型从对象更改为字符串。

如果返回类型保持不变,则只返回一个PropertyInfo。 br />

不幸的是,我不能选择使返回类型相同。

它是由其他人在公平中编写的代码大系统,很可能会导致各种各样的问题。有没有人有任何建议我怎么可以从$ Child获取目标PropertyInfo?


使用声明类型属性是不可行的属性信息为

我的真实世界代码中有很多其他类在ChildClass和

ParentClass之间,我还没有添加到上面的例子中。


我还看了一下BindingFlags属性参数,但是有没有合适的BindingFlag会给我返回最高级别的Target对象

- 最接近的是BindingFlags.DeclaredOnly,但如果我继承ChildClass但是不要覆盖Target
$ b,那么这不会让我返回

目标$ b对象。


我希望我已经很好地解释了这一点,有点难以得到你的回报

回合。


我们非常感谢任何帮助。


Stu

Hi,

I''m going to start this off with some code as it''ll make it easier to
explain afterwards...

using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
System.Reflection.PropertyInfo oPi =
typeof(ChildClass).GetProperty("Target");
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target with the new keyword, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}
}

What I am trying to do is get the property Target from the ChildClass. When
I do the GetProperty I am returned two PropertyInfo objects matching Target -
one from ParentClass and one from ChildClass. This happens because I am
changing the return type of Target from object to string in the ChildClass.
If the return type stays the same then only one PropertyInfo is returned.

Unfortunately, it''s not an option for me to make the return types the same.
It''s code written by someone else in a fairly big system and would likely
cause all sorts of problems. Has anyone got any suggestions how I can just
get the Target PropertyInfo from the ChildClass?

It''s not feasible to use the declaring type attribute of property info as
there are many other classes in my real world code between ChildClass and
ParentClass that I haven''t added to the example above.

I''ve also taken a look at the BindingFlags attribute parameter, but there is
no suitable BindingFlag that will return me the very top level Target object
- the closest is BindingFlags.DeclaredOnly, but this won''t return me the
Target if I then inherit from ChildClass but then don''t override the Target
object.

I hope I have explained this well, it''s kinda difficult to get your head
round.

Any help would be much appreciated.

Stu

推荐答案

试试ca lling GetProperties而不是GetProperty。然后循环返回

返回的数组,找到你想要的属性。


Thi

Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi


感谢您的快速回复,


我想过使用它,但有几个问题。显然他们

都有相同的名字,所以我必须查看

PropertyType属性上的其他属性来获得正确的属性,但是,这个的候选者呢? />
将是...


1)物业类型属性 - 我不知道

物业的返回类型是什么将会如此无法检查,有很多

类实现''父类'',所有类型都有不同的返回类型。


2)声明Type属性 - 这是属性声明的类型

in,所以如果我从ChildClass继承并且不重新定义Target属性,

声明类型将是ChildClass而不是对象的类型我反映的是



我需要做的是,以某种方式循环类层次结构并找到

类最高(或最低取决于你如何看待它)级别

声明Target属性,但我不知道怎么没有它凌乱......


Truong Hong Thi写道:
Thanks for the quick reply,

I thought about using that but there are several problems. Obviously they
both have the same name, so I have to look at other properties on the
PropertyType attribute to get the right one, however, the candidates for this
would be...

1) Property Type property - I don''t know what the return type of the
property is going to be so can''t check against that, there are loads of
classes that implement the ''Parent Class'', all with different return types.

2) Declaring Type Property - This is the type that the property is declared
in, so if I inherit from ChildClass and don''t redefine the Target property,
the declaring type will be ChildClass and not the Type of the object I am
reflecting.

What I need to do, is somehow loop around the class hierarchy and find the
class at the highest (or lowest depending on how you look at it) level that
declares the Target property, but I don''t know how without it being messy...

"Truong Hong Thi" wrote:
尝试调用GetProperties而不是GetProperty。然后循环返回
返回的数组,找到你想要的属性。

Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi



I我最终做到了这一点......我觉得它运作正常,看起来很乱......


使用System;

使用System.Reflection ;


命名空间ConsoleApplication1

{

///< summary>

/// Class1的摘要描述。

///< / summary>

class Class1

{

[STAThread ]

static void Main(string [] args)

{

//抛出一个AmbiguousMatchException

类型oType = typeof(SubChildClass);

System.Reflection.PropertyInfo [] oPis = oType.GetProperties();


System.Reflection.PropertyInfo oRequiredPi = null;


int nHierarchyLevel = -1;


foreach(oPis中的PropertyInfo oPi)

{

if(oPi.Name ==" Target")

{

//考试基本类型,计算我们上层次的次数

int nTempHierarchyLevel = GetHierarchyLevel(oPi.DeclaringType,oType,0);

if((nTempHierarchyLevel< = nHierarchyLevel )||(nHiera rchyLevel == - 1))

{

nHierarchyLevel = nTempHierarchyLevel;

oRequiredPi = oPi;

}

}

}

}


private static int GetHierarchyLevel(类型tDeclaringType ,类型

tTypeToMatch,int nHierarchyCount)

{

if(tDeclaringType == tTypeToMatch)

{

返回nHierarchyCount;

}

else

{

nHierarchyCount ++;

返回GetHierarchyLevel(tDeclaringType,tTypeToMatch.BaseType,

nHierarchyCount);

}

}

}


//父类

class ParentClass

{

公共虚拟对象Target

{

get {return new object();}

} $ / $
}


//继承自Parent类并隐藏继承成员的子类

目标,并且还更改返回类型

class ChildClass:ParentClass

{

公共新字符串目标

{

get {return"";}

}

}


//继承自Parent类的子类隐藏继承成员

目标,并且还更改返回类型

class SubChildClass:ChildClass

{


}

}

" satankidneypie"写道:
I''ve ended up doing this... I think it works right, it just looks messy...

using System;
using System.Reflection;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
//throws an AmbiguousMatchException
Type oType = typeof(SubChildClass);
System.Reflection.PropertyInfo[] oPis = oType.GetProperties();

System.Reflection.PropertyInfo oRequiredPi = null;

int nHierarchyLevel = -1;

foreach(PropertyInfo oPi in oPis)
{
if(oPi.Name=="Target")
{
//examine the basetypes, counting how many times we go up the hierarchy
int nTempHierarchyLevel = GetHierarchyLevel(oPi.DeclaringType, oType, 0);
if((nTempHierarchyLevel<=nHierarchyLevel)||(nHiera rchyLevel==-1))
{
nHierarchyLevel = nTempHierarchyLevel;
oRequiredPi = oPi;
}
}
}
}

private static int GetHierarchyLevel(Type tDeclaringType, Type
tTypeToMatch, int nHierarchyCount)
{
if(tDeclaringType==tTypeToMatch)
{
return nHierarchyCount;
}
else
{
nHierarchyCount++;
return GetHierarchyLevel(tDeclaringType, tTypeToMatch.BaseType,
nHierarchyCount);
}
}
}

//Parent Class
class ParentClass
{
public virtual object Target
{
get{return new object();}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class ChildClass : ParentClass
{
public new string Target
{
get{return "";}
}
}

//Child class which inherits from Parent class and hides inherited member
Target, and also changes the return type
class SubChildClass : ChildClass
{

}
}
"satankidneypie" wrote:
感谢您的快速回复,

我想过使用它,但有几个问题。显然它们都具有相同的名称,因此我必须查看
PropertyType属性上的其他属性以获得正确的属性,但是,这个
的候选者将是...

1)属性类型属性 - 我不知道
属性的返回类型是什么,所以无法检查,有很多
实现''Parent Class''的类,都有不同的返回类型。

2)声明Type属性 - 这是属性声明的类型,所以如果我继承自ChildClass并且不重新定义Target属性,
声明类型将是ChildClass而不是我反映的对象的类型。

我需要什么do,以某种方式循环遍历类层次结构并找到
类最高(或最低,取决于你如何看待它)级别
声明Target属性,但我不知道如何没有它凌乱......

&quo t; Truong Hong Thi写道:
Thanks for the quick reply,

I thought about using that but there are several problems. Obviously they
both have the same name, so I have to look at other properties on the
PropertyType attribute to get the right one, however, the candidates for this
would be...

1) Property Type property - I don''t know what the return type of the
property is going to be so can''t check against that, there are loads of
classes that implement the ''Parent Class'', all with different return types.

2) Declaring Type Property - This is the type that the property is declared
in, so if I inherit from ChildClass and don''t redefine the Target property,
the declaring type will be ChildClass and not the Type of the object I am
reflecting.

What I need to do, is somehow loop around the class hierarchy and find the
class at the highest (or lowest depending on how you look at it) level that
declares the Target property, but I don''t know how without it being messy...

"Truong Hong Thi" wrote:
尝试调用GetProperties而不是GetProperty。然后循环返回
返回的数组,找到你想要的属性。

Thi
Try calling GetProperties instead of GetProperty. Then loop through the
returned array and find the property you want.

Thi



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

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