Bajtek80
Bajtek80

Reputation: 123

How can I pass current class type into <T>?

I have a attribute class (inheriting Attribute), inside which I have a method which calls a generic method:

memberInfo.GetCustomAttribute<T>();  

How can I pass current class type into T? I don't want to hard type the class name inside as it may be inherited and then it will not work.

Upvotes: 0

Views: 411

Answers (2)

rahulaga-msft
rahulaga-msft

Reputation: 4164

below should work :

memberinfo.GetType().GetMethod("GetCustomAttribute").MakeGenericMethod(t).Invoke(instance, new object[] { 'yourMethodParameter'} )

Upvotes: -1

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249636

You can use the version that takes Type, not the generic version :

memberInfo.GetCustomAttribute(this.GetType());  

Upvotes: 5

Related Questions