Es ist nicht (ganz) wahr, dass es kein Äquivalent in C# ist. Es gibt kein statisches Äquivalent, das Sie als Typ verwenden oder Methoden aufrufen können, wahr genug. Verwenden Sie dazu Jorge's answer.
Auf der anderen Seite, manchmal brauchen Sie die entsprechende Idee für die Reflexion, und es gibt ein Äquivalent dort. Wenn Sie:
interface IFoo<T>
{
T Bar(T t, int n);
}
können Sie ein Type
erhalten, die IFoo<int>
mit typeof(IFoo<int>)
darstellt. Weniger bekannt und eine teilweise Antwort auf Ihre Frage ist, dass Sie auch eine Type
erhalten können, die IFoo<T>
mit typeof(IFoo<>)
darstellt.
Dies ist nützlich, wenn Sie IFoo<T>
für einige T
durch Reflexion verwenden möchten und T
bis zur Laufzeit nicht wissen.
Type theInterface = typeof(IFoo<>);
Type theSpecificInterface = theInterface.MakeGenericType(typeof(string));
// theSpecificInterface now holds IFoo<string> even though we may not have known we wanted to use string until runtime
// proceed with reflection as normal, make late bound calls/constructions, emit DynamicMethod code, etc.
Was ist der Template-Typ endgültig gelöst? –