mardi 18 avril 2017

Passing info of marked property into custom attribute

I am trying to create a custom attribute, in which an argument is the name of a property of the property the attribute is used on.

I guess that did not make much sense when typed out, so here's an example of what I am trying to do:

[Test(x => x.Paws)]
public Dog Dog { get; set; }

As you see, I have used the attribute on Dog, and use a lambda expression to pass the paws property of dog to the constructor of the attribute.

Is this possible? The best I have come up with so far is the following:

[Test(nameof(Dog.Paws)]
public Dog Dog { get; set; }

The reason why this solution is not optimal is due to the fact that there is no type checking (I could write nameof(Cat.Tail) as well.

So my question is; is this possible? And if so, how would one do it?





Aucun commentaire:

Enregistrer un commentaire