English 中文(简体)
将json卷宗注入C#的固定类别
原标题:Deserializing a json file into a static class in C#

我有一个固定等级,有固定的田地和一个小区。

我可以把 j子注入一个充满活力的物体,因此,我拥有所有的田地,完全与平级的静态田相匹配。

我怎么能用反思来列举领域,并将动态类别的价值复制到静态类别领域?

I can t change the architecture, make it a singleton, etc; it s shared code and the class is going to remain static since it s globally shared settings object used by a shared library.

解决办法需要加以思考,因为这一类别随着时间的推移而与新成员形成。 否则,我本可以写出一种帝国的习俗。


添加更多细节,但实际情况并非如此:

我有这一静态等级:

static class A
{
    static int I;
    static string S;
}

a) 与田间相匹配:

{
    "I" : 3,
    "S" : "hello"
}

var Data = JsonConvert.Deserialize<dynamic>(...);

我愿将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);
}
最佳回答

你可以很容易地这样做,拥有一个相应的非统计类别,获取来源和目的地的特性,并通过各自进行居住。 例如,假设我们有两个班子:

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; }
}

我们现在可以这样做:

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));
    }
}

另一种选择是将海流降为J Token,并同时使用:

var source = JsonConvert.DeserializeObject<JToken>(json);

之后:

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));
    }
}
问题回答

您可以避免中间(动力学)班,并直接读到静态班级,但需要复杂的代码:

var t = typeof(YourType);

var rdr = new JsonTextReader(new StringReader("{json text}"));
PropertyInfo pinf = null;
while (rdr.Read()) {
    if (rdr.TokenType == JsonToken.PropertyName) {
        pinf = t.GetProperty((string)rdr.Value);
    } else if (rdr.TokenType == JsonToken.String || rdr.TokenType == JsonToken.Boolean || rdr.TokenType == JsonToken.Date || 
                rdr.TokenType == JsonToken.Float || rdr.TokenType == JsonToken.Integer || rdr.TokenType == JsonToken.Null) {
        pinf.SetValue(null, rdr.Value);
    }
}
rdr.Close();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签