2using System.Collections.Immutable;
3using System.Diagnostics;
5using System.Reflection;
6using System.Reflection.Metadata.Ecma335;
23 static bool IsTypeDefinition(
Type type)
26 return type.IsTypeDefinition;
28 return type.HasElementType ==
false && type.IsConstructedGenericType ==
false && type.IsGenericParameter ==
false;
32 readonly
Module _underlyingModule;
47 _underlyingModule = underlyingModule ??
throw new ArgumentNullException(nameof(underlyingModule));
53 internal Module UnderlyingModule => _underlyingModule;
64 throw new ArgumentNullException(nameof(type));
66 Debug.Assert(type.Module == _underlyingModule);
69 if (IsTypeDefinition(type) ==
false)
70 return GetOrCreateTypeSymbolForSpecification(type);
73 var hnd = MetadataTokens.TypeDefinitionHandle(type.MetadataToken);
74 var row = MetadataTokens.GetRowNumber(hnd);
77 if (_typesSource ==
null)
79 Interlocked.CompareExchange(
ref _typesSource, _underlyingModule.GetTypes().OrderBy(i => i.MetadataToken).ToArray(),
null);
80 _typesBaseRow = _typesSource.Length != 0 ? MetadataTokens.GetRowNumber(MetadataTokens.MethodDefinitionHandle(_typesSource[0].MetadataToken)) : 0;
85 Interlocked.CompareExchange(
ref _types,
new ReflectionTypeSymbol?[_typesSource.Length],
null);
88 var idx = row - _typesBaseRow;
90 throw new Exception();
92 Debug.Assert(idx >= 0);
93 Debug.Assert(idx < _typesSource.Length);
96 if (_types.Length < idx)
97 throw new IndexOutOfRangeException();
100 if (_types[idx] ==
null)
104 if (_types[idx] is not ReflectionTypeSymbol sym)
105 throw new InvalidOperationException();
115 ReflectionTypeSymbol GetOrCreateTypeSymbolForSpecification(
Type type)
118 throw new ArgumentNullException(nameof(type));
120 Debug.Assert(type.Module == _underlyingModule);
122 if (type.GetElementType() is { } elementType)
124 var elementTypeSymbol = GetOrCreateTypeSymbol(elementType);
128 return (ReflectionTypeSymbol)elementTypeSymbol.MakeArrayType(type.GetArrayRank());
131 return (ReflectionTypeSymbol)elementTypeSymbol.MakePointerType();
134 return (ReflectionTypeSymbol)elementTypeSymbol.MakeByRefType();
136 throw new InvalidOperationException();
139 if (type.IsGenericType)
141 var definitionType = type.GetGenericTypeDefinition();
142 var definitionTypeSymbol = GetOrCreateTypeSymbol(definitionType);
143 return definitionTypeSymbol.GetOrCreateGenericTypeSymbol(type.GetGenericArguments());
147 if (type.IsGenericParameter && type.DeclaringMethod is
null && type.DeclaringType is not
null)
149 var declaringType = GetOrCreateTypeSymbol(type.DeclaringType);
150 return declaringType.GetOrCreateGenericParameterSymbol(type);
154 if (type.IsGenericParameter && type.DeclaringMethod is not
null && type.DeclaringMethod.DeclaringType is not
null)
156 var declaringMethod = GetOrCreateTypeSymbol(type.DeclaringMethod.DeclaringType);
157 return declaringMethod.GetOrCreateGenericParameterSymbol(type);
160 throw new InvalidOperationException();
176 public string Name => _underlyingModule.Name;
184 return _underlyingModule.GetField(name) is { } f ? ResolveFieldSymbol(f) :
null;
190 return _underlyingModule.GetField(name, bindingAttr) is { } f ? ResolveFieldSymbol(f) :
null;
196 return ResolveFieldSymbols(_underlyingModule.GetFields(bindingFlags));
202 return ResolveFieldSymbols(_underlyingModule.GetFields());
208 return _underlyingModule.GetMethod(name) is { } m ? ResolveMethodSymbol(m) :
null;
214 return _underlyingModule.GetMethod(name, UnpackTypeSymbols(types)) is { } m ? ResolveMethodSymbol(m) :
null;
220 return _underlyingModule.GetMethod(name, bindingAttr,
null, callConvention, UnpackTypeSymbols(types), modifiers) is { } m ? ResolveMethodSymbol(m) :
null;
226 return ResolveMethodSymbols(_underlyingModule.GetMethods());
232 return ResolveMethodSymbols(_underlyingModule.GetMethods(bindingFlags));
238 return _underlyingModule.GetType(className) is { } t ? ResolveTypeSymbol(t) :
null;
244 return _underlyingModule.GetType(className, ignoreCase) is { } t ? ResolveTypeSymbol(t) :
null;
250 return _underlyingModule.GetType(className, throwOnError, ignoreCase) is { } t ? ResolveTypeSymbol(t) :
null;
256 return ResolveTypeSymbols(_underlyingModule.GetTypes());
262 return _underlyingModule.IsResource();
268 return _underlyingModule.ResolveField(metadataToken) is { } f ? ResolveFieldSymbol(f) :
null;
274 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
275 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
276 return _underlyingModule.ResolveField(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } f ? ResolveFieldSymbol(f) :
null;
282 return _underlyingModule.ResolveMember(metadataToken) is { } m ?
ResolveMemberSymbol(m) :
null;
288 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
289 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
290 return _underlyingModule.ResolveMember(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } m ?
ResolveMemberSymbol(m) :
null;
296 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
297 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
298 return _underlyingModule.ResolveMethod(metadataToken, _genericTypeArguments, _genericMethodArguments) is { } m ? ResolveMethodBaseSymbol(m) :
null;
304 return _underlyingModule.ResolveMethod(metadataToken) is { } m ? ResolveMethodBaseSymbol(m) :
null;
310 return _underlyingModule.ResolveSignature(metadataToken);
316 return _underlyingModule.ResolveString(metadataToken);
322 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken));
328 var _genericTypeArguments = genericTypeArguments !=
null ? UnpackTypeSymbols(genericTypeArguments) :
null;
329 var _genericMethodArguments = genericMethodArguments !=
null ? UnpackTypeSymbols(genericMethodArguments) :
null;
330 return ResolveTypeSymbol(_underlyingModule.ResolveType(metadataToken, _genericTypeArguments, _genericMethodArguments));
336 return ResolveCustomAttributes(_underlyingModule.GetCustomAttributesData());
343 return ResolveCustomAttributes(_underlyingModule.GetCustomAttributesData(), i => underlyingAttributeType.IsAssignableFrom(i.AttributeType));
System.Threading.Interlocked Interlocked
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
global::java.lang.invoke.LambdaForm.Name Name
Implementation of IModuleSymbol derived from System.Reflection.
bool IsResource()
Gets a value indicating whether the object is a resource.
IMethodSymbol? GetMethod(string name, ITypeSymbol[] types)
Returns a method having the specified name and parameter types.
IMethodSymbol[] GetMethods()
Returns the global methods defined on the module.
ITypeSymbol[] GetTypes()
Returns all the types defined within this module.
ITypeSymbol? GetType(string className, bool throwOnError, bool ignoreCase)
Returns the specified type, specifying whether to make a case-sensitive search of the module and whet...
IFieldSymbol? GetField(string name, BindingFlags bindingAttr)
Returns a field having the specified name and binding attributes.
int MetadataToken
Gets a token that identifies the module in metadata.
IFieldSymbol? ResolveField(int metadataToken)
Returns the field identified by a metadata token.
IFieldSymbol? ResolveField(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the field identified by the specified metadata token, in the context defined by the specified...
IFieldSymbol[] GetFields()
Returns the global fields defined on the module.
ITypeSymbol ResolveType(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the type identified by the specified metadata token, in the context defined by the specified ...
IMethodBaseSymbol? ResolveMethod(int metadataToken)
Returns the method or constructor identified by the specified metadata token.
IFieldSymbol? GetField(string name)
Returns a field having the specified name.
ReflectionModuleSymbol(ReflectionSymbolContext context, Module underlyingModule)
Initializes a new instance.
ImmutableArray< CustomAttributeSymbol > GetCustomAttributes()
Returns an array of all of the custom attributes defined on this member, excluding named attributes,...
IMemberSymbol? ResolveMember(int metadataToken)
Returns the type or member identified by a metadata token.
string FullyQualifiedName
Gets a string representing the fully qualified name and path to this module.
ITypeSymbol ResolveType(int metadataToken)
Returns the type identified by the specified metadata token.
string ScopeName
Gets a string representing the name of the module.
IMethodSymbol? GetMethod(string name, BindingFlags bindingAttr, CallingConventions callConvention, ITypeSymbol[] types, ParameterModifier[]? modifiers)
Returns a method having the specified name, binding information, calling convention,...
IMethodSymbol[] GetMethods(BindingFlags bindingFlags)
Returns the global methods defined on the module that match the specified binding flags.
bool IsDefined(ITypeSymbol attributeType)
Indicates whether one or more instance of attributeType is defined on this member.
IFieldSymbol[] GetFields(BindingFlags bindingFlags)
Returns the global fields defined on the module that match the specified binding flags.
Guid ModuleVersionId
Gets a universally unique identifier (UUID) that can be used to distinguish between two versions of a...
ImmutableArray< CustomAttributeSymbol > GetCustomAttributes(ITypeSymbol attributeType)
Returns an array of custom attributes defined on this member, identified by type, or an empty array i...
byte[] ResolveSignature(int metadataToken)
Returns the signature blob identified by a metadata token.
string ResolveString(int metadataToken)
Returns the string identified by the specified metadata token.
IMemberSymbol? ResolveMember(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the type or member identified by the specified metadata token, in the context defined by the ...
IMethodBaseSymbol? ResolveMethod(int metadataToken, ITypeSymbol[]? genericTypeArguments, ITypeSymbol[]? genericMethodArguments)
Returns the method or constructor identified by the specified metadata token, in the context defined ...
IMethodSymbol? GetMethod(string name)
Returns a method having the specified name.
ITypeSymbol? GetType(string className)
Returns the specified type, performing a case-sensitive search.
ITypeSymbol? GetType(string className, bool ignoreCase)
Returns the specified type, searching the module with the specified case sensitivity.
Holds references to symbols derived from System.Reflection.
ReflectionAssemblySymbol GetOrCreateAssemblySymbol(Assembly assembly)
Gets or creates a ReflectionAssemblySymbol for the specified Assembly.
Base class for managed symbols.
virtual ReflectionMemberSymbol ResolveMemberSymbol(MemberInfo member)
Resolves the symbol for the specified type.
ReflectionSymbolContext Context
Gets the associated ReflectionSymbolContext.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Discovers the attributes of a field and provides access to field metadata.
Obtains information about the attributes of a member and provides access to member metadata.
Provides information about methods and constructors.
Discovers the attributes of a method and provides access to method metadata.
Performs reflection on a module.
Represents type declarations: class types, interface types, array types, value types,...