Table of Contents

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

td ITypeDefinition

Returns

IType

FindModuleByReference(ICompilation, IAssemblyReference)

public static IModule FindModuleByReference(this ICompilation compilation, IAssemblyReference assemblyName)

Parameters

compilation ICompilation
assemblyName IAssemblyReference

Returns

IModule

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 ICompilation
fullTypeName FullTypeName

Returns

IType

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

member IMember
type string
name string

Returns

bool

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

IEnumerable<ITypeDefinition>

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

IEnumerable<IType>

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

IEnumerable<ITypeDefinition>

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 IEntity

The entity on which the attributes are declared.

attributeType KnownAttribute

The attribute type to look for.

inherit bool

Specifies whether attributes inherited from base classes and base members (if the given entity in an override) 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 IParameter

The parameter on which the attributes are declared.

attributeType KnownAttribute

The 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 IEntity

The entity on which the attributes are declared.

inherit bool

Specifies whether attributes inherited from base classes and base members (if the given entity in an override) 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

IEnumerable<ITypeDefinition>

GetDelegateInvokeMethod(IType)

Gets the invoke method for a delegate type.

public static IMethod GetDelegateInvokeMethod(this IType type)

Parameters

type IType

Returns

IMethod

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 IType
compilation ICompilation
allowIEnumerator bool
isGeneric bool?

Returns

IType

GetNamespaceByFullName(ICompilation, string)

public static INamespace GetNamespaceByFullName(this ICompilation compilation, string name)

Parameters

compilation ICompilation
name string

Returns

INamespace

GetNonInterfaceBaseTypes(IType)

Gets all non-interface base types.

public static IEnumerable<IType> GetNonInterfaceBaseTypes(this IType type)

Parameters

type IType

Returns

IEnumerable<IType>

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

rr ResolveResult

Returns

ISymbol

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

IEnumerable<ITypeDefinition>

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 IModule
fullTypeName FullTypeName

Returns

ITypeDefinition

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

module IModule
namespaceName string
name string
typeParameterCount int

Returns

ITypeDefinition

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 IEntity

The entity on which the attributes are declared.

attributeType KnownAttribute

The attribute type to look for.

inherit bool

Specifies whether attributes inherited from base classes and base members (if the given entity in an override) should be returned.

Returns

bool

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 IParameter

The parameter on which the attributes are declared.

attributeType KnownAttribute

The attribute type to look for.

Returns

bool

HasReadonlyModifier(IMethod)

public static bool HasReadonlyModifier(this IMethod accessor)

Parameters

accessor IMethod

Returns

bool

IsAnyPointer(TypeKind)

public static bool IsAnyPointer(this TypeKind typeKind)

Parameters

typeKind TypeKind

Returns

bool

IsBuiltinAttribute(ITypeDefinition)

public static KnownAttribute IsBuiltinAttribute(this ITypeDefinition type)

Parameters

type ITypeDefinition

Returns

KnownAttribute

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 ITypeDefinition
baseType ITypeDefinition

Returns

bool

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 ITypeDefinition
baseType KnownTypeCode

Returns

bool

IsDirectImportOf(ITypeDefinition, IModule)

public static bool IsDirectImportOf(this ITypeDefinition type, IModule module)

Parameters

type ITypeDefinition
module IModule

Returns

bool

IsKnownType(FullTypeName, KnownTypeCode)

public static bool IsKnownType(this FullTypeName typeName, KnownTypeCode knownType)

Parameters

typeName FullTypeName
knownType KnownTypeCode

Returns

bool

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 IType
knownType KnownTypeCode

Returns

bool

IsKnownType(TopLevelTypeName, KnownTypeCode)

public static bool IsKnownType(this TopLevelTypeName typeName, KnownTypeCode knownType)

Parameters

typeName TopLevelTypeName
knownType KnownTypeCode

Returns

bool

IsOpen(IType)

Gets whether the type is an open type (contains type parameters).

public static bool IsOpen(this IType type)

Parameters

type IType

Returns

bool

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

bool

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

type IType
allowGenerics bool

Returns

bool

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

IReadOnlyList<IType>

SkipModifiers(IType)

public static IType SkipModifiers(this IType ty)

Parameters

ty IType

Returns

IType

UnwrapByRef(IType)

public static IType UnwrapByRef(this IType type)

Parameters

type IType

Returns

IType

WithoutNullability(IType)

public static IType WithoutNullability(this IType type)

Parameters

type IType

Returns

IType