提取要用作泛型<t>的对象类型

时间:2017-12-08 16:30:26

标签: c# generics reflection json.net

我有一个可以用

实例化的类bool value = true; VResult<bool> vr = new VResult<bool>(value);
value

如果我不知道VResult<typeof value> = new VResult<typeof value>(value); 的类型,我想做点什么

VResult<T>

这可能吗?

最终目标是序列化/反序列化string json = JsonConvert.SerializeObject(new VResult<bool>(true));

int

其中可以是对象或基本数据类型,例如boolValueTypeName = Value.GetType().Name;

我正在使用添加

的数据传输对象
ValueTypeNamespace = Value.GetType().Namespace;

JObject obj = JObject.Parse(json);
string vt = (string)obj["ValueTypeName"];
string vtn = (string)obj["ValueTypeNamespace"];
Type type = Type.GetType($"{vtn}.{vt}");
var value = Activator.CreateInstance(type);
value = obj["Value"];
VResult<typeof value> vr = new VResult<typeof value> (value); //not correct

属性,以便在接收方我可以使用

Type

获取有关value的所有<T>信息,但我不知道如何从value获取通用VResult<T>以将其传递给{{1}} {{1}}构造函数;

1 个答案:

答案 0 :(得分:3)

您可以像这样创建通用实例:

VResult<int>

现在您的value实例的1 //Creating rows and columns for text file echo "<h1>Data Table</h1>"; echo "<table border = 0 >"; foreach($result as $key=>$value){ echo "<tr>"; foreach($value as $v){ echo "<td>".$v."</td>"; } echo "</tr>"; } echo "</table>"; } function transpose_matrix($result) { $transpose = array(); // foreach ($result as $key => $sub) { foreach ($sub as $subkey => $subvalue) { $transpose[$subkey][$key] = $subvalue; } } return $transpose; } 。这是你在找什么?

相关问题