private static AssemblyInfo ReadAssemblyRef(PEFileReader peFile, MetadataToken assemblyRef) { System.Diagnostics.Contracts.Contract.Requires(peFile != null); System.Diagnostics.Contracts.Contract.Requires(assemblyRef.Table == MDTables.Tables.AssemblyRef); MDTables metaData = peFile.MetaData; BinaryReader B = metaData.B; metaData.SeekToMDToken(assemblyRef); UInt16 major = B.ReadUInt16(); UInt16 minor = B.ReadUInt16(); UInt16 build = B.ReadUInt16(); UInt16 revision = B.ReadUInt16(); Version v = new Version(major, minor, build, revision); UInt32 assemblyFlags = B.ReadUInt32(); byte[] publicKey = metaData.ReadBlob(); String simpleName = metaData.ReadString(); String culture = metaData.ReadString(); if ((culture != null) && (culture.Length == 0)) { culture = null; } return(new AssemblyInfo(v, assemblyFlags, publicKey, simpleName, culture)); }
internal TypeInfo(MetadataToken typeRefToken, MiniAssembly referencingAssembly, bool disambiguatingJunkForTypeRefOverload) { System.Diagnostics.Contracts.Contract.Requires(referencingAssembly != null); System.Diagnostics.Contracts.Contract.Requires(typeRefToken.Table == MDTables.Tables.TypeRef); _mdToken = typeRefToken; _assembly = referencingAssembly; _representation = Representation.TypeRef | Representation.Name; PEFileReader peFile = referencingAssembly.PEFileReader; MDTables MetaData = peFile.MetaData; MetaData.SeekToMDToken(typeRefToken); MetadataToken assemblyRef = MetaData.ReadMetadataToken(MDTables.Encodings.ResolutionScope); _typeName = MetaData.ReadString(); _nameSpace = MetaData.ReadString(); // Read assembly information MetaData.SeekToMDToken(assemblyRef); UInt16 major = peFile.B.ReadUInt16(); UInt16 minor = peFile.B.ReadUInt16(); UInt16 build = peFile.B.ReadUInt16(); UInt16 revision = peFile.B.ReadUInt16(); peFile.B.ReadUInt32(); // assembly flags byte[] publicKeyOrToken = MetaData.ReadBlob(); String simpleName = MetaData.ReadString(); String culture = MetaData.ReadString(); // assert so we can get the AssemblyName of mscorlib.dll FileIOPermission permission = new FileIOPermission(PermissionState.None); permission.AllLocalFiles = FileIOPermissionAccess.PathDiscovery; permission.Assert(); System.Reflection.Assembly mscorlib = typeof(Object).Assembly; if (simpleName == "mscorlib" && (culture.Length == 0) && Utils.PublicKeyMatches(mscorlib.GetName(), publicKeyOrToken)) { // Upgrade to using a Type! Type t = mscorlib.GetType(FullName, false); if (t != null) { _reflectionType = t; _representation |= Representation.ReflectionType; } } String typeRefDefiningAssemblyName = String.Format(CultureInfo.InvariantCulture, "{0}, Version={1}.{2}.{3}.{4}, Culture={5}, PublicKeyToken={6}", simpleName, major, minor, build, revision, (culture.Length == 0 ? "neutral" : culture), Utils.PublicKeyToString(publicKeyOrToken)); _assemblyQualifiedName = FullName + ", " + typeRefDefiningAssemblyName; _representation |= Representation.AssemblyQualifiedName; }
// Return the attributes on this type of the given custom attribute type internal MiniCustomAttributeInfo[] GetCustomAttributeInfos(Type caReflectedType) { List <MiniCustomAttributeInfo> result = new List <MiniCustomAttributeInfo>(); PEFileReader peFile = _assembly.PEFileReader; peFile.InitMetaData(); MDTables metaData = peFile.MetaData; uint numRows = metaData.RowsInTable(MDTables.Tables.CustomAttribute); for (uint i = 0; i < numRows; i++) { metaData.SeekToRowOfTable(MDTables.Tables.CustomAttribute, i); // Format: Parent type token, CA type token (really the constructor method), value (index into blob heap) MetadataToken targetType = metaData.ReadMetadataToken(MDTables.Encodings.HasCustomAttribute); MetadataToken caType = metaData.ReadMetadataToken(MDTables.Encodings.CustomAttributeType); byte[] caBlob = metaData.ReadBlob(); if (targetType.Equals(this._mdToken)) { //Console.WriteLine("CA - Applied to: {0} CA .ctor: {1} Value: {2}", targetType, caType, value); //Console.WriteLine("CA MD Tokens Parent: {0} Type: {1}", targetType.ToMDToken(), caType.ToMDToken()); // Ensure the custom attribute type is the type we expect metaData.SeekToMDToken(caType); String caTypeName = null, caNameSpace = null; if (caType.Table != MDTables.Tables.MemberRef) { // Custom attribute was defined in the assembly we are currently inspecting? // Ignore it. System.Diagnostics.Contracts.Contract.Assert(caType.Table == MDTables.Tables.MethodDef); continue; } MetadataToken customAttributeType = metaData.ReadMetadataToken(MDTables.Encodings.MemberRefParent); //Console.WriteLine(" MemberRef: {0} Type of MemberRef: {1}", caType.ToMDToken(), customAttributeType.ToMDToken()); metaData.SeekToMDToken(customAttributeType); MetadataToken resolutionScope = metaData.ReadMetadataToken(MDTables.Encodings.ResolutionScope); caTypeName = metaData.ReadString(); caNameSpace = metaData.ReadString(); if (caTypeName == caReflectedType.Name && caNameSpace == caReflectedType.Namespace) { MiniCustomAttributeInfo customAttributeInfo = ParseCustomAttribute(caBlob, caReflectedType); result.Add(customAttributeInfo); } } } return(result.ToArray()); }
public MiniAssembly ResolveAssemblyRef(MetadataToken token, bool throwOnError) { System.Diagnostics.Contracts.Contract.Requires(token.Table == MDTables.Tables.AssemblyRef); PEFileReader peFile = this.PEFileReader; MDTables metaData = peFile.MetaData; metaData.SeekToMDToken(token); peFile.B.ReadUInt64(); // Skip 4 parts of the version number. peFile.B.ReadUInt32(); // AssemblyFlags byte[] publicKeyOrToken = metaData.ReadBlob(); // Public key or token String assemblySimpleName = metaData.ReadString(); // simple name String cultureName = metaData.ReadString(); // assembly culture if (!String.IsNullOrEmpty(cultureName)) { throw new BadImageFormatException(Res.UnexpectedlyLoadingASatellite, FullName); } if (assemblySimpleName == "mscorlib" && (cultureName.Length == 0 || cultureName == "neutral")) { return(new MiniAssembly(typeof(Object).Assembly)); } MiniAssembly loadedAssembly = Open(assemblySimpleName, _dependencyDirs, throwOnError); if (loadedAssembly != null) { // Check whether the reference to the assembly matches what we actually loaded. // We don't respect the "throwOnError" parameter here because if someone does // violate this, they've either severely messed up their deployment, or they're // attempting a security exploit. System.Reflection.AssemblyName loadedAssemblyName = new System.Reflection.AssemblyName(loadedAssembly.FullName); if (!Utils.PublicKeyMatches(loadedAssemblyName, publicKeyOrToken)) { throw new FileLoadException(String.Format(CultureInfo.CurrentCulture, Res.AssemblyLoadRefDefMismatch, assemblySimpleName, publicKeyOrToken, loadedAssemblyName.GetPublicKeyToken())); } if (!String.IsNullOrEmpty(loadedAssemblyName.CultureInfo.Name)) { throw new FileLoadException(String.Format(CultureInfo.CurrentCulture, Res.AssemblyLoadRefDefMismatch, assemblySimpleName, String.Empty, loadedAssemblyName.CultureInfo.Name)); } } return(loadedAssembly); }
public MiniConstructorInfo[] GetConstructors(bool includePrivate) { System.Diagnostics.Contracts.Contract.Assert(HasToken /* || HasReflectionType*/, "GetConstructors needs a token (or you should uncomment the support for Reflection types)"); List <MiniConstructorInfo> ctors = new List <MiniConstructorInfo>(); /* * if (HasReflectionType) { * System.Reflection.BindingFlags visibility = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public; * if (includePrivate) * visibility |= System.Reflection.BindingFlags.NonPublic; * foreach (System.Reflection.ConstructorInfo ctor in _reflectionType.GetConstructors(visibility)) * ctors.Add(new MiniConstructorInfo(ctor)); * return ctors.ToArray(); * } */ System.Diagnostics.Contracts.Contract.Assert(_mdToken.Table == MDTables.Tables.TypeDef); PEFileReader peFile = _assembly.PEFileReader; peFile.InitMetaData(); MDTables MetaData = peFile.MetaData; MetaData.SeekToMDToken(_mdToken); System.Reflection.TypeAttributes flags = (System.Reflection.TypeAttributes)peFile.B.ReadUInt32(); System.Reflection.TypeAttributes vis = System.Reflection.TypeAttributes.VisibilityMask & flags; bool isPublic = (vis == System.Reflection.TypeAttributes.Public); // don't support NestedPublic if (!includePrivate && !isPublic) { return(new MiniConstructorInfo[0]); } MetaData.ReadStringIndex(); // typename MetaData.ReadStringIndex(); // namespace MetadataToken baseClass = MetaData.ReadMetadataToken(MDTables.Encodings.TypeDefOrRef); // Base class uint firstMemberIndex = MetaData.ReadRowIndex(MDTables.Tables.FieldDef); // Field list uint firstMethodIndex = MetaData.ReadRowIndex(MDTables.Tables.MethodDef); // Method list uint lastMethodIndex; // If this is the last entry in the TypeDef table, then all the rest of the methods in the MethodDef // table belong to this type. Otherwise, look for the methods belonging to the next type. if (_mdToken.Index == MetaData.RowsInTable(MDTables.Tables.TypeDef)) { lastMethodIndex = MetaData.RowsInTable(MDTables.Tables.MethodDef); } else { MetaData.SeekToRowOfTable(MDTables.Tables.TypeDef, _mdToken.Index); // Seek to next type (not off by 1!) peFile.B.ReadUInt32(); // Flags MetaData.ReadStringIndex(); // type name MetaData.ReadStringIndex(); // namespace MetaData.ReadMetadataToken(MDTables.Encodings.TypeDefOrRef); // Next type's base class MetaData.ReadRowIndex(MDTables.Tables.FieldDef); // field list; uint firstMethodOfNextType = MetaData.ReadRowIndex(MDTables.Tables.MethodDef); // method list lastMethodIndex = firstMethodOfNextType - 1; } // Now walk through list of methods, looking for ones w/ the name ".ctor". for (uint i = firstMethodIndex; i <= lastMethodIndex; i++) { MetadataToken method = new MetadataToken(MDTables.Tables.MethodDef, i); MetaData.SeekToMDToken(method); UInt32 rva = peFile.B.ReadUInt32(); UInt16 implFlags = peFile.B.ReadUInt16(); // MethodImplAttributes System.Reflection.MethodAttributes attrs = (System.Reflection.MethodAttributes)peFile.B.ReadUInt16(); // Flags - MethodAttributes // Visibility check if (!includePrivate && (attrs & System.Reflection.MethodAttributes.Public) == 0) { continue; } String methodName = MetaData.ReadString(); // Name // @ if (!String.Equals(methodName, ".ctor")) { continue; } byte[] sig = MetaData.ReadBlob(); try { MiniParameterInfo[] parameters = ParseSig(sig); ctors.Add(new MiniConstructorInfo(parameters)); } catch (GenericsNotImplementedException) { // may be caused by a Generic contract. The user will be warned elsewhere that generic contracts are not supported. /* * if (Warnings != null) { * lock (Warnings) { * Warnings.Add(String.Format(CultureInfo.CurrentCulture, Res.UnparsibleConstructorSignature, this.Name, e.GetType().Name, e.Message)); * } * } */ } } // for each .ctor return(ctors.ToArray()); }