【问题标题】:Get a default value if dynamic object doesn't contain property如果动态对象不包含属性,则获取默认值
【发布时间】:2015-09-25 06:10:22
【问题描述】:

在使用多种语言的动态对象时,有一个构造允许您获取属性的值,如果该属性不存在,则返回默认值。

我想知道在 .NET 中使用动态时是否有类似的方法/语法。我知道您可以将 ExpandoObject 转换为 Dictionary,但有时不能保证动态对象就是 Expando。

我正在考虑与以下代码具有相同效果的东西

public class SomeClass
{
    public string ValidProperty { get; set; }
}

dynamic t = new SomeClass() { 
    ValidProperty = "someValue"
};

Console.WriteLine(t.Get("ValidProperty", "doesn't exist")); // Prints 'someValue'
Console.WriteLine(t.Get("InvalidProperty", "doesn't exist")); // Prints 'doesn't exist'

【问题讨论】:

  • 可能必须使用反射来解决这个问题?
  • @Jeremy,例如,如果类型是 ExpandoObject 将不起作用(因为 Expando 没有属性,它只是一个字典)。此外,尝试将动态传递给扩展方法会导致 RuntimeException
  • 嗯,不,这不是 C# 团队的想法。使用这样的匿名类型最终会让您后悔,它的成员具有 internal 可访问性。换句话说,它不会跨越程序集边界。
  • 匿名类型只是为了写一个快速而肮脏的例子。我正在使用的课程是完全公开的课程。对此感到抱歉。

标签: c# .net dynamic


【解决方案1】:

我想知道在 .NET 中使用动态时是否有类似的方法/语法。我知道您可以将 ExpandoObject 转换为 Dictionary,但有时不能保证动态对象就是 Expando。

而且也不能保证它是一个编译时对象。

您可以使用 try/catch 但这甚至没有说明该属性的存在。动态对象示例:

 public class MyDynamic: DynamicObject
{
    static int Count = 0;
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = Count++;
        if (binder.Name == "Test")
        {
            return Count % 2 == 0;
        }
        return false;
    }
}

假设你把它当作

dynamic d = new MyDynamic();
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }
try { Console.WriteLine(d.Test); } catch (Exception ex) { Console.WriteLine(ex.Message); }

d.Test 的一些调用会返回一个值,而一些会抛出异常。所以我会说,没有安全的方法来测试它,并且没有任何可能不存在的方法的默认值。

【讨论】:

    【解决方案2】:

    如果您谈论的是 ExpandoObject,它们只是字典。这意味着您可以将它们转换为 IDictionary 并查看是否存在与您的属性名称匹配的键。这一切都可以在通用扩展方法中完成。

        public static T PropertyOrDefault<T>(this ExpandoObject obj, string propertyName)
        {
            var dynamicAsDictionary = (IDictionary<string, object>)obj;
    
            if (!dynamicAsDictionary.ContainsKey(propertyName))
            {
                return default(T);
            }
    
            object propertyValue = dynamicAsDictionary[propertyName];
    
            if (!(propertyValue is T))
            {
                return default(T);
            }
    
            return (T)propertyValue;
        }
    

    如果动态不是 ExpandoObject,那么您需要使用反射。

        public static T PropertyOrDefault<T>(dynamic obj, string propertyName)
        {
            if (obj is ExpandoObject)
            {
                return ((ExpandoObject)obj).PropertyOrDefault<T>(propertyName);
            }
    
            Type objectType = obj.GetType();
            PropertyInfo p = objectType.GetProperty(propertyName);
    
            if (p != null)
            {
                object propertyValue = p.GetValue(obj);
    
                if (propertyValue is T)
                {
                    return (T)propertyValue;
                }
            }
    
            return default(T);
        }
    

    【讨论】:

      【解决方案3】:

      如果我将评论中的答案转换为非扩展方法,则以下工作:

      public bool HasProperty(Object o, string propertyName)
      {
          return o.GetType().GetProperty(propertyName) != null;
      }
      

      ...

      dynamic t = new
                    {
                  validProperty = "value",
                    };
      
      MessageBox.Show(HasProperty(t, "invalidProperty")?"true":"false");
      MessageBox.Show(HasProperty(t, "validProperty")  ?"true":"false");
      

      【讨论】:

        猜你喜欢
        • 2016-02-11
        • 2011-01-17
        • 1970-01-01
        • 2018-03-06
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多