How do I detect an ExpandoObject vs a Dynamic Object?
How do I detect an ExpandoObject vs a Dynamic Object?
How to I determine if a Type is an ExpandoObject vs a Dynamic object?
This is returning true for both:
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:
dynamic childX = new ExpandoObject();
childX.A = 1;
dynamic a = 5;
int
a = "hello";
string
Both
ExpandoObject and DynamicObject are types derived from IDynamicMetaObjectProvider, but they are still types. Check for either– Nkosi
Jul 1 at 3:24
ExpandoObject
DynamicObject
IDynamicMetaObjectProvider
Why would you want to know?
– Dennis Kuypers
Jul 1 at 4:13
This is to fix this Github issue: github.com/GregFinzer/Compare-Net-Objects/issues/103
– Greg ''Wildman'' Finzer
Jul 1 at 11:27
1 Answer
1
The ExpandoObject can be casted to a dictionary to get the member names and values
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);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why do you mean "a dynamic object"? See here and here for why I'm asking.
dynamic a = 5;doesn't have a different type - it's anint, but then assigninga = "hello";makes it astring.– john
Jul 1 at 3:13