【问题标题】:C# Generic way to store unknown properties in ListC#在List中存储未知属性的通用方法
【发布时间】:2020-09-14 15:53:06
【问题描述】:

我想在我的应用程序中存储多个域的 LicenseInformation。

结构如下所示:

   public class LicenseData
    {
        // properties...
        public List<LicenseDomain> Domains { get; set; }
        // other properties...
    }

    public class LicenseDomain
    {
        // properties...
        public object LicenseConfig { get; set; }
    }

我们有多个具有不同属性的域,但许可证可能包含多个配置..

例如:

{
   "MaxValidUsers": 5
}
{
   "Property": "xy"
   "SubProperty": { "Foo" : "Bar" 
   }
}

一代无论如何都没有问题.. 但是如果我从我签名的 json 文件中恢复信息,我会反序列化为对象..

我必须使用哪种模式/可能性使用接口/抽象/我可以(RE)在此处存储通用信息..

现在我破解:

JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(domain.LicenseConfig))

但我不能同意自己。

【问题讨论】:

标签: c# .net object generics casting


【解决方案1】:

因此,根据我可以获取的上下文,我实际上建议将您的 LicenseConfig 存储为 JSON 字符串,这样您就可以执行以下操作:

public class LicenseDomain
{
    // properties...

    // Depending on how this is loaded,
    // this property (or at least its setter) could be made private/protected/internal
    public string LicenseConfigJson { get; set; }

    public T LicenseConfig<T>() where T : BaseLicenseConfig
    {
        if (string.IsNullOrWhiteSpace(LicenseConfigJson))
        {
            return null;
        }
        return JsonConvert.DeserializeObject<T>(LicenseConfigJson);
    }
    public void SaveLicenseConfig<T>(T config) where T : BaseLicenseConfig
    {
        if (config == null)
        {
            LicenseConfigJson = null; 
        }
        else
        {
            LicenseConfigJson = JsonConvert.SerializeObject(config);
        }
    }


}

或者如果每个LicenseDomain 只能有一种LicenseConfig,您可以将其作为类的通用参数:

public class LicenseData
{
    // properties...
    public List<LicenseDomain<BaseLicenseConfig>> Domains { get; set; }
    // other properties...
}

public class LicenseDomain<T> where T : BaseLicenseConfig
{
    // properties...

    // Depending on where this value comes from, you could do this a variety of ways, 
    //but this is just one
    public string LicenseConfigJson { get; set; }
    public T LicenseConfig
    {
        get
        {
            if (string.IsNullOrWhiteSpace(LicenseConfigJson))
            {
                return null;
            }
            return JsonConvert.DeserializeObject<T>(LicenseConfigJson);
        }
        set
        {
            if (value == null)
            {
                LicenseConfigJson = null;
            }
            else
            {
                LicenseConfigJson = JsonConvert.SerializeObject(value);
            }
        }
    }
}

public abstract class BaseLicenseConfig
{
    
}

public class LicConfig1 : BaseLicenseConfig
{
    public int MaxValidUsers { get; set;}
}

public class LicConfig2 : BaseLicenseConfig
{
    public string Property {get;set;}
    public SubProp SubProperty {get;set;}
}

public class SubProp
{
    public string Foo {get;set;}
}

在这两种情况下,BaseLicenseConfig 类都严格强制域列表中的所有内容都可以来自某种基类。如果这不重要,则不需要基类,并且可以从 LicenseDomain 类中删除 where T : BaseLicenseConfig。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2023-01-07
    相关资源
    最近更新 更多