开发者

How to Call this Generic Method with a Generic Type?

开发者 https://www.devze.com 2023-03-12 05:25 出处:网络
I have this code at the moment: Method1<Class1<Class2>>();开发者_如何学运维 public void Method1<T>()

I have this code at the moment:

Method1<Class1<Class2>>();开发者_如何学运维

public void Method1<T>()
{
    // process
}

Class1 needs a generic type itself (Class2).

I have to call the Method1 about 10 times for all of which Class2 would be the same type.

So how could I call Method1 with something like following:

Method1<Class1<J>>();

Where J is a generic type itself for the Class1.


Yes.

The same applies here:

IList<IEnumerable<string>>


You can use the using directive in your class file to alias either one or both of your classes.

using J = Class2;
using MyClass = Class1<Class2>;

(Apply appropriate namespaces as necessary to the class names, like Foo.Bar.Class1, etc.)

Now you can invoke your method with any of the following statements

Method1<Class1<Class2>>();
Method1<Class1<J>>();
Method1<MyClass>();


Or even cleaner code as using using: create a real subclass:

class SpecializedClass1:Class1<Class2>
{
    /* empty or do whatever makes sense in this specialized variant */
}
0

精彩评论

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

关注公众号