I need to detect if an object was created anonymously like new{name=value,}
if it is an AnonymousType, it should add it's properties names/values into a
Dictionary<string,object>
This is what I hacked together myself:
var name="name";
var obj = new { name = new object(), };
var lookup = new Dictionary<string,object>();
if(obj.GetType().Name.StartsWith("<>f__AnonymousType"))
{
foreach (var property in obj.GetType().GetProperties())
{
lookup[property.Name] = property.GetValue(obj, null);
}
}
else
{
lookup[name]=obj;
}
I was wondering if there is a better/faster way of detecting AnonymousTypes, or if there is a better/faster way to dump an object's properties names/values into a
Dictionary<string,object>