How To Test if a Type is Anonymous?
You can just check if the namespace is null.
public static bool IsAnonymousType(this object instance)
{
if (instance==null)
return false;
return instance.GetType().Namespace == null;
}
Quick and dirty:
if(obj.GetType().Name.Contains("AnonymousType"))
From http://www.liensberger.it/web/blog/?p=191:
private static bool CheckIfAnonymousType(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
// HACK: The only way to detect anonymous types right now.
return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false)
&& type.IsGenericType && type.Name.Contains("AnonymousType")
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
&& type.Attributes.HasFlag(TypeAttributes.NotPublic);
}
EDIT:
Another link with extension method: Determining whether a Type is an Anonymous Type