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

C# 分析类型为字符串的动态数据

C# 分析类型为字符串的动态数据,c#,types,C#,Types,我有一个包含2列的sql表,不包括PK等 如何将value列中的值动态解析为DataType列中指定的类型。也可以有不同的数据类型:bool、double等(总是标准的。没有海关数据类型,所以我不需要获取程序集等) 当然,我可以有一个愚蠢的解决方案,比如: object value; if (DataType == "System.String") { value = Convert.ToString(XXXX); } 有没有更好的通用方法来动态解析这个问题 编辑: 你们

我有一个包含2列的sql表,不包括PK等


如何将
value
列中的值动态解析为
DataType
列中指定的类型。也可以有不同的数据类型:bool、double等(总是标准的。没有海关数据类型,所以我不需要获取程序集等)

当然,我可以有一个愚蠢的解决方案,比如:

 object value;

 if (DataType == "System.String")
 {
     value = Convert.ToString(XXXX);
 }
有没有更好的通用方法来动态解析这个问题

编辑:

你们觉得这个怎么样?这是一种过度的杀伤力吗

Type oType = Type.GetType("System.String");

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(VALUE_IN_HERE)))
{
    stream.Seek(0, SeekOriginal.Begin);
    var dcs = new DataContractSerializer(oType);
    return dcs.ReadObject(stream);
}

存储数据类型似乎是一种反模式,但您可以执行以下操作:

Type t = Type.GetType(DataType);
value = Convert.ChangeType(XXXX, t);

第一:不要获取字符串输出,如果可能,使用datareader、Linq映射或EntityFramework来访问列

第二:你可以用一个工厂来做。。。或者是因为没有更好的名字

Dictionary<String,Func<String,object>> factory
让它,你有两个变量的类型和值,只是执行

object val = factory[type](value);
您仍然没有类型信息,但可以更进一步:

Dictionary<String, Action<String>> factory;
factory["System.Int32"] = i => DoSomethingGeneric(Int32.Parse(i));
factory["System.String"] = s => DoSomethingGeneric(s);

private void DoSomethingGeneric<T>(T value)
{
  at least you have a type here and can do something with it
}
字典工厂;
factory[“System.Int32”]=i=>DoSomethingGeneric(Int32.Parse(i));
工厂[“System.String”]=s=>DoSomethingGeneric(s);
私人无效剂量(T值)
{
至少你有一个类型在这里,可以做些什么
}
仍然不是很有用,但您提到了泛型。如果您愿意独立处理这些值,您可以使用以下方法:

Dictionary<String, Action<String>> factory;
factory["System.Int32"] = i => DoSomethingWithInt(Int32.Parse(i));
factory["System.String"] = s => DoSomethingWithStrings(s);
字典工厂;
工厂[“System.Int32”]=i=>DoSomethingWithInt(Int32.Parse(i));
工厂[“System.String”]=s=>DoSomethingWithStrings;

将返回已解析的类型,即使您使用type对象继承了
数据。

当然,不要将类型存储在列中,而是为每个值类型提供一列,然后您可以使用
reader.GetString(…)
reader.GetInt32(…)
reader.GetDecimal(…)
reader.GetDateTime(…)
按名称创建
Type
对象并执行
Convert.ChangeType
数据库中的
值列到底是什么类型?@TimSchmelter:nvarchar bothSeams Hanselman有一个很好的解决方案:@Fabjan yes per OP的
值声明
<代码>动态值=
。。。可能是更好的,但我真的认为这只是一个创伤性代码库上的创可贴。哎哟,对不起<代码>值<代码>实际上是类型<代码>对象< /代码>,那么这是一个很好的答案。
Dictionary<String, Action<String>> factory;
factory["System.Int32"] = i => DoSomethingGeneric(Int32.Parse(i));
factory["System.String"] = s => DoSomethingGeneric(s);

private void DoSomethingGeneric<T>(T value)
{
  at least you have a type here and can do something with it
}
Dictionary<String, Action<String>> factory;
factory["System.Int32"] = i => DoSomethingWithInt(Int32.Parse(i));
factory["System.String"] = s => DoSomethingWithStrings(s);
Type foo = typeof(data);