开发者

Is there any way to get the instance class inside the attribute code?

Is there any way to get the instance class inside the attribute开发者_运维技巧 codein C#?

public class MyAttirbute : Attribute {
    public someMethod(){
       this.InstanceClass????
    }
}


No, unfortunately not.

Attributes are per type and per member (ie. per "something" they're attached to), not per instance.

You could even inspect the attributes and call those methods on it without there ever being any instances of the class it has been applied to.

What are you trying to do that requires this?


If I understand you correctly, you want to get the Type ("class") where an attribute is applied on.

This information is not available unfortunately, but you can only obtain it the other way around using reflection by visiting all types in an assembly and getting their custom attributes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜