Class TypeSystemExtensions
- Namespace
- ICSharpCode.Decompiler.TypeSystem
- Assembly
- ICSharpCode.Decompiler.dll
Contains extension methods for the type system.
public static class TypeSystemExtensions
- Inheritance
-
TypeSystemExtensions
- Inherited Members
Methods
AsParameterizedType(ITypeDefinition)
When given a generic type definition, returns the self-parameterized type (i.e. the type of "this" within the type definition). When given a non-generic type definition, returns that definition unchanged.
public static IType AsParameterizedType(this ITypeDefinition td)
Parameters
Returns
FindModuleByReference(ICompilation, IAssemblyReference)
public static IModule FindModuleByReference(this ICompilation compilation, IAssemblyReference assemblyName)
Parameters
compilation
ICompilationassemblyName
IAssemblyReference
Returns
FindType(ICompilation, FullTypeName)
Retrieves the specified type in this compilation. Returns an UnknownType if the type cannot be found in this compilation.
public static IType FindType(this ICompilation compilation, FullTypeName fullTypeName)
Parameters
compilation
ICompilationfullTypeName
FullTypeName
Returns
Remarks
There can be multiple types with the same full name in a compilation, as a full type name is only unique per assembly. If there are multiple possible matches, this method will return just one of them. When possible, use GetTypeDefinition(TopLevelTypeName) instead to retrieve a type from a specific assembly.
FullNameIs(IMember, string, string)
public static bool FullNameIs(this IMember member, string type, string name)
Parameters
Returns
GetAllBaseTypeDefinitions(IType)
Gets all base type definitions. The output is ordered so that base types occur before derived types.
public static IEnumerable<ITypeDefinition> GetAllBaseTypeDefinitions(this IType type)
Parameters
type
IType
Returns
Remarks
This is equivalent to type.GetAllBaseTypes().Select(t => t.GetDefinition()).Where(d => d != null).Distinct().
GetAllBaseTypes(IType)
Gets all base types.
public static IEnumerable<IType> GetAllBaseTypes(this IType type)
Parameters
type
IType
Returns
Remarks
This is the reflexive and transitive closure of DirectBaseTypes. Note that this method does not return all supertypes - doing so is impossible due to contravariance (and undesirable for covariance as the list could become very large).
The output is ordered so that base types occur before derived types.
GetAllTypeDefinitions(ICompilation)
Gets all type definitions in the compilation. This may include types from referenced assemblies that are not accessible in the main assembly.
public static IEnumerable<ITypeDefinition> GetAllTypeDefinitions(this ICompilation compilation)
Parameters
compilation
ICompilation
Returns
GetAttribute(IEntity, KnownAttribute, bool)
Gets the attribute of the specified attribute type.
public static IAttribute GetAttribute(this IEntity entity, KnownAttribute attributeType, bool inherit)
Parameters
entity
IEntityThe entity on which the attributes are declared.
attributeType
KnownAttributeThe attribute type to look for.
inherit
boolSpecifies whether attributes inherited from base classes and base members (if the given
entity
in anoverride
) should be returned.
Returns
- IAttribute
Returns the attribute that was found; or
null
if none was found. If inherit is true, an from the entity itself will be returned if possible; and the base entity will only be searched if none exists.
GetAttribute(IParameter, KnownAttribute)
Gets the attribute of the specified attribute type.
public static IAttribute GetAttribute(this IParameter parameter, KnownAttribute attributeType)
Parameters
parameter
IParameterThe parameter on which the attributes are declared.
attributeType
KnownAttributeThe attribute type to look for.
Returns
- IAttribute
Returns the attribute that was found; or
null
if none was found.
GetAttributes(IEntity, bool)
Gets the attributes on the entity.
public static IEnumerable<IAttribute> GetAttributes(this IEntity entity, bool inherit)
Parameters
entity
IEntityThe entity on which the attributes are declared.
inherit
boolSpecifies whether attributes inherited from base classes and base members (if the given
entity
in anoverride
) should be returned.
Returns
- IEnumerable<IAttribute>
Returns the list of attributes that were found. If inherit is true, attributes from the entity itself are returned first; followed by attributes inherited from the base entity.
GetDeclaringTypeDefinitions(ITypeDefinition)
Returns all declaring type definitions of this type definition. The output is ordered so that inner types occur before outer types.
public static IEnumerable<ITypeDefinition> GetDeclaringTypeDefinitions(this ITypeDefinition definition)
Parameters
definition
ITypeDefinition
Returns
GetDelegateInvokeMethod(IType)
Gets the invoke method for a delegate type.
public static IMethod GetDelegateInvokeMethod(this IType type)
Parameters
type
IType
Returns
Remarks
Returns null if the type is not a delegate type; or if the invoke method could not be found.
GetElementTypeFromIEnumerable(IType, ICompilation, bool, out bool?)
public static IType GetElementTypeFromIEnumerable(this IType collectionType, ICompilation compilation, bool allowIEnumerator, out bool? isGeneric)
Parameters
collectionType
ITypecompilation
ICompilationallowIEnumerator
boolisGeneric
bool?
Returns
GetNamespaceByFullName(ICompilation, string)
public static INamespace GetNamespaceByFullName(this ICompilation compilation, string name)
Parameters
compilation
ICompilationname
string
Returns
GetNonInterfaceBaseTypes(IType)
Gets all non-interface base types.
public static IEnumerable<IType> GetNonInterfaceBaseTypes(this IType type)
Parameters
type
IType
Returns
Remarks
When type
is an interface, this method will also return base interfaces (return same output as GetAllBaseTypes()).
The output is ordered so that base types occur before derived types.
GetSymbol(ResolveResult)
public static ISymbol GetSymbol(this ResolveResult rr)
Parameters
Returns
GetTopLevelTypeDefinitions(ICompilation)
Gets all top level type definitions in the compilation. This may include types from referenced assemblies that are not accessible in the main assembly.
public static IEnumerable<ITypeDefinition> GetTopLevelTypeDefinitions(this ICompilation compilation)
Parameters
compilation
ICompilation
Returns
GetTypeDefinition(IModule, FullTypeName)
Gets the type definition for the specified unresolved type. Returns null if the unresolved type does not belong to this assembly.
public static ITypeDefinition GetTypeDefinition(this IModule module, FullTypeName fullTypeName)
Parameters
module
IModulefullTypeName
FullTypeName
Returns
GetTypeDefinition(IModule, string, string, int)
Gets the type definition for a top-level type.
public static ITypeDefinition GetTypeDefinition(this IModule module, string namespaceName, string name, int typeParameterCount = 0)
Parameters
Returns
Remarks
This method uses ordinal name comparison, not the compilation's name comparer.
HasAttribute(IEntity, KnownAttribute, bool)
Gets whether the entity has an attribute of the specified attribute type.
public static bool HasAttribute(this IEntity entity, KnownAttribute attributeType, bool inherit)
Parameters
entity
IEntityThe entity on which the attributes are declared.
attributeType
KnownAttributeThe attribute type to look for.
inherit
boolSpecifies whether attributes inherited from base classes and base members (if the given
entity
in anoverride
) should be returned.
Returns
HasAttribute(IParameter, KnownAttribute)
Gets whether the parameter has an attribute of the specified attribute type.
public static bool HasAttribute(this IParameter parameter, KnownAttribute attributeType)
Parameters
parameter
IParameterThe parameter on which the attributes are declared.
attributeType
KnownAttributeThe attribute type to look for.
Returns
HasReadonlyModifier(IMethod)
public static bool HasReadonlyModifier(this IMethod accessor)
Parameters
accessor
IMethod
Returns
IsAnyPointer(TypeKind)
public static bool IsAnyPointer(this TypeKind typeKind)
Parameters
typeKind
TypeKind
Returns
IsBuiltinAttribute(ITypeDefinition)
public static KnownAttribute IsBuiltinAttribute(this ITypeDefinition type)
Parameters
type
ITypeDefinition
Returns
IsDerivedFrom(ITypeDefinition, ITypeDefinition)
Gets whether this type definition is derived from the base type definition.
public static bool IsDerivedFrom(this ITypeDefinition type, ITypeDefinition baseType)
Parameters
type
ITypeDefinitionbaseType
ITypeDefinition
Returns
IsDerivedFrom(ITypeDefinition, KnownTypeCode)
Gets whether this type definition is derived from a given known type.
public static bool IsDerivedFrom(this ITypeDefinition type, KnownTypeCode baseType)
Parameters
type
ITypeDefinitionbaseType
KnownTypeCode
Returns
IsDirectImportOf(ITypeDefinition, IModule)
public static bool IsDirectImportOf(this ITypeDefinition type, IModule module)
Parameters
type
ITypeDefinitionmodule
IModule
Returns
IsKnownType(FullTypeName, KnownTypeCode)
public static bool IsKnownType(this FullTypeName typeName, KnownTypeCode knownType)
Parameters
typeName
FullTypeNameknownType
KnownTypeCode
Returns
IsKnownType(IType, KnownTypeCode)
Gets whether the type is the specified known type. For generic known types, this returns true for any parameterization of the type (and also for the definition itself).
public static bool IsKnownType(this IType type, KnownTypeCode knownType)
Parameters
type
ITypeknownType
KnownTypeCode
Returns
IsKnownType(TopLevelTypeName, KnownTypeCode)
public static bool IsKnownType(this TopLevelTypeName typeName, KnownTypeCode knownType)
Parameters
typeName
TopLevelTypeNameknownType
KnownTypeCode
Returns
IsOpen(IType)
Gets whether the type is an open type (contains type parameters).
public static bool IsOpen(this IType type)
Parameters
type
IType
Returns
Examples
class X<T> {
List<T> open;
X<X<T[]>> open;
X<string> closed;
int closed;
}
IsUnbound(IType)
Gets whether the type is unbound (is a generic type, but no type arguments were provided).
public static bool IsUnbound(this IType type)
Parameters
type
IType
Returns
Remarks
In "typeof(List<Dictionary<,>>)
", only the Dictionary is unbound, the List is considered
bound despite containing an unbound type.
This method returns false for partially parameterized types (Dictionary<string, >
).
IsUnmanagedType(IType, bool)
Gets whether the type is considered unmanaged.
public static bool IsUnmanagedType(this IType type, bool allowGenerics)
Parameters
Returns
Remarks
The C# 6.0 spec lists the following criteria: An unmanaged type is one of the following
- sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool
- any enum type
- any pointer type
- any user-defined struct type that is not a constructed (= generic) type and contains fields of unmanaged types only.
C# 8.0 removes the restriction that constructed (= generic) types are not considered unmanaged types.
Resolve(IList<ITypeReference>, ITypeResolveContext)
public static IReadOnlyList<IType> Resolve(this IList<ITypeReference> typeReferences, ITypeResolveContext context)
Parameters
typeReferences
IList<ITypeReference>context
ITypeResolveContext
Returns
SkipModifiers(IType)
public static IType SkipModifiers(this IType ty)
Parameters
ty
IType
Returns
UnwrapByRef(IType)
public static IType UnwrapByRef(this IType type)
Parameters
type
IType
Returns
WithoutNullability(IType)
public static IType WithoutNullability(this IType type)
Parameters
type
IType