【问题标题】:Dynamically select part of object properties动态选择部分对象属性
【发布时间】:2019-10-15 14:10:31
【问题描述】:

有没有动态选择部分对象属性的方法?

我在 .net Core 3 上,在一个 web api 项目中

我需要根据其值向客户端发送源属性的子集

示例 1:

    source = { max="",min="1", label="Code"};
    sendToClient obj should be 
    output = { min="1",label="Code"}

示例 2:

    source = { max="35",min="1", label="MixerColor",width="",height="",rounded=null};
    sendToClient should be 
    output = { max="35",min="1",label="Code"}

如果 source[prop] 不是(null 或空)=> 将 source[prop] 发送给客户端

【问题讨论】:

    标签: c# .net class asp.net-core reflection


    【解决方案1】:

    好吧,您可以通过将对象转换为 json 并返回来做到这一点

    var source = new { max = "", min = "1", label = "Code" };
                var tmp=JObject.FromObject(source);
                tmp.Property("max").Remove();
               var newSource= JsonConvert.DeserializeObject(tmp.ToString());
    

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 2016-05-08
      相关资源
      最近更新 更多