如果动态对象不包含属性,则获取默认值
本文关键字:获取 默认值 属性 包含 动态 对象 如果 | 更新日期: 2023-09-27 18:14:30
在许多语言中处理动态对象时,有一个结构允许您获取属性的值,如果该属性不存在,则返回默认值。
我想知道在。net中使用动态时是否有类似的方法/语法。我知道可以将ExpandoObject强制转换为Dictionary,但有时不能保证动态对象就是ExpandoObject。
我正在考虑一些与下面代码
具有相同效果的东西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'
如果我将注释中的答案转换为非extension方法,则如下工作:
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");
我想知道在。net中使用动态时是否有类似的方法/语法。我知道可以将ExpandoObject强制转换为Dictionary,但有时不能保证动态对象就是ExpandoObject。
也不能保证它是编译时对象。
你可以使用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
的一些调用将返回一个值,而另一些调用将抛出异常。所以我要说,没有安全的方法来测试它,对于一个可能不存在的方法也没有任何默认值
如果你在谈论ExpandoObjects,它们只是字典。这意味着您可以将它们转换为一个dictionary,并查看是否存在与您的属性名称匹配的键。这些都可以在一个泛型扩展方法中完成。
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);
}