TypeHelper
Namespace: SnowBank.Runtime · class
Extensions methods that add advanced features on Type
Methods
CanAssignNull
static bool CanAssignNull(Type type)
Tests if an instance of type can be null (Reference Type, Nullable Type)
CompileGenerator
static Func<object> CompileGenerator(Type type)
Generates a factory method that will instantiate an object of the specified type, or equivalent, if possible
type— Type of the object to instantiate (can be an interface or abstract class, for a limited set of "well known" types)
Returns: Function that calls the parameterless constructor for the type, or null if it is impossible to create such a type (unsupported interface or abstract class, type without a parameterless ctor, ...)
CompileGetter
static Func<object, object> CompileGetter(FieldInfo field)
Generates a getter Lambda (object instance) => (object) ((TInstance) instance).FIELD
static Func<object, object> CompileGetter(PropertyInfo property, bool nonPublic = false)
Generates a getter Lambda (object instance) => (object) ((TInstance) instance).PROPERTY
Set to also use a non-public get accessor (compiled expression trees can invoke them)
CompileSetter
static Action<object, object> CompileSetter(FieldInfo field)
Generates a setter Lambda (object instance, object value) => ((TInstance) instance).FIELD = (TValue) value
Returns: Returns null if the field is read-only
static Action<object, object> CompileSetter(PropertyInfo property, bool nonPublic = false)
Generates a setter Lambda (object instance, object value) => ((TInstance) instance).PROPERTY = (TValue) value
Returns: Returns null if the property is read-only
Set to also use a non-public set accessor (compiled expression trees can invoke them)
ConvertKeyToString
static string ConvertKeyToString(object key)
Converts an arbitrary dictionary key to a string literal, that can be used as a key in a string-only map (such as the field name in a JSON Object)
key— Instance that represents a key
Returns: Corresponding string literal
FindGenericType
static Type FindGenericType(Type type, Type genericType)
Returns the closed version of the generic interface implemented by a type
type— Type of the inspected instance (ex: List) genericType— Type of the generic interface (ex: IList<>)
Returns: Closed interface (ex: IListnull if the type does not implement this generic interface
GetAssemblyName
static string GetAssemblyName(Type type)
Returns the full name of a type, that can be later passed to Type.GetType(...) to retrieve the original type
type— Type
Returns: Name of the type, with the form "Namespace.ClassName, AssemblyName"
GetCompilableTypeName
static string GetCompilableTypeName(Type type, bool omitNamespace, bool global)
Returns a "human-readable" version of a type's name, including the generic arguments and nested types.
Returns: "string", "int", "FooBar", "FooBar.NestedBaz", "List
This name will NOT be in a format that easily allows retrieve it later, and should mostly be used in error message, logs or debug messages.
GetDefaultValue
static object GetDefaultValue(Type type)
Returns the default value for a type
Returns: null, 0, false, DateTime.MinValue, ...
GetFriendlyName
static string GetFriendlyName(Type type)
Returns a "human-readable" version of a type's name, including the generic arguments and nested types.
Returns: "string", "int", "FooBar", "FooBar.NestedBaz", "List
This name will NOT be in a format that easily allows retrieve it later, and should mostly be used in error message, logs or debug messages.
static string GetFriendlyName(MethodBase method, bool omitNamespace = true)
Returns a "human-readable" version of a method's name, including the parameters.
Returns: "
GetTaskLikeType
static Type GetTaskLikeType(Type taskLike)
Returns the type returned by a 'Task-like' type (Task, Task>T<, ValueTask<T>, ...), or null if it is not a task.
IsAnonymousType
static bool IsAnonymousType(Type type)
Tests if a type is an anonymous type generated by the compiler (ie: new { foo = ..., bar = .... })
IsAssignableTo
static bool IsAssignableTo<T>(Type type)
Tests if a type is in instance of another type, abstract class, or interface
type— Type of the instance
Returns: true if an instance of type type can be assigned to a variable of type T
IsConcrete
static bool IsConcrete(Type type)
Tests if a type is "concrete", meaning it is not a class or struct that is not abstract
type— Type to inspect
Returns: false if the type is an interface an abstract class, or Object; otherwise, true
This includes both sealed class, and non-sealed but non-abstract class.
IsConcreteImplementationOfInterface
static bool IsConcreteImplementationOfInterface(Type type, Type interfaceType)
Tests if a type is a concrete implementation of a specific interface
Returns: false if the type does not implement this interface, is an itself an interface, an abstract class, or Object; otherwise, true
IsCustomIndexer
static bool IsCustomIndexer(PropertyInfo prop)
Tests if a property is a custom indexer (ex: "get_Item[string key]")
IsDictionaryType
static bool IsDictionaryType(Type type, out Type keyType, out Type valueType)
Tests if a type implements IDictionary and returns the types of the keys and values when this is the case
type— Type of the inspected instance (ex:List<string>)keyType— When the method returnstrue, receives the types of the keysvalueType— When the method returnstrue, receives the types of the values
Returns: true if the type implements IDictionary; otherwise, false.
typeof(Dictionary<int, string>).IsDictionaryType(out var keyType, out var valueType) == true; keyType == typeof(int); valueType == typeof(string)
typeof(List<KeyValuePair<int, string>>).IsDictionaryType(...) == false
IsEnumerableType
static bool IsEnumerableType(Type type, out Type elementType)
Tests if a type implements IEnumerable and returns the type of the enumerated elements when this is the case
type— Type of the inspected instance (ex:List<string>)elementType— When the method returnstrue, receives the types of the enumerated elements
Returns: true if the type implements IEnumerable; otherwise, false.
typeof(List<string>).IsEnumerableType(out var elementType) == true; elementType == typeof(string)
typeof(Dictionary<int, string>).IsEnumerableType(out var elementType) == true; elementType == typeof(KeyValuePair<int, string>)
typeof(string).IsEnumerableType(out var elementType) == true; elementType == typeof(char)
typeof(int).IsEnumerableType(out var elementType) == false;
IsGenericInstanceOf
static bool IsGenericInstanceOf(Type type, Type genericType)
Tests if a type is implements a generic type or interface
type— Type of the inspected instance (ex:List<string>)genericType— Type of the generic interface (ex:IList<T>)
Returns: true if the type implements the generic interface
This method is required because new List
typeof(List<string>).IsGenericInstanceOf(typeof(IList<>)) == true
typeof(HashSet<string>).IsGenericInstanceOf(typeof(IList<>)) == false
static bool IsGenericInstanceOf(Type type, Type genericType, out Type closedType)
Tests if a type is implements a generic type or interface
type— Type of the inspected instance (ex:List<string>)genericType— Type of the generic interface (ex:IList<>)closedType— Receives the corresponding closed generic type (ex:IList<string>)
Returns: true if the type implements the generic interface
This method is required because new List
typeof(List<string>).IsGenericInstanceOf(typeof(IList<>), out var closedType) == true && closedType == typeof(IList<string>)
typeof(HashSet<string>).IsGenericInstanceOf(typeof(IList<>)) == false
IsNullableType
static bool IsNullableType(Type type)
Tests if a type is nullable (ie: 'int?' or 'DateTime?')
IsSameGenericType
static bool IsSameGenericType(Type type, Type genericType)
Tests if a generic type has the same generic definition as another type (ie: Dictionary<string, int> ~= Dictionary<,>
type— Type to inspect (ex: IDictionary<string, string>)genericType— Generic type (ex: IDictionary<,>)
Returns: true if the types are equivalent; otherwise, false.
MakeGenericMethod
static MethodInfo MakeGenericMethod(Type type, string name, BindingFlags flags, params Type[] types)
Returns the generic version of a method
type— Declaring typename— Name of the methodflags— Binding flagstypes— Types of the generic arguments of the method
Returns: Corresponding generic method
TryGetCustomAttribute
static bool TryGetCustomAttribute(MemberInfo member, string attributeName, bool inherit, out Attribute attribute)
Returns an Attribute of a member of a type, given its name
member— Member (field or property)attributeName— Name (without the namespace) of the attribute to fetch (ex: "DataMemberAttribute")inherit—trueto also look into the attributes up the derived chainattribute— receives the correspondingAttributeif found; otherwise,null
Returns: true if the attribute was found; otherwise, false.