开发者

How does one get the type of a generic class with multiple type parameters? - C#

开发者 https://www.devze.com 2023-01-20 13:43 出处:网络
This compiles: public class A<T> { public void test() { var a = typeof (A<>); } } This does not:

This compiles:

public class A<T> {
    public void test() {
        var a = typeof (A<>);
    }
}

This does not:

public class A<T,S> {
    public void test() {
        var a = typeof (A<>);
    }
}

I get the error: Using the generic type 'A' requires 2 type arguments

How do I get a reference to the type of this generic t开发者_StackOverflow中文版ype with two arguments?


All you need is a comma:

var a = typeof (A<,>);

Note of course that this will return a System.Type that represents the unbound generic type A. Since the code is in a method that belongs to the type, you might just be looking for typeof (A<T, S>), depending on your requirements.


use A<,> not A<>

0

精彩评论

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