Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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/1/ssh/2.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#_Arrays_Generics_Collections - Fatal编程技术网

c#数组泛型,每个数组项的类型不同

c#数组泛型,每个数组项的类型不同,c#,arrays,generics,collections,C#,Arrays,Generics,Collections,我有一个结构定义了一些使用泛型的pair类型: struct SomeNameValuePair<T> { string TypeName; T Value; } struct SomeNameValuePair { 字符串类型名; T值; } 需要声明SomeNameValuePair的数组: SomeNameValuePair<T>[] someNameValuePair=new SomeNameValuePair<T>[3]; So

我有一个结构定义了一些使用泛型的pair类型:

struct SomeNameValuePair<T>
{
    string TypeName;
    T Value;
}
struct SomeNameValuePair
{
字符串类型名;
T值;
}
需要声明SomeNameValuePair的数组:

SomeNameValuePair<T>[] someNameValuePair=new SomeNameValuePair<T>[3];
SomeNameValuePair[]SomeNameValuePair=新的SomeNameValuePair[3];
问题是不同数组项的Value属性需要不同的类型。 在代码中,更容易理解:

SomeNameValuePair<int> tempSomeNameValuePair0;

tempSomeNameValuePair0.TypeName="int";
tempSomeNameValuePair0.Value=10;


SomeNameValuePair<double> tempSomeNameValuePair1;

tempSomeNameValuePair1.TypeName="double";
tempSomeNameValuePair1.Value=10.5;

tempSomeNameValuePair2.TypeName="string";
tempSomeNameValuePair2.Value="Random String";

someNameValuePair[0]=tempSomeNameValuePair0
someNameValuePair[1]=tempSomeNameValuePair1
someNameValuePair[2]=tempSomeNameValuePair2
SomeNameValuePair0;
tempSomeNameValuePair0.TypeName=“int”;
tempSomeNameValuePair0.Value=10;
SomeNameValuePair tempSomeNameValuePair1;
tempSomeNameValuePair1.TypeName=“double”;
tempSomeNameValuePair1.Value=10.5;
tempSomeNameValuePair2.TypeName=“string”;
tempSomeNameValuePair2.Value=“随机字符串”;
SomeNameValuePair0=tempSomeNameValuePair0
someNameValuePair[1]=tempSomeNameValuePair1
someNameValuePair[2]=tempSomeNameValuePair2

很明显,我的代码在实例化数组
someNameValuePair=new someNameValuePair[3]时无法正常工作,我将其项的数组属性值提交为T类型


有什么方法可以实现我在C#中的目标吗?

您需要一个非泛型基类:

class SomeNameValuePairBase
{
    string TypeName;
}
然后,所有泛型项都继承自该基类: (它的类,而不是struct,因为structs不能使用继承)

并像这样使用它:

someNameValuePair[0] = new SomeNewValuePair<double>();
someNameValuePair[0]=新的SomeNewValuePair();
此外,您不应使用“TypeName”,而应使用C#“is”运算符:

if(someValuePair[0] is someValuePair<double>) ...
如果(someValuePair[0]是someValuePair)。。。

请重新检查您的代码。你是缺少括号还是只有我?在你的实际项目中,你是否使用与示例中相同的类型?我只是在想一个普通的基类。如果使用具有共同祖先的类,则可以将其用于此类泛型集合。否则我会使用
对象
@dmitrypov No,上面的类型只是为了示例。我之所以需要这样做,是因为在某些情况下,类型在编译时是未知的。@geraldCelente泛型更像是编译时的泛型。请参阅“我正在将其项的数组属性值提交为T类型”——这根本没有意义。没有“T型”。那只是个占位符。你想要使用你的类型的方式,它甚至不应该是泛型的;只需创建
的类型
对象
。如果您想展示一个合理的代码示例,这说明了为什么您认为将所有这些不相关的对象放在同一个数组中是有意义的,那么可能会提供一个有用的答案。否则,就不清楚你想做什么。
someNameValuePair[0] = new SomeNewValuePair<double>();
if(someValuePair[0] is someValuePair<double>) ...