这里伪代码来解释我的问题一个泛型类型:动态创建它们使用该类型本身作为泛型参数
public class A<TX, TA> where TA : A<TX, TA>
{
}
public void Do()
{
var doy = typeof(A<,>).MakeGenericType(typeof(string), null/*??????*/));
}
我怎样才能让一个类型从我的泛型类型,如果我不能identifty我键入将其用作参数?
感谢您的帮助。
编辑,例如静态这样做的:
public abstract class A<TX, TA> where TA : A<TX, TA>
{
public TA ChainMethod()
{
return (TA)this;
}
}
public class AConcrete : A<string, AConcrete>
{
public AConcrete OtherMethod()
{
return this;
}
}
public class App
{
public static void Start()
{
new AConcrete().ChainMethod().ChainMethod().OtherMethod();
}
}
如果这是CRTP - 它会变得非常丑陋;你如何用C#表达它? A
你不能。但是,“动态地”添加是一个红色的鲱鱼 - 它不像是静态*这样做的任何方式。 'A' - 我该如何扩大“???”?必须有一个基地的地方。这个类本身是合法的,但你至少需要一个其他类来引导它。 [Eric Lippert的评论](https://blogs.msdn.microsoft.com/ericlippert/2011/02/03/curiouser-and-curiouser/)对此感到困惑,并且可能无法实现您想要的内容,无论如何仍然有效。 –
你能解释一个更高层次的需求吗? – Andrei