Wanzyee Studio

Extension methods for System.Type. More...

Static Public Member Functions

static string GetPrettyName (this Type type, bool full=false)
 Get a pretty readable name of the type, even generic, optional to use the full name. More...
 
static object GetDefault (this Type type)
 Get the default value of the type, just like default. More...
 
static bool IsCreatable (this Type type, bool exception=false)
 Determine if able to create an instance of the type. More...
 
static Type GetNestedType (this Type type, string name, bool isStatic)
 Get the named public or nonpublic nested type of the specified type. More...
 
static FieldInfo GetField (this Type type, string name, bool isStatic, Type fieldType=null)
 Get the named public or nonpublic FieldInfo of the specified type. More...
 
static PropertyInfo GetProperty (this Type type, string name, bool isStatic, Type propertyType=null, params Type[] indexTypes)
 Get the named public or nonpublic PropertyInfo of the specified type. More...
 
static MethodInfo GetMethod (this Type type, string name, bool isStatic, Type returnType=null, params Type[] paramTypes)
 Get the named public or nonpublic MethodInfo of the specified type. More...
 
static bool Is< T > (this Type type)
 Determine if the type is derived from the other type, or implements the interface. More...
 
static bool Is (this Type type, Type other)
 Determine if the type is derived from the other type, or implements the interface. More...
 
static Type GetItemType (this Type type)
 Return the element type of an array or list type, otherwise null. More...
 
static Type[] GetParents (this Type type)
 Get the parent hierarchy array, sorted from self to root type. More...
 
static Type[] GetChildren (this Type type, bool deep=false)
 Get all child types, excluding self, optional to find deep or directly inheritance only. More...
 
static Type[] GetDerivatives (this Type type, params Assembly[] dlls)
 Get all types derived from the specified type. More...
 
static Type[] FindTypes (this Assembly dll, Type type=null)
 Find all valid types in the assembly, optional to filter which derived from the specified type. More...
 
static string GetLocation (this Assembly dll)
 Get the location path if valid, otherwise empty. More...
 

Detailed Description

Extension methods for System.Type.

3

Member Function Documentation

static string GetPrettyName ( this Type  type,
bool  full = false 
)
static

Get a pretty readable name of the type, even generic, optional to use the full name.

This doesn't handle anonymous types.

Parameters
typeType.
fullIf set to true use the full name.
Returns
The pretty name.
static object GetDefault ( this Type  type)
static

Get the default value of the type, just like default.

Parameters
typeType.
Returns
The default value.
static bool IsCreatable ( this Type  type,
bool  exception = false 
)
static

Determine if able to create an instance of the type.

This only checks some basic conditions and might be not precise:

  1. Return false only if it's interface, abstract, generic definition, delegate.
  2. Recurse to check the element type of an array type.
  3. Recurse to check the generic arguments of a list or dictionary type.
Parameters
typeType.
exceptionIf invalid, throw an exception or return false.
Returns
true, if creatable, false otherwise.
static Type GetNestedType ( this Type  type,
string  name,
bool  isStatic 
)
static

Get the named public or nonpublic nested type of the specified type.

Parameters
typeType.
nameType name.
isStaticIf to get a static type.
Returns
The nested type.
static FieldInfo GetField ( this Type  type,
string  name,
bool  isStatic,
Type  fieldType = null 
)
static

Get the named public or nonpublic FieldInfo of the specified type.

Parameters
typeType.
nameField name.
isStaticIf to get a static field.
fieldTypeField type.
Returns
The field.
static PropertyInfo GetProperty ( this Type  type,
string  name,
bool  isStatic,
Type  propertyType = null,
params Type[]  indexTypes 
)
static

Get the named public or nonpublic PropertyInfo of the specified type.

Parameters
typeType.
nameProperty name.
isStaticIf to get a static property.
propertyTypeProperty type.
indexTypesIndex types.
Returns
The property.
static MethodInfo GetMethod ( this Type  type,
string  name,
bool  isStatic,
Type  returnType = null,
params Type[]  paramTypes 
)
static

Get the named public or nonpublic MethodInfo of the specified type.

Parameters
typeType.
nameMethod name.
isStaticIf to get a static method.
returnTypeReturn type.
paramTypesParameter types.
Returns
The method.
static bool Is< T > ( this Type  type)
static

Determine if the type is derived from the other type, or implements the interface.

Template Parameters
TThe other type.
Parameters
typeType.
Returns
true, if is, false otherwise.
static bool Is ( this Type  type,
Type  other 
)
static

Determine if the type is derived from the other type, or implements the interface.

Parameters
typeType.
otherThe other type.
Returns
true, if is, false otherwise.
static Type GetItemType ( this Type  type)
static

Return the element type of an array or list type, otherwise null.

Parameters
typeType.
Returns
The element type.
static Type [] GetParents ( this Type  type)
static

Get the parent hierarchy array, sorted from self to root type.

Parameters
typeType.
Returns
The parent hierarchy array.
static Type [] GetChildren ( this Type  type,
bool  deep = false 
)
static

Get all child types, excluding self, optional to find deep or directly inheritance only.

Parameters
typeType.
deepIf set to true deep.
Returns
The child types.
static Type [] GetDerivatives ( this Type  type,
params Assembly[]  dlls 
)
static

Get all types derived from the specified type.

Parameters
typeType.
dllsAssemblies to search within, or default from all assemblies.
Returns
The derived types.
static Type [] FindTypes ( this Assembly  dll,
Type  type = null 
)
static

Find all valid types in the assembly, optional to filter which derived from the specified type.

Parameters
dllAssembly.
typeType filter.
Returns
Types.
static string GetLocation ( this Assembly  dll)
static

Get the location path if valid, otherwise empty.

Parameters
dllAssembly.
Returns
Location.