如何获得对象实例';s注释或自定义属性
本文关键字:注释 自定义属性 何获得 对象 实例 | 更新日期: 2023-09-27 18:25:13
是否可以访问Object实例的Annotation定义?
假设我有以下代码:
public class A
{
[CustomStuff( Value="" )]
public B propertyB;
}
在另一个类中,我收到一个B的实例作为参数,是否可以从该对象中获得注释?
这是不可能的,而且能够做到这一点也没有意义。注释是在属性上,而不是在实例上。举个例子:
public class A
{
[CustomStuff( Value="Something" )]
public B One;
[CustomStuff( Value="SomethingElse" )]
public B Two;
[MoreCustom( Value="" )]
public B One;
}
然后使用它:
var a = new A();
DoSomething(a.One);
public void DoSomething(B instance)
{
//What should the result be here?
//Should be 'CustomStuff:Something', right?
}
var a = new A();
a.Two = a.One
DoSomething(a.One);
//Now what should it be?
var a = new A();
a.Two = a.One
var tmp = a.One;
a.One = a.Two = null;
DoSomething(tmp);
//And now?