Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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/vb.net/14.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中字符串的数据类型列表创建元组#_C#_List_Tuples - Fatal编程技术网

C# 使用c中字符串的数据类型列表创建元组#

C# 使用c中字符串的数据类型列表创建元组#,c#,list,tuples,C#,List,Tuples,我需要从字符串中的数据类型列表创建元组,但并没有得到任何解决方案 下面是我想做的例子 string[] dataType = {"int", "float", "datetime"}; //I want to create list like this but dynamically datatype come from db. //List<Tuple<int, string, string>> list = new List<Tuple<int, str

我需要从字符串中的数据类型列表创建元组,但并没有得到任何解决方案

下面是我想做的例子

string[] dataType = {"int", "float", "datetime"};

//I want to create list like this but dynamically datatype come from db.
//List<Tuple<int, string, string>> list = new List<Tuple<int, string, string>>(); 

List<Tuple<dataType[0],dataType[1], dataType[2]>> list = new List<Tuple<dataType[0],dataType[1], dataType[2]>>();
//Here datatype value is in string so I want to convert string to actual datatype.
string[]dataType={“int”、“float”、“datetime”};
//我想创建这样的列表,但动态数据类型来自db。
//列表=新列表();
列表=新列表();
//这里的数据类型值是字符串,所以我想将字符串转换为实际的数据类型。

或者,如果有其他解决方案,请指导我。

您可以在这里使用
动态
来处理您的类型。例如,当您从数据库中读取三个值时,您可以分配它们,而不必担心以下类型:

dynamic a = 10;
dynamic b = "Some String";
dynamic c = new DateTime(2020,4,9);

var test = new System.Tuple<dynamic,dynamic,dynamic>(a, b, c);

不过,我不确定您是否可以基于字符串值设置变量类型(问题的第二部分)。

这是为了扩展我在问题下的评论,并向您展示我的意思

从字符串格式的类型列表中动态创建
元组的列表并不困难。例如,使用反射:

private Type InferType(string typeName)
{
    switch (typeName.ToLowerInvariant())
    {
        case "int":
            return typeof(int);
        case "float":
            return typeof(float);
        default:
            return Type.GetType($"System.{typeName}", true, true);
    }
}

private object CreateListOfTupleFromTypes(string[] types)
{
    var elementTypes = types.Select(InferType).ToArray();

    // Get Tuple<,,>
    var tupleDef = typeof(Tuple)
        .GetMethods(BindingFlags.Static | BindingFlags.Public)
        .First(mi => mi.Name == "Create"
            && mi.GetParameters().Count() == elementTypes.Length)
        .ReturnType
        .GetGenericTypeDefinition();

    // Get Tuple<int, float, DateTime>
    var tupleType = tupleDef.MakeGenericType(elementTypes);

    // Get List<Tuple<int, float, DateTime>>
    var listType = typeof(List<>).MakeGenericType(tupleType);

    // Create list of tuple.
    var list = Activator.CreateInstance(listType);

    return list;
}
但是,如果这样做,那么使用
元组就没有意义了。您只需要
列表


那么,这又回到了我的问题,你的代码中的元组列表是什么?

你可以使用
动态
作为类型,请给我举个例子。检查元组。创建,这可能会解决你的问题你有办法创建
列表
,但你不能在代码中使用创建的值作为
列表
。一切都必须在运行时完成。你想对创建的列表做什么?嗨@PravinTukadiya,请看我发布的答案。OP使用的是
System.Tuple
而不是
ValueTuple
。我认为他不是在要求动态,而是希望Tuple是实际类型的。@SinaHoseinkhani,我能想到的接近这一点的唯一方法是编写一个工厂方法,返回该类型的变量。您可以创建一个switch语句:switch(typeString):{case“string”:返回字符串a;case“int”:返回int a;…}
a.GetType().ToString();
private Type InferType(string typeName)
{
    switch (typeName.ToLowerInvariant())
    {
        case "int":
            return typeof(int);
        case "float":
            return typeof(float);
        default:
            return Type.GetType($"System.{typeName}", true, true);
    }
}

private object CreateListOfTupleFromTypes(string[] types)
{
    var elementTypes = types.Select(InferType).ToArray();

    // Get Tuple<,,>
    var tupleDef = typeof(Tuple)
        .GetMethods(BindingFlags.Static | BindingFlags.Public)
        .First(mi => mi.Name == "Create"
            && mi.GetParameters().Count() == elementTypes.Length)
        .ReturnType
        .GetGenericTypeDefinition();

    // Get Tuple<int, float, DateTime>
    var tupleType = tupleDef.MakeGenericType(elementTypes);

    // Get List<Tuple<int, float, DateTime>>
    var listType = typeof(List<>).MakeGenericType(tupleType);

    // Create list of tuple.
    var list = Activator.CreateInstance(listType);

    return list;
}
var list = new List<ITuple>();
list.Add(new Tuple<int, float, DateTime>(...);

int value1 = (int)list[0][0];
float value1 = (float)list[0][1];
DateTime value1 = (DateTime)list[0][2];