private NativeFormatRuntimeNamedTypeInfo(MetadataReader reader, TypeDefinitionHandle typeDefinitionHandle, RuntimeTypeHandle typeHandle) : base(typeHandle) { _reader = reader; _typeDefinitionHandle = typeDefinitionHandle; _typeDefinition = _typeDefinitionHandle.GetTypeDefinition(reader); }
public unsafe bool TryGetMetadataForNamedType(RuntimeTypeHandle runtimeTypeHandle, out MetadataReader metadataReader, out TypeDefinitionHandle typeDefHandle) { // Iterate over all modules, starting with the module that defines the EEType foreach (IntPtr moduleHandle in ModuleList.Enumerate(RuntimeAugments.GetModuleFromTypeHandle(runtimeTypeHandle))) { MetadataTable mapTable = MetadataTable.CreateTypeMapTable(moduleHandle); foreach (var ptrEntry in mapTable) { var pCurrentEntry = (TypeMapEntry*)ptrEntry; RuntimeTypeHandle entryTypeHandle = RuntimeAugments.CreateRuntimeTypeHandle(pCurrentEntry->EEType); Handle entryMetadataHandle = pCurrentEntry->TypeDefinitionHandle.AsHandle(); if (entryTypeHandle.Equals(runtimeTypeHandle) && entryMetadataHandle.HandleType == HandleType.TypeDefinition) { metadataReader = ModuleList.Instance.GetMetadataReaderForModule(moduleHandle); typeDefHandle = entryMetadataHandle.ToTypeDefinitionHandle(metadataReader); return true; } } } metadataReader = null; typeDefHandle = default(TypeDefinitionHandle); return false; }
internal NativeFormatType(NativeFormatMetadataUnit metadataUnit, TypeDefinitionHandle handle) { _handle = handle; _metadataUnit = metadataUnit; _typeDefinition = metadataUnit.MetadataReader.GetTypeDefinition(handle); _module = metadataUnit.GetModuleFromNamespaceDefinition(_typeDefinition.NamespaceDefinition); _baseType = this; // Not yet initialized flag #if DEBUG // Initialize name eagerly in debug builds for convenience this.ToString(); #endif }
public unsafe bool TryGetNamedTypeForMetadata(MetadataReader metadataReader, TypeDefinitionHandle typeDefHandle, out RuntimeTypeHandle runtimeTypeHandle) { IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader); MetadataTable mapTable = MetadataTable.CreateTypeMapTable(moduleHandle); foreach (var ptrEntry in mapTable) { TypeMapEntry* pCurrentEntry = (TypeMapEntry*)ptrEntry; if (pCurrentEntry->TypeDefinitionHandle.AsHandle().Equals(typeDefHandle)) { runtimeTypeHandle = RuntimeAugments.CreateRuntimeTypeHandle(pCurrentEntry->EEType); return true; } } runtimeTypeHandle = default(RuntimeTypeHandle); return false; }
public unsafe void TestDefinitionHashCodes() { var wr = new Writer.MetadataWriter(); wr.ScopeDefinitions.Add(BuildSimpleTestDefinitionData()); var ms = new MemoryStream(); wr.Write(ms); fixed(byte *pBuffer = ms.ToArray()) { var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length); Reader.ScopeDefinitionHandle scopeHandle = rd.ScopeDefinitions.Single(); Reader.ScopeDefinition systemRuntimeScope = scopeHandle.GetScopeDefinition(rd); // Validate root type hash code Reader.NamespaceDefinition rootNamespace = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd); Reader.TypeDefinitionHandle moduleTypeHandle = rootNamespace.TypeDefinitions.Single(); Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("<Module>"), MetadataTypeHashingAlgorithms.ComputeHashCode(moduleTypeHandle, rd)); // Validate namespace type hashcode Reader.NamespaceDefinition systemNamespace = rootNamespace.NamespaceDefinitions.Single().GetNamespaceDefinition(rd); Reader.TypeDefinitionHandle objectTypeHandle = systemNamespace.TypeDefinitions.Single(); int objectHashCode = TypeHashingAlgorithms.ComputeNameHashCode("System.Object"); Assert.Equal(objectHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd)); // Validate nested type hashcode Reader.TypeDefinitionHandle nestedTypeHandle = objectTypeHandle.GetTypeDefinition(rd).NestedTypes.Single(); int nestedHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(objectHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested")); Assert.Equal(nestedHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd)); // Validate really nested type hashcode Reader.TypeDefinitionHandle reallyNestedTypeHandle = nestedTypeHandle.GetTypeDefinition(rd).NestedTypes.Single(); int reallyNestedHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested")); Assert.Equal(reallyNestedHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd)); } }
public unsafe bool TryGetMetadataForNamedType(RuntimeTypeHandle runtimeTypeHandle, out MetadataReader metadataReader, out TypeDefinitionHandle typeDefHandle) { // Iterate over all modules, starting with the module that defines the EEType foreach (IntPtr moduleHandle in ModuleList.Enumerate(RuntimeAugments.GetModuleFromTypeHandle(runtimeTypeHandle))) { NativeReader typeMapReader; if (TryGetNativeReaderForBlob(moduleHandle, ReflectionMapBlob.TypeMap, out typeMapReader)) { NativeParser typeMapParser = new NativeParser(typeMapReader, 0); NativeHashtable typeHashtable = new NativeHashtable(typeMapParser); ExternalReferencesTable externalReferences = default(ExternalReferencesTable); externalReferences.InitializeCommonFixupsTable(moduleHandle); var lookup = typeHashtable.Lookup(runtimeTypeHandle.GetHashCode()); NativeParser entryParser; while (!(entryParser = lookup.GetNext()).IsNull) { RuntimeTypeHandle foundType = externalReferences.GetRuntimeTypeHandleFromIndex(entryParser.GetUnsigned()); if (foundType.Equals(runtimeTypeHandle)) { Handle entryMetadataHandle = entryParser.GetUnsigned().AsHandle(); if (entryMetadataHandle.HandleType == HandleType.TypeDefinition) { metadataReader = ModuleList.Instance.GetMetadataReaderForModule(moduleHandle); typeDefHandle = entryMetadataHandle.ToTypeDefinitionHandle(metadataReader); return true; } } } } } metadataReader = null; typeDefHandle = default(TypeDefinitionHandle); return false; }
public abstract bool TryGetNamedTypeForMetadata(MetadataReader metadataReader, TypeDefinitionHandle typeDefHandle, out RuntimeTypeHandle runtimeTypeHandle);
//============================================================================================== // Reflection Mapping Tables //============================================================================================== public abstract bool TryGetMetadataForNamedType(RuntimeTypeHandle runtimeTypeHandle, out MetadataReader metadataReader, out TypeDefinitionHandle typeDefHandle);
} // IsNull internal bool IsNull(TypeDefinitionHandle handle) { return (handle._value & 0x00FFFFFF) == 0; } // IsNull
} // ToHandle internal Handle ToHandle(TypeDefinitionHandle handle) { return new Handle(handle._value); } // ToHandle
} // GetScopeReference public TypeDefinition GetTypeDefinition(TypeDefinitionHandle handle) { var record = new TypeDefinition() { _reader = this, _handle = handle }; var offset = (uint)handle.Offset; offset = _streamReader.Read(offset, out record._flags); offset = _streamReader.Read(offset, out record._baseType); offset = _streamReader.Read(offset, out record._namespaceDefinition); offset = _streamReader.Read(offset, out record._name); offset = _streamReader.Read(offset, out record._size); offset = _streamReader.Read(offset, out record._packingSize); offset = _streamReader.Read(offset, out record._enclosingType); offset = _streamReader.Read(offset, out record._nestedTypes); offset = _streamReader.Read(offset, out record._methods); offset = _streamReader.Read(offset, out record._fields); offset = _streamReader.Read(offset, out record._properties); offset = _streamReader.Read(offset, out record._events); offset = _streamReader.Read(offset, out record._genericParameters); offset = _streamReader.Read(offset, out record._interfaces); offset = _streamReader.Read(offset, out record._methodImpls); offset = _streamReader.Read(offset, out record._customAttributes); return record; } // GetTypeDefinition
} // Read public static uint Read(this NativeReader reader, uint offset, out TypeDefinitionHandle handle) { uint value; offset = reader.DecodeUnsigned(offset, out value); handle = new TypeDefinitionHandle((int)value); handle._Validate(); return offset; } // Read
public unsafe bool TryGetOrCreateNamedTypeForMetadata( MetadataReader metadataReader, TypeDefinitionHandle typeDefHandle, out RuntimeTypeHandle runtimeTypeHandle) { if (TryGetNamedTypeForMetadata(metadataReader, typeDefHandle, out runtimeTypeHandle)) { return true; } #if SUPPORTS_NATIVE_METADATA_TYPE_LOADING IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader); IntPtr runtimeTypeHandleAsIntPtr; if (TryResolveSingleMetadataFixup( moduleHandle, typeDefHandle.ToHandle(metadataReader).ToInt(), MetadataFixupKind.TypeHandle, out runtimeTypeHandleAsIntPtr)) { runtimeTypeHandle = *(RuntimeTypeHandle*)&runtimeTypeHandleAsIntPtr; return true; } #endif return false; }
public abstract IEnumerable<CustomAttributeData> GetPseudoCustomAttributes(MetadataReader reader, PropertyHandle propertyHandle, TypeDefinitionHandle declaringTypeHandle);
} // Read public static uint Read(this NativeReader reader, uint offset, out TypeDefinitionHandle[] values) { uint count; offset = reader.DecodeUnsigned(offset, out count); #if !NETFX_45 if (count == 0) { values = Array.Empty<TypeDefinitionHandle>(); } else #endif { values = new TypeDefinitionHandle[count]; for (uint i = 0; i < count; ++i) { TypeDefinitionHandle tmp; offset = reader.Read(offset, out tmp); values[i] = tmp; } } return offset; } // Read
public abstract IEnumerable<CustomAttributeData> GetPsuedoCustomAttributes(MetadataReader reader, TypeDefinitionHandle typeDefinitionHandle);
public abstract IEnumerable<CustomAttributeData> GetPsuedoCustomAttributes(MetadataReader reader, EventHandle eventHandle, TypeDefinitionHandle declaringTypeHandle);
public unsafe bool TryGetNamedTypeForMetadata(MetadataReader metadataReader, TypeDefinitionHandle typeDefHandle, out RuntimeTypeHandle runtimeTypeHandle) { int hashCode = typeDefHandle.ComputeHashCode(metadataReader); IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader); NativeReader typeMapReader; if (TryGetNativeReaderForBlob(moduleHandle, ReflectionMapBlob.TypeMap, out typeMapReader)) { NativeParser typeMapParser = new NativeParser(typeMapReader, 0); NativeHashtable typeHashtable = new NativeHashtable(typeMapParser); ExternalReferencesTable externalReferences = default(ExternalReferencesTable); externalReferences.InitializeCommonFixupsTable(moduleHandle); var lookup = typeHashtable.Lookup(hashCode); NativeParser entryParser; while (!(entryParser = lookup.GetNext()).IsNull) { var foundTypeIndex = entryParser.GetUnsigned(); if (entryParser.GetUnsigned().AsHandle().Equals(typeDefHandle)) { runtimeTypeHandle = externalReferences.GetRuntimeTypeHandleFromIndex(foundTypeIndex); return true; } } } runtimeTypeHandle = default(RuntimeTypeHandle); return false; }
} // Read public static uint Read(this NativeReader reader, uint offset, out TypeDefinitionHandle[] values) { uint count; offset = reader.DecodeUnsigned(offset, out count); if (count == 0) { values = s_emptyTypeDefinitionHandleArray; } else { values = new TypeDefinitionHandle[count]; for (uint i = 0; i < count; ++i) { TypeDefinitionHandle tmp; offset = reader.Read(offset, out tmp); values[i] = tmp; } } return offset; } // Read
} // Equals public bool Equals(TypeDefinitionHandle handle) { return _value == handle._value; } // Equals