Class TypeExtensions
Provides a set of extension methods for System.Type.
Inheritance
Namespace: System
Assembly: Nuqleon.Linq.CompilerServices.dll
Syntax
public static class TypeExtensions : Object
Methods
FindGenericType(Type, Type)
Gets the closed generic type implemented by the specified type, matching the supplied generic type definition.
Declaration
public static Type FindGenericType(this Type type, Type definition)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type to check for an implementation of the generic type. |
System.Type | definition | The generic type definition to search for. |
Returns
Type | Description |
---|---|
System.Type | Closed generic type implemented by the given type, matching the given generic type definition. |
IsAnonymousType(Type)
Checks whether the specified type is a compiler-generated anonymous type.
Declaration
public static bool IsAnonymousType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to check for anonymous type implementation. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsClosureClass(Type)
Checks whether the specified type is a compiler-generated closure class.
Declaration
public static bool IsClosureClass(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to check for closure class implementation. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsCompilerGenerated(Type)
Checks whether the specified type is a compiler-generated type.
Declaration
public static bool IsCompilerGenerated(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to check. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsRecordType(Type)
Checks whether the specified type is a compiler-generated record type.
Declaration
public static bool IsRecordType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type to check for record type implementation. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsReferenceAssignableFrom(Type, Type, out IDictionary<Type, Type>, Boolean)
Checks whether the specified left-hand side type is assignable from the specified right-hand side type, taking wildcard type unification into account.
Declaration
public static bool IsReferenceAssignableFrom(this Type lhsType, Type rhsType, out IDictionary<Type, Type> substitutions, bool throwException = false)
Parameters
Type | Name | Description |
---|---|---|
System.Type | lhsType | Left-hand side type to check for assignability. |
System.Type | rhsType | Right-hand type to check for assignability. |
System.Collections.Generic.IDictionary<System.Type, System.Type> | substitutions | Dictionary containing mappings of wildcards to types to satisfy the assignment. |
System.Boolean | throwException | Indicates whether to throw an exception upon failing the assignability check. |
Returns
Type | Description |
---|---|
System.Boolean | true of the right-hand side type is assignable to the left-hand side type; otherwise, false. |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Thrown when the assignment check failed and |
IsSZArray(Type)
Checks whether the specified type
is a one-dimensional vector array.
Declaration
public static bool IsSZArray(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type to check. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
The name of this method is consistent with internal naming of one-dimensional vector
array types in the CLR. The CoreCLR implementation is exposing this as a property
named IsSzArray
, so we're being consistent with the future here.
ToCSharpString(Type)
Returns the C# code string representation of the type, without expanded namespaces and using C# type aliases. When compiler-generated types are encountered, an exception will be thrown.
Declaration
public static string ToCSharpString(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type whose name to print as C# code. |
Returns
Type | Description |
---|---|
System.String | C# code string representation of the type. |
ToCSharpString(Type, Boolean, Boolean, Boolean)
Returns the C# code string representation of the type, using the specified configuration settings.
Declaration
public static string ToCSharpString(this Type type, bool useNamespaceQualifiedNames, bool useCSharpTypeAliases, bool disallowCompilerGeneratedTypes)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type whose name to print as C# code. |
System.Boolean | useNamespaceQualifiedNames | Indicates whether to use type names that include namespace names. |
System.Boolean | useCSharpTypeAliases | Indicates whether to use C# names for primitive types. |
System.Boolean | disallowCompilerGeneratedTypes | Indicates whether to throw upon encountering a C# compiler generated type name. When allowed, a type name may be returned that isn't valid C# code. |
Returns
Type | Description |
---|---|
System.String | C# code string representation of the type. |
ToCSharpStringPretty(Type)
Returns the C# code string representation of the type, without expanded namespaces and using C# type aliases. When compiler-generated types are encountered, its underlying name will be used.
Declaration
public static string ToCSharpStringPretty(this Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Type whose name to print as C# code. |
Returns
Type | Description |
---|---|
System.String | String representation of the type using C# syntax. If compiler-generated type names occur, the output will not be valid C#. |
TryUnifyExact(Type, Type, out IDictionary<Type, Type>)
Tries to unify two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing.
Declaration
public static bool TryUnifyExact(this Type type1, Type type2, out IDictionary<Type, Type> result)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type1 | First type to unify. |
System.Type | type2 | Second type to unify. |
System.Collections.Generic.IDictionary<System.Type, System.Type> | result | Map of wildcards to their resolved bound type. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
It is common for one type to contain wildcards while the other doesn't contain any wildcards, though this is not required. In case both types contain wildcards, identity of wildcard types is used to resolve bindings.
TryUnifyExact(Type, Type, IEqualityComparer<Type>, out IDictionary<Type, Type>)
Tries to unify two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing.
Declaration
public static bool TryUnifyExact(this Type type1, Type type2, IEqualityComparer<Type> comparer, out IDictionary<Type, Type> result)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type1 | First type to unify. |
System.Type | type2 | Second type to unify. |
System.Collections.Generic.IEqualityComparer<System.Type> | comparer | Type equality comparer for simple and open generic types. |
System.Collections.Generic.IDictionary<System.Type, System.Type> | result | Map of wildcards to their resolved bound type. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
It is common for one type to contain wildcards while the other doesn't contain any wildcards, though this is not required. In case both types contain wildcards, identity of wildcard types is used to resolve bindings.
TryUnifyWith(Type, Type, out IDictionary<Type, Type>)
Tries to unify two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing. Only the left type is allowed to have wildcards. One other limitation is that the wildcard attributes must not be on generic definitions.
Declaration
public static bool TryUnifyWith(this Type left, Type right, out IDictionary<Type, Type> result)
Parameters
Type | Name | Description |
---|---|---|
System.Type | left | The left type, may contain wildcards. |
System.Type | right | The right type, without wildcards. |
System.Collections.Generic.IDictionary<System.Type, System.Type> | result | Map of wildcards to their resolved bound type. |
Returns
Type | Description |
---|---|
System.Boolean |
|
TryUnifyWith(Type, Type, IEqualityComparer<Type>, out IDictionary<Type, Type>)
Tries to unify two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing. Only the left type is allowed to have wildcards. One other limitation is that the wildcard attributes must not be on generic definitions.
Declaration
public static bool TryUnifyWith(this Type left, Type right, IEqualityComparer<Type> comparer, out IDictionary<Type, Type> result)
Parameters
Type | Name | Description |
---|---|---|
System.Type | left | The left type, may contain wildcards. |
System.Type | right | The right type, without wildcards. |
System.Collections.Generic.IEqualityComparer<System.Type> | comparer | Type equality comparer for simple and open generic types. |
System.Collections.Generic.IDictionary<System.Type, System.Type> | result | Map of wildcards to their resolved bound type. |
Returns
Type | Description |
---|---|
System.Boolean |
|
UnifyExact(Type, Type)
Unifies two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing.
Declaration
public static IDictionary<Type, Type> UnifyExact(this Type type1, Type type2)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type1 | First type to unify. |
System.Type | type2 | Second type to unify. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IDictionary<System.Type, System.Type> | Map of wildcards to their resolved bound type. |
Remarks
It is common for one type to contain wildcards while the other doesn't contain any wildcards, though this is not required. In case both types contain wildcards, identity of wildcard types is used to resolve bindings.
UnifyExact(Type, Type, IEqualityComparer<Type>)
Unifies two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing.
Declaration
public static IDictionary<Type, Type> UnifyExact(this Type type1, Type type2, IEqualityComparer<Type> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type1 | First type to unify. |
System.Type | type2 | Second type to unify. |
System.Collections.Generic.IEqualityComparer<System.Type> | comparer | Type equality comparer for simple and open generic types. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IDictionary<System.Type, System.Type> | Map of wildcards to their resolved bound type. |
Remarks
It is common for one type to contain wildcards while the other doesn't contain any wildcards, though this is not required. In case both types contain wildcards, identity of wildcard types is used to resolve bindings.
UnifyReferenceAssignableFrom(Type, Type)
Unifies the specified left-hand side type with the specified right-hand side type, taking reference assignment compatibility in mind.
Declaration
public static IDictionary<Type, Type> UnifyReferenceAssignableFrom(this Type lhsType, Type rhsType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | lhsType | Left-hand side type to unify. |
System.Type | rhsType | Right-hand type to unify. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IDictionary<System.Type, System.Type> | Dictionary containing mappings of wildcards to types to satisfy the assignment. |
UnifyWith(Type, Type)
Unifies two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing. Only the left type is allowed to have wildcards. One other limitation is that the wildcard attributes must not be on generic definitions.
Declaration
public static IDictionary<Type, Type> UnifyWith(this Type left, Type right)
Parameters
Type | Name | Description |
---|---|---|
System.Type | left | The left type, may contain wildcards. |
System.Type | right | The right type, without wildcards. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IDictionary<System.Type, System.Type> | Map of wildcards to their resolved bound type. |
UnifyWith(Type, Type, IEqualityComparer<Type>)
Unifies two types by matching wildcards to types. Matches are exact; subtyping relationships and assignability properties are not taken into account. Unification fails if wildcard bindings are conflicting or missing. Only the left type is allowed to have wildcards. One other limitation is that the wildcard attributes must not be on generic definitions.
Declaration
public static IDictionary<Type, Type> UnifyWith(this Type left, Type right, IEqualityComparer<Type> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Type | left | The left type, may contain wildcards. |
System.Type | right | The right type, without wildcards. |
System.Collections.Generic.IEqualityComparer<System.Type> | comparer | Type equality comparer for simple and open generic types. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IDictionary<System.Type, System.Type> | Map of wildcards to their resolved bound type. |