[英]How do I detect an ExpandoObject vs a Dynamic Object?
How to I determine if a Type is an ExpandoObject vs a Dynamic object? 如何确定Type是ExpandoObject还是Dynamic对象?
This is returning true for both: 两者均返回true:
public static bool IsDynamicObject(Type type)
{
return typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type);
}
Example Code for Dynamic Object: 动态对象的示例代码:
public class Entity
{
public Guid Id { get; set; }
public String Name { get; set; }
}
Delta<Entity> x = new Delta<Entity>();
dynamic dynamicX = x;
dynamicX.Name = nameof(Entity);
dynamicX.Id = typeof(Entity).GUID;
Example Code for Expando Object: Expando对象的示例代码:
dynamic childX = new ExpandoObject();
childX.A = 1;
The ExpandoObject can be casted to a dictionary to get the member names and values 可将ExpandoObject强制转换为字典以获取成员名称和值
public static bool IsExpandoObject(object objectValue)
{
if (objectValue == null)
return false;
if (IsDynamicObject(objectValue.GetType()))
{
IDictionary<string, object> expandoPropertyValues = objectValue as IDictionary<string, object>;
return expandoPropertyValues != null;
}
return false;
}
public static bool IsDynamicObject(Type type)
{
return typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.