Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可能T<;T>;_C# - Fatal编程技术网

C# 是否可能T<;T>;

C# 是否可能T<;T>;,c#,C#,我有一些类,例如A,还有一些类扩展了A(A1,A2)。A是一个容器-它只有字段、任何方法和属性 另一类是B,其中T:A,并扩展了B(B1,B2) 有可能在c#中这样做吗 public List DoSomething(对象参数) 其中T:B,new(),其中T1:A,new() 在该方法中,我必须动态创建T(B1、B2或B3)对象,并填充动态创建的T1对象(A1、A2、A3)的通用对象。是的,我可能可以使用Activator,但是使用新的T()会更好,并且使用更少的强制转换您不需要T 要稍微清

我有一些类,例如
A
,还有一些类扩展了
A(A1,A2)
A
是一个容器-它只有字段、任何方法和属性

另一类是
B,其中T:A
,并扩展了
B(B1,B2)

有可能在c#中这样做吗

public List DoSomething(对象参数)
其中T:B,new(),其中T1:A,new()
在该方法中,我必须动态创建T(B1、B2或B3)对象,并填充动态创建的T1对象(A1、A2、A3)的通用对象。是的,我可能可以使用Activator,但是使用新的T()会更好,并且使用更少的强制转换

您不需要
T

要稍微清理一下:

public List<T> DoSomething<T, T1>(object parameter) 
      where T : B<T1>, new()   // this specifies T<T1> already
      where T1 : A, new()
{
}
public List DoSomething(对象参数)
其中T:B,new()//这已经指定了T
其中T1:A,new()
{
}
假设
A
B
具有默认构造函数,则不需要
new()
约束

您可以这样做,但使用以下命令会很烦人:

public List<TB> DoSomething<TA, TB>(object parameter)
    where TA : A, new()
    where TB : B<TA>, new()
{ }

var list = DoSomething<A1, B1<A1>>(3);
public List DoSomething(对象参数)
其中TA:A,new()
其中TB:B,new()
{ }
var列表=剂量测定法(3);

是,但是不能为
T
使用类型参数(无论如何这是不必要的)。此外,必须指定
T
T1
作为方法的类型参数

public List<T> DoSomething<T, T1>(object parameter)
    where T : B<T1>, new()
    where T1 : A, new()
public List DoSomething(对象参数)
其中T:B,new()
其中T1:A,new()

如果我知道你在问什么,这是可能的-但你不需要
列表

公共类A{}
公共类ADerived:A{}
公共B类,其中Ta:A
{
公共Ta{get;set;}
}
公共类B驱动:B其中Ta:ADerived{}
公开课考试
{
公共列表剂量测量(Ta输入)
其中Tb:B,new()
其中Ta:A,new()
{
var list=新列表();
var b=新Tb();
b、 a=输入;
列表.添加(b);
退货清单;
}
公共int testThis()
{
var结果=剂量测定法(新的ADerived());
返回结果。计数;
}
}

是不是
B1
B2
也是泛型类?B1和B2是扩展的B,所以是的,是泛型让我头疼。。。你能用C语言描述一下你要做的事情吗?即使它不能编译?这可能是一个更好的起点。@user1091406
B1
也可能是继承自
B
的非泛型类,因此我无法从这个问题中得出结论。您是否试图实现复合模式?首先,您需要在
DoSomething
的定义中显式指定类型参数。其次,仅仅因为
A
B
有默认构造函数并不意味着任何派生类都有默认构造函数。您确实需要
new()
约束。@hvd:您在这两方面都是对的。我忘了把
放回去,傻瓜。编辑。那只是一份草稿。
public List<T> DoSomething<T, T1>(object parameter)
    where T : B<T1>, new()
    where T1 : A, new()
public class A {}
public class ADerived : A {}

public class B<Ta> where Ta : A
{
    public Ta a { get; set; }
}
public class BDerived<Ta> : B<Ta> where Ta : ADerived {}

public class Test
{
    public List<Tb> DoSomething<Tb, Ta>(Ta input) 
        where Tb : B<Ta>, new() 
        where Ta : A, new()
    {
        var list = new List<Tb>();

        var b = new Tb();
        b.a = input;

        list.Add(b);

        return list;
    }

    public int testThis()
    {
        var result = DoSomething<BDerived<ADerived>, ADerived>(new ADerived());
        return result.Count;
    }
}