0

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?

  • If it is not possible, I must have imagined this whole day – Ňɏssa Pøngjǣrdenlarp Dec 01 '15 at 23:05
  • @Plutonix I actually think it's *not* possible, at least not reliably. That is, getting the attribute which was defined in `A` on a property of type `B`, simply by having an instance of type `B` (what if other classes annotate their properties of `B` with different attributes? What if it's the same instance?) – Rob Dec 02 '15 at 00:39
  • I thought it's a duplicate and marked. But after reading the answers I thought that I misunderstood the question. I've already voted for reopening. Sorry guys. – Cheng Chen Dec 02 '15 at 02:45
  • And [this question](http://stackoverflow.com/questions/3656382/is-it-possible-to-initialize-a-property-of-an-attribute-class-by-where-its-mark) I asked a few years ago is also related, just FYI. – Cheng Chen Dec 02 '15 at 02:46
  • So, the question comes because im working with Selenium, and im trying to execute some javascript using Selenium, all WebElements are created as properties, but for some reason when i pass one of the WebElements that were created as properties ( they are already instanced and working ) when i pass it to the JavaScriptExecutor, it says that the web element is not valid. – Luis Javier Peña Ureña Dec 02 '15 at 14:00

1 Answers1

1

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?
Rob
  • 26,989
  • 16
  • 82
  • 98