有沒有辦法生成動態物件的類定義?
- 如果沒有使用像一個在線工具,這
例如:
dynamic test = new System.Dynamic.ExpandoObject();
test.property1 = "test";
test.property2 = 123;
到
public class test
{
string property1;
int property2;
}
我的用例涉及決議包含名稱型別和值(簡單地說)的檔案并從中生成 ac# 類。我在這里有兩個選擇,將名稱和型別存盤為字串或使用動態物件,這似乎更合適。然后應將此物件的類定義生成到 c# 代碼中。
編輯:序列化可能是錯誤的詞 - 代更適合
uj5u.com熱心網友回復:
你可以像這樣制作一個運行時代碼生成器:
public static string GenerateClass(string className, ExpandoObject eObj)
{
var sb = new StringBuilder();
sb.Append("public class ");
sb.Append(className);
sb.AppendLine();
sb.Append("{");
sb.AppendLine();
foreach (var property in (IDictionary<String, Object>)eObj)
{
sb.Append(" ");
sb.Append(property.Value.GetType());
sb.Append(" ");
sb.Append(property.Key);
sb.Append(" ");
sb.Append("{ get; set; }");
sb.AppendLine();
}
sb.Append("}");
sb.AppendLine();
return sb.ToString();
}
傳遞您的 expando 物件回傳:
public class test
{
System.String property1 { get; set; }
System.Int32 property2 { get; set; }
}
用法:
static void Main(string[] args)
{
dynamic test = new System.Dynamic.ExpandoObject();
test.property1 = "test";
test.property2 = 123;
Console.WriteLine(GenerateClass("test", (ExpandoObject)test));
}
**編輯:對于嵌套型別,您可以這樣做:**
public static string GenerateClass(string className, ExpandoObject eObj)
{
var sb = new StringBuilder();
sb.Append("public class ");
sb.Append(className);
sb.AppendLine();
sb.Append("{");
sb.AppendLine();
foreach (var property in (IDictionary<String, Object>)eObj)
{
string typeName = property.Value.GetType().Name;
if (property.Value is ExpandoObject nestedEObj)
{
typeName = property.Key;
sb.AppendLine();
foreach (string nestedClassLine in GenerateClass(property.Key, nestedEObj).Split(Environment.NewLine))
{
sb.Append(" ");
sb.Append(nestedClassLine);
sb.AppendLine();
}
}
sb.Append(" ");
sb.Append(typeName);
sb.Append(" ");
sb.Append(property.Key);
sb.Append(" ");
sb.Append("{ get; set; }");
sb.AppendLine();
}
sb.Append("}");
sb.AppendLine();
return sb.ToString();
}
用法:
static void Main(string[] args)
{
dynamic test = new System.Dynamic.ExpandoObject();
test.property1 = "test";
test.property2 = 123;
dynamic nest = new System.Dynamic.ExpandoObject();
nest.property1 = "testForNest";
nest.property2 = 456;
test.nest = nest;
Console.WriteLine(GenerateClass("test", (ExpandoObject)test));
}
產生:
public class test
{
String property1 { get; set; }
Int32 property2 { get; set; }
public class nest
{
String property1 { get; set; }
Int32 property2 { get; set; }
}
nest nest { get; set; }
}
uj5u.com熱心網友回復:
好吧,這里有一些棘手的解決方案:你可以使用 JSON 序列化
using System;
using Newtonsoft.Json;
namespace XY
{
class MainClass
{
public static void Main(string[] args)
{
dynamic x = new System.Dynamic.ExpandoObject();
x.property1 = "sadsa";
var xy = JsonConvert.DeserializeObject<Test>(JsonConvert.SerializeObject(x));
Console.WriteLine(xy.property1);
Console.ReadKey();
}
}
public class Test
{
public string property1 { get; set; }
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407932.html
標籤:
上一篇:子查詢中的計數正在洗掉空值-我如何包含它們?MYSQL
下一篇:MediatRIPipelineBehavior<TRequest,TResponse>錯誤為型別“TRequest”不能用作泛型型別或方法中的型別引數“TRequest”
