C# 在C语言中构造泛型数据类型#

C# 在C语言中构造泛型数据类型#,c#,C#,我遇到的问题是如何使我的SetContainer更通用,以包含扩展CompoundValue的任何类型。 这就是我尝试的MapContainer。 但是我在state[“a”]=newsetcontainer上收到一个错误,说类型a不能显式转换为CompoundValue 我已经包括了一个相关的例子来说明下面的问题 using System; using System.Collections.Generic; using System.Linq; using System.Text; using

我遇到的问题是如何使我的
SetContainer
更通用,以包含扩展
CompoundValue
的任何类型。 这就是我尝试的
MapContainer
。 但是我在
state[“a”]=newsetcontainer
上收到一个错误,说类型a不能显式转换为CompoundValue

我已经包括了一个相关的例子来说明下面的问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Microsoft.Modeling; // SetContainer and MapContainer

namespace SGSN
{
    class ControlState
    {
        MapContainer<string, SetContainer<CompoundValue>> state;

        ControlState()
        {
            state = new MapContainer<string, SetContainer<CompoundValue>>();

            state["a"] = new SetContainer<A>(); //ERROR
            state["b"] = new SetContainer<B>(); //ERROR
            state["c"] = new SetContainer<C>(); //ERROR
            state["d"] = new SetContainer<D>(); //ERROR
        }
    }

    class A: CompoundValue
    {
        internal string a1;
        internal string a2;
        internal string a3;
        internal string a4;
        internal string a5;
        internal string a6;

        internal A(string a1, string a2, string a3,
            string a4, string a5, string a6)
        {
            this.a1= a1;
            this.a2= a2;
            this.a3= a3;
            this.a4= a4;
            this.a5= a5;
            this.a6= a6;
        }
    }

    class B: CompoundValue
    {
        internal string b1;
        internal string b2;
        internal string b3;

        internal B(string b1, string b2, string b3)
        {
            this.b1= b1;
            this.b2= b2;
            this.b3= b3;
        }
    }

    class C: CompoundValue
    {
        internal string c1;
        internal string c2;
        internal string c3;
        internal string c4;
        internal string c5;
        internal string c6;
        internal string c7;
        internal string c8;

        internal C(string c1, string c2, string c3,
            string c4, string c5, string c6, string c7, string c8)
        {
            this.c1 = c1;
            this.c2= c2;
            this.c3= c3;
            this.c4= c4;
            this.c5 = c5;
            this.c6= c6;
            this.c7= c7;
            this.c8= c8;
        }
    }

    class D: CompoundValue
    {
        internal string d1;
        internal string d2;
        internal string d3;

        internal D(string d1, string d2, string d3)
        {
            this.d1= d1;
            this.d2= d2;
            this.d3= d3;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用Microsoft.Modeling;//SetContainer和MapContainer
名称空间SGSN
{
类控制状态
{
地图容器状态;
ControlState()
{
state=新的MapContainer();
状态[“a”]=new SetContainer();//错误
状态[“b”]=new SetContainer();//错误
状态[“c”]=new SetContainer();//错误
状态[“d”]=new SetContainer();//错误
}
}
A类:复合值
{
内管柱a1;
内管柱a2;
内部字符串a3;
内部字符串a4;
内部字符串a5;
内管柱a6;
内部A(串a1、串a2、串a3、,
字符串a4、字符串a5、字符串a6)
{
这个。a1=a1;
这个。a2=a2;
这个。a3=a3;
这个。a4=a4;
这个。a5=a5;
这个。a6=a6;
}
}
B类:复合值
{
内管柱b1;
内部字符串b2;
内部字符串b3;
内部B(管柱b1、管柱b2、管柱b3)
{
这是b1=b1;
这是b2=b2;
这个。b3=b3;
}
}
C类:复合值
{
内部字符串c1;
内部字符串c2;
内管柱c3;
内管柱c4;
内管柱c5;
内部字符串c6;
内部字符串c7;
内部字符串c8;
内部C(管柱c1、管柱c2、管柱c3、,
c4管柱、c5管柱、c6管柱、c7管柱、c8管柱)
{
这1.c1=c1;
这个。c2=c2;
这是c3=c3;
这个。c4=c4;
这1.c5=c5;
这个。c6=c6;
这个。c7=c7;
这个。c8=c8;
}
}
D类:复合值
{
内部字符串d1;
内部字符串d2;
内部字符串d3;
内部D(字符串d1、字符串d2、字符串d3)
{
这是d1=d1;
这是d2=d2;
这个。d3=d3;
}
}
}

您可以从SetContainer派生并在派生类中定义约束。您可以使用带有约束的派生类,而不是使用SetContainer

因此,首先要定义从SetContainer派生的内容:

public class YourContainer<T> : SetContainer<T>  where T : CompoundValue
{
   // no code needed here
}
MapContainer<string, YourContainer<CompoundValue>> state;
public类YourContainer:SetContainer其中T:CompoundValue
{
//这里不需要代码
}
之后,您将在任何地方使用容器,而不是SetContainer:

public class YourContainer<T> : SetContainer<T>  where T : CompoundValue
{
   // no code needed here
}
MapContainer<string, YourContainer<CompoundValue>> state;
MapContainer状态;
现在,您可以使用容器来初始化您的成员:

state = new MapContainer<string, YourContainer<CompoundValue>>();
state=newMapContainer();
现在可以添加定义了类型约束的容器,而不是添加SetContainer

state["a"] = new YourContainer<A>();
state[“a”]=新建YourContainer();

您可以从SetContainer派生并在派生类中定义约束。您可以使用带有约束的派生类,而不是使用SetContainer

因此,首先要定义从SetContainer派生的内容:

public class YourContainer<T> : SetContainer<T>  where T : CompoundValue
{
   // no code needed here
}
MapContainer<string, YourContainer<CompoundValue>> state;
public类YourContainer:SetContainer其中T:CompoundValue
{
//这里不需要代码
}
之后,您将在任何地方使用容器,而不是SetContainer:

public class YourContainer<T> : SetContainer<T>  where T : CompoundValue
{
   // no code needed here
}
MapContainer<string, YourContainer<CompoundValue>> state;
MapContainer状态;
现在,您可以使用容器来初始化您的成员:

state = new MapContainer<string, YourContainer<CompoundValue>>();
state=newMapContainer();
现在可以添加定义了类型约束的容器,而不是添加SetContainer

state["a"] = new YourContainer<A>();
state[“a”]=新建YourContainer();

您可以这样声明集合容器:

public class SetContainer<T> 
    where T : CompoundValue
{ ... }
这是不可能的,但让我们假设它是。现在,让我们向列表中添加值:

list.Add(new CompoundValue()); // Error!
list.Add(new A());             // OK
list.Add(new B());             // Error!

这似乎是可能的,因为
A
B
继承自
CompoundValue
。但是请记住,列表实际上是一个
列表
,此列表希望存储
a
a
的后代。
CompoundValue
B
都不是
A
s

您可以这样声明集合容器:

public class SetContainer<T> 
    where T : CompoundValue
{ ... }
这是不可能的,但让我们假设它是。现在,让我们向列表中添加值:

list.Add(new CompoundValue()); // Error!
list.Add(new A());             // OK
list.Add(new B());             // Error!

这似乎是可能的,因为
A
B
继承自
CompoundValue
。但是请记住,列表实际上是一个
列表
,此列表希望存储
a
a
的后代。
CompoundValue
B
都不是
A
s

你想做的是不可能的。即使类B派生自a,也不意味着Foo与Foo兼容

尝试编译以下内容:

class Foo<T> {}
class Bar {}
class Baz : Bar {}

void Main()
{
    var list = new List<Foo<Bar>>();
    list.Add(new Foo<Baz>());
}
class Foo{}
类条{}
类Baz:Bar{}
void Main()
{
var list=新列表();
添加(新的Foo());
}
您需要更改您的设计。尝试使用包装器类

class Foo<T> 
{
    public T Item { get; set; }
    public Foo(T t)
    {
        Item = t;
    }
}

class Bar 
{
    public override string ToString()
    {
        return "Bar";
    }
}

class Baz : Bar 
{
    public override string ToString()
    {
        return "Baz";
    }
}

class BarHolder 
{
    public Bar BarItem { get; set; }
    public BarHolder(Bar bar)
    {
        BarItem = bar;
    }
}

void Main()
{
    var wrappedBar = new BarHolder(new Bar());
    var wrappedBaz = new BarHolder(new Baz());

    var barList = new List<Foo<BarHolder>>();
    barList.Add(new Foo<BarHolder>(wrappedBar));
    barList.Add(new Foo<BarHolder>(wrappedBaz));

    foreach (var obj in barList)
    {
        Console.WriteLine(obj.Item.BarItem);
    }
}
class-Foo
{
公共T项{get;set;}
公共食物(T)
{
项目=t;
}
}
分类栏
{
公共重写字符串ToString()
{
返回“Bar”;
}
}
Baz类:酒吧
{
公共重写字符串ToString()
{
返回“Baz”;
}
}
等级酒吧老板
{
公共酒吧酒吧项目{get;set;}
公共酒吧持有人(酒吧)
{
气压=巴;
}
}
void Main()
{
var wrappedBar=新的杆支架(新杆());
var wrappedBaz=新的杆支架(new Baz());
var barList=新列表();
添加(新的Foo(wrappedBar));