
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Determining if a Type or Method Is Generic
|
783
If you are using Dotfuscator, you need to allow it to use these
attributes by setting the Honor Obfuscation Attributes button to
enabled.
See Also
See the “ObfuscationAssemblyAttribute Class,” “ObfuscationAttribute Class,” and
“Declarative Obfuscation” topics in the MSDN documentation.
13.13 Determining if a Type or Method Is Generic
Problem
You need to test a type and/or a method to determine whether it is generic.
Solution
Use the IsGenericType method of the Type class and the IsGenericMethod method of
the
MethodInfo class:
public static bool IsGenericType(Type type)
{
return (type.IsGenericType);
}
public static bool IsGenericMethod(MethodInfo mi)
{
return (mi.IsGenericMethod);
}
Discussion
The IsGenericType method examines objects and the IsGenericMethod method exam-
ines methods. These methods will return a
true indicating that this object or method
accepts type arguments and
false indicating that it does not. One or more type argu-
ments indicate that this type is a generic type.
To call these methods, use code like the following:
Assembly asm = Assembly.GetExecutingAssembly( );
// Get the type.
Type t = typeof(CSharpRecipes.DataStructsAndAlgorithms.PriorityQueue<int>);
bool genericType = IsGenericType(t);
bool genericMethod = false;
foreach (MethodInfo ...