反思属性以获得属性。 当它们在其他地方定义时该怎么办?
我有一个像这样的 Bar 类:
class Foo : IFoo {
[Range(0,255)]
public int? FooProp {get; set}
}
class Bar : IFoo
{
private Foo foo = new Foo();
public int? FooProp { get { return foo.FooProp; }
set { foo.FooProp= value; } }
}
我需要找到仅反映 Bar.FooProp 属性的属性 [Range(0,255)]。 我的意思是,当我当前正在解析时,该 prop 是在类实例 (.. new Foo()) 中装饰的,而不是在类中装饰的。 事实上 Bar.FooProp 没有属性
编辑
我在接口的定义上移动了属性,所以我要做的就是解析继承的接口以找到它们。 我可以这样做,因为 Bar 类必须实现 IFoo。在这种特殊情况下,我很幸运,但当我没有接口时问题仍然存在......我下次会注意
foreach(PropertyInfo property in properties)
{
IList<Type> interfaces = property.ReflectedType.GetInterfaces();
IList<CustomAttributeData> attrList;
foreach(Type anInterface in interfaces)
{
IList<PropertyInfo> props = anInterface.GetProperties();
foreach(PropertyInfo prop in props)
{
if(prop.Name.Equals(property.Name))
{
attrList = CustomAttributeData.GetCustomAttributes(prop);
attributes = new StringBuilder();
foreach(CustomAttributeData attrData in attrList)
{
attributes.AppendFormat(ATTR_FORMAT,
GetCustomAttributeFromType(prop));
}
}
}
}
I have a class Bar like this:
class Foo : IFoo {
[Range(0,255)]
public int? FooProp {get; set}
}
class Bar : IFoo
{
private Foo foo = new Foo();
public int? FooProp { get { return foo.FooProp; }
set { foo.FooProp= value; } }
}
I need to find the attribute [Range(0,255)] reflecting ONLY on the property Bar.FooProp. I mean, the prop is decorated in the class instance (.. new Foo()) not in the class when I am currently parsing. Infact Bar.FooProp has no attributes
EDIT
I moved attributes on the interface's definition, so what I have to do is parsing the inherited interfaces to find them. I can do that because Bar class must implement IFoo.In this particular case, I'm lucky, but the problem remains when I have no interfaces... I will take note for the next time
foreach(PropertyInfo property in properties)
{
IList<Type> interfaces = property.ReflectedType.GetInterfaces();
IList<CustomAttributeData> attrList;
foreach(Type anInterface in interfaces)
{
IList<PropertyInfo> props = anInterface.GetProperties();
foreach(PropertyInfo prop in props)
{
if(prop.Name.Equals(property.Name))
{
attrList = CustomAttributeData.GetCustomAttributes(prop);
attributes = new StringBuilder();
foreach(CustomAttributeData attrData in attrList)
{
attributes.AppendFormat(ATTR_FORMAT,
GetCustomAttributeFromType(prop));
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不久前我遇到过类似的情况,我在接口中的方法上声明了一个属性,并且我想从实现该接口的类型上的方法获取该属性。 例如:
下面的代码用于检查该类型实现的所有接口,查看给定的
方法
实现了哪些接口成员(使用GetInterfaceMap
),并返回任何属性关于那些成员。 在此之前,我还检查方法本身是否存在该属性。I had a similar situation a while back where I had an attribute declared on a method in an interface, and I wanted to get the attribute from a method on a type implementing the interface. For example:
The code below is used to check all of the interface implemented by the type, see which interface members the given
method
implements (usingGetInterfaceMap
), and returns any attributes on those members. Right before this, I also check if the attribute is present on the method itself.当查看
FooProp
时,没有任何东西可以识别Foo
的存在(在任何时候)。 也许您可以添加一个属性来标识foo
字段,并对此进行反思(通过FieldInfo.FieldType
)?When looking at
FooProp
, there is nothing to identify the existence of aFoo
(at any point). Perhaps you could add an attribute to identify thefoo
field, and reflect on that (viaFieldInfo.FieldType
)?