简体   繁体   中英

How i get an object instance's annotation or Custom Attribute?

Is it possible to access the Object instance's Annotation definition?

Lets Say I have the following Code:

public class A
{
    [CustomStuff( Value="" )]
    public B propertyB;
}

In another class i receive an instance of B as parameter, is it possible to get the annotations from that object?

Not possible, and it doesn't really make sense to be able to do it. Annotations are on properties, not instances. Take for example:

public class A
{
    [CustomStuff( Value="Something" )]
    public B One;

    [CustomStuff( Value="SomethingElse" )]
    public B Two;

    [MoreCustom( Value="" )]
    public B One;
}

And then using it:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM