如何检测是否在一个ExpandoObject存在的属性? [英] How to detect if a property exists on an ExpandoObject?

查看:667
本文介绍了如何检测是否在一个ExpandoObject存在的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript的你可以,如果属性是使用未定义的关键字定义的检测:

In javascript you can detect if a property is defined by using the undefined keyword:

if( typeof data.myProperty == "undefined" ) ...

将如何在C#中做到这一点使用与动态关键字 ExpandoObject 并没有抛出异常?

推荐答案

根据 MSDN 的声明显示其正在实施的IDictionary:

According to MSDN the declaration shows it is implementing IDictionary:

public sealed class ExpandoObject : IDynamicMetaObjectProvider, 
    IDictionary<string, Object>, ICollection<KeyValuePair<string, Object>>, 
    IEnumerable<KeyValuePair<string, Object>>, IEnumerable, INotifyPropertyChanged

您可以用它来看看一个成员定义为:

You can use this to see if a member is defined:

var expandoObject = ...;
if(((IDictionary<String, object>)expandoObject).ContainsKey("SomeMember")) {
    // expandoObject.SomeMember exists.
}

这篇关于如何检测是否在一个ExpandoObject存在的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆