Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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#泛型问题_C#_Generics - Fatal编程技术网

C#泛型问题

C#泛型问题,c#,generics,C#,Generics,是否可以在c#中执行以下操作? 基本上,TParent和TChildren应该是A类的类型,但不一定具有传入的相同类型。我知道这听起来可能有点混乱,但我想强烈地键入特定对象的子对象和父对象,但同时它们必须是同一类型。因为TParent继承自,这意味着它需要继承自但使用可能不同类型的类型参数 public class A<TParent, TChildren> where TParent : A where TControls : A { TParent Pa

是否可以在c#中执行以下操作? 基本上,TParent和TChildren应该是A类的类型,但不一定具有传入的相同类型。我知道这听起来可能有点混乱,但我想强烈地键入特定对象的子对象和父对象,但同时它们必须是同一类型。因为TParent继承自,这意味着它需要继承自但使用可能不同类型的类型参数

public class A<TParent, TChildren> 
    where TParent : A
    where TControls : A
{
    TParent Parent;
    List<TChildren> Children;
}
公共A类
租金:A
其中t控制:A
{
t父母;
列出儿童名单;
}
或者更容易在这里看到:

public class A<TParent, TChildren>
    where TParent : A<?, ?>
    where TChildren : A<?, ?>
{
    TParent Parent;
    List<TChildren> Children;
}
公共A类
租金:A
孩子们:A
{
t父母;
列出儿童名单;
}
我希望这不会太令人困惑。这有可能吗

谢谢:)

如果你想说“父母必须是这种类型的孩子,而孩子必须是这种类型的家长”,那么我认为答案是否定的,你不能,因为你还需要知道父母的父母和孩子的孩子的类型

但是,您可以这样做

public interface IHasChildren<T>
{
    List<T> Children { get; set; }
}

public interface IHasParent<T>
{
    T Parent { get; set; }
}

public class A<TParent, TChildren> : IHasChildren<TChildren>, IHasParent<TParent>
    where TParent : IHasChildren<A<TParent, TChildren>>
    where TChildren : IHasParent<A<TParent, TChildren>>
{
    public List<TChildren> Children { get; set; }
    public TParent Parent { get; set; }
}
公共接口IHasChildren
{
列出子项{get;set;}
}
公共接口设备
{
T父{get;set;}
}
公共A级:iHasdren,iHasplant
地点t租金:我是孩子
孩子们:我在哪里
{
公共列表子项{get;set;}
公共TParent父项{get;set;}
}
如果你想说“父母必须是这种类型的孩子,而孩子必须是这种类型的家长”,那么我认为答案是否定的,你不能,因为你还需要知道父母的父母和孩子的孩子的类型

但是,您可以这样做

public interface IHasChildren<T>
{
    List<T> Children { get; set; }
}

public interface IHasParent<T>
{
    T Parent { get; set; }
}

public class A<TParent, TChildren> : IHasChildren<TChildren>, IHasParent<TParent>
    where TParent : IHasChildren<A<TParent, TChildren>>
    where TChildren : IHasParent<A<TParent, TChildren>>
{
    public List<TChildren> Children { get; set; }
    public TParent Parent { get; set; }
}
公共接口IHasChildren
{
列出子项{get;set;}
}
公共接口设备
{
T父{get;set;}
}
公共A级:iHasdren,iHasplant
地点t租金:我是孩子
孩子们:我在哪里
{
公共列表子项{get;set;}
公共TParent父项{get;set;}
}

不,这是不可能的。最接近的方法是定义另一个基类型,并要求TParent和TChild继承该基类型:

public class A { }  // or an interface

public class A<TParent, TChild> : A
  where TParent : A
  where TChild : A
{ }
公共类A{}//或接口
公开甲级:A
租金:A
孩子:A
{ }

这并不能保证TParent和TChild属于泛型
A
类型,但至少允许您对它们施加一些约束,而
A
实现可以采用这些约束。

不,这是不可能的。最接近的方法是定义另一个基类型,并要求TParent和TChild继承该基类型:

public class A { }  // or an interface

public class A<TParent, TChild> : A
  where TParent : A
  where TChild : A
{ }
公共类A{}//或接口
公开甲级:A
租金:A
孩子:A
{ }

这并不能保证TParent和TChild属于泛型
A
类型,但至少允许您对它们施加一些约束,而
A
实现可以采用这些约束。

不,这是不可能的。但是,一种解决方法是创建
a
的子类,并强制泛型类型参数为该类型:

public interface A
{
   // some properties and methods
}

public class A<TParent, TChild> : A where TParent : A where TChild A
{
   // something else here.  
}
公共接口A
{
//一些性质和方法
}
公共A类:A where t租金:A where t儿童A
{
//这里还有别的。
}

不,这是不可能的。但是,一种解决方法是创建
a
的子类,并强制泛型类型参数为该类型:

public interface A
{
   // some properties and methods
}

public class A<TParent, TChild> : A where TParent : A where TChild A
{
   // something else here.  
}
公共接口A
{
//一些性质和方法
}
公共A类:A where t租金:A where t儿童A
{
//这里还有别的。
}

只是为了澄清问题:您希望有两个相同类型的泛型参数,但不希望它们是同一个实例。@Michael Stum-两个泛型参数可以是相同的类型,但可能不同。只是为了澄清问题:您希望有两个相同类型的泛型参数,但是您不希望它们是相同的实例。@Michael Stum-两个泛型参数可以是相同的类型,但它们可能不同。