我有一个带有静态字段和 json 的静态类。
我可以将 json 反序列化为动态对象,因此我拥有所有字段,并且它们与类中的静态字段完全匹配。
如何使用反射来枚举字段并将值从动态类复制到静态类字段中?
我无法改变架构,使其成为单例等等;它是共享代码,并且该类将保持静态,因为它是共享库使用的全局共享设置对象。
该解决方案需要使用反射,因为类会随着时间的推移而随着新成员的出现而演变。否则我可以编写一个自定义反序列化器。
添加更多细节,但确实不多:
我有这个静态类:
static class A
{
static int I;
static string S;
}
Run Code Online (Sandbox Code Playgroud)
和一个与字段完全匹配的 json:
{
"I" : 3,
"S" : "hello"
}
var Data = JsonConvert.Deserialize<dynamic>(...);
Run Code Online (Sandbox Code Playgroud)
我想用我从 json 反序列化到动态对象中的值来初始化 A 类的静态字段。
另一个编辑:
我想出了与大卫写的类似的东西,但是由于我使用反序列化器来转换类型,所以效率较低,所以大卫的解决方案更好。
这是我想出的:
foreach (var Destination in typeof(Settings).GetProperties())
{
var Name = Destination.Name;
var T = Destination.PropertyType;
var Value = JsonConvert.DeserializeObject("\"" + JT[Name] + "\"", T);
Destination.SetValue(null, Value);
}
Run Code Online (Sandbox Code Playgroud)
您可以通过一个匹配的非静态类、获取源和目标的属性并循环遍历每个类来轻松地完成此操作。例如,假设我们有两个类:
public static class A
{
public static int I { get; set; }
public static string S { get; set; }
}
public class B
{
public int I { get; set; }
public string S { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我们现在可以这样做:
public void MapToStaticClass(B source)
{
var sourceProperties = source.GetType().GetProperties();
//Key thing here is to specify we want the static properties only
var destinationProperties = typeof(A)
.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (var prop in sourceProperties)
{
//Find matching property by name
var destinationProp = destinationProperties
.Single(p => p.Name == prop.Name);
//Set the static property value
destinationProp.SetValue(null, prop.GetValue(source));
}
}
Run Code Online (Sandbox Code Playgroud)
另一种选择是反序列化JToken
并与反射结合使用:
var source = JsonConvert.DeserializeObject<JToken>(json);
Run Code Online (Sandbox Code Playgroud)
进而:
public void MapJTokenToStaticClass(JToken source)
{
var destinationProperties = typeof(A)
.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (JProperty prop in source)
{
var destinationProp = destinationProperties
.SingleOrDefault(p => p.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
var value = ((JValue)prop.Value).Value;
//The ChangeType is required because JSON.Net will deserialise
//numbers as long by default
destinationProp.SetValue(null, Convert.ChangeType(value, destinationProp.PropertyType));
}
}
Run Code Online (Sandbox Code Playgroud)