Skip to content

Instantly share code, notes, and snippets.

@hugoware
Created May 12, 2010 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoware/398099 to your computer and use it in GitHub Desktop.
Save hugoware/398099 to your computer and use it in GitHub Desktop.
/// <summary>
/// Attempts to identify an anonymous value using the type name and a variety of common traits
/// (Unlikely this is the best way to determine)
/// </summary>
public static bool IsAnonymousType(object value) {
//make sure this has a value
if (value == null) { return false; }
//check if this is anonymous type using the name and
//some values that hint it might be anonymous - This
//could probably be better though if a better way of
//checking for anonymous types becomes available
Type type = value.GetType();
return Regex.IsMatch(type.FullName, "^(<>f__AnonymousType|VB\\$AnonymousType)") &&
type.IsSealed &&
type.IsGenericType &&
type.BaseType.Equals(typeof(object));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment