Convert class to dynamic and add properties

后端 未结 5 1837
庸人自扰
庸人自扰 2020-12-04 01:53

I have a class MyClass. I would like to convert this to a dynamic object so I can add a property.

This is what I had hoped for:

dynamic          


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 02:33

    As my object has JSON specific naming, I came up with this as an alternative:

    public static dynamic ToDynamic(this object obj)
    {
      var json = JsonConvert.SerializeObject(obj);
      return JsonConvert.DeserializeObject(json, typeof(ExpandoObject));
    }
    

    For me the results worked great:

    Model:

    public partial class Settings
    {
      [JsonProperty("id")]
      public int Id { get; set; }
    
      [JsonProperty("runTime")]
      public TimeSpan RunTime { get; set; }
    
      [JsonProperty("retryInterval")]
      public TimeSpan RetryInterval { get; set; }
    
      [JsonProperty("retryCutoffTime")]
      public TimeSpan RetryCutoffTime { get; set; }
    
      [JsonProperty("cjisUrl")]
      public string CjisUrl { get; set; }
    
      [JsonProperty("cjisUserName")]
      public string CjisUserName { get; set; }
    
      [JsonIgnore]
      public string CjisPassword { get; set; }
    
      [JsonProperty("importDirectory")]
      public string ImportDirectory { get; set; }
    
      [JsonProperty("exportDirectory")]
      public string ExportDirectory { get; set; }
    
      [JsonProperty("exportFilename")]
      public string ExportFilename { get; set; }
    
      [JsonProperty("jMShareDirectory")]
      public string JMShareDirectory { get; set; }
    
      [JsonIgnore]
      public string Database { get; set; }
    }
    

    I used it in this manner:

    private static dynamic DynamicSettings(Settings settings)
    {
      var settingsDyn = settings.ToDynamic();
      if (settingsDyn == null)
        return settings;
      settingsDyn.guid = Guid.NewGuid();
      return settingsDyn;
    }
    

    And received this as a result:

    {
      "id": 1,
      "runTime": "07:00:00",
      "retryInterval": "00:05:00",
      "retryCutoffTime": "09:00:00",
      "cjisUrl": "xxxxxx",
      "cjisUserName": "xxxxx",
      "importDirectory": "import",
      "exportDirectory": "output",
      "exportFilename": "xxxx.xml",
      "jMShareDirectory": "xxxxxxxx",
      "guid": "210d936e-4b93-43dc-9866-4bbad4abd7e7"
    }
    

    I don't know about speed, I mean it is serializing and deserializing, but for my use it has been great. A lot of flexability like hiding properties with JsonIgnore.

    Note: xxxxx above is redaction. :)

提交回复
热议问题