public static INamedTypeDefinition FindType <T>(IMetadataHost host) { var asm = (IAssembly)host.LoadUnitFrom(typeof(T).Assembly.Location); var type = UnitHelper.FindType( host.NameTable, asm, typeof(T).FullName); return(type); }
/// <summary> /// Constructs module identity for the given module /// </summary> /// <param name="module">Module for which the identity is desired.</param> /// <returns>The module identity corresponding to the passed module.</returns> public static ModuleIdentity GetModuleIdentity( IModule module ) { if (module.ContainingAssembly != null) { return(new ModuleIdentity(module.Name, module.Location, UnitHelper.GetAssemblyIdentity(module.ContainingAssembly))); } else { return(new ModuleIdentity(module.Name, module.Location)); } }
/// <summary> /// Constructs module identity for the given module /// </summary> /// <param name="module">Module for which the identity is desired.</param> /// <returns>The module identity corresponding to the passed module.</returns> public static ModuleIdentity GetModuleIdentity(IModule module) { Contract.Requires(module != null); Contract.Ensures(Contract.Result <ModuleIdentity>() != null); if (module.ContainingAssembly != null) { return(new ModuleIdentity(module.Name, module.Location, UnitHelper.GetAssemblyIdentity(module.ContainingAssembly))); } else { return(new ModuleIdentity(module.Name, module.Location)); } }
/// <summary> /// Returns a unit namespace definition, nested in the given unitNamespace if possible, /// otherwise nested in the given unit if possible, otherwise nested in the given host if possible, otherwise it returns Dummy.UnitNamespace. /// If unitNamespaceReference is a root namespace, the result is equal to the given unitNamespace if not null, /// otherwise the result is the root namespace of the given unit if not null, /// otherwise the result is root namespace of unitNamespaceReference.Unit, if that can be resolved via the given host, /// otherwise the result is Dummy.UnitNamespace. /// </summary> public static IUnitNamespace Resolve(IUnitNamespaceReference unitNamespaceReference, IMetadataHost host, IUnit unit = null, IUnitNamespace unitNamespace = null) { Contract.Requires(unitNamespaceReference != null); Contract.Requires(host != null); Contract.Requires(unit != null || unitNamespace == null); Contract.Ensures(Contract.Result <IUnitNamespace>() != null); var rootNsRef = unitNamespaceReference as IRootUnitNamespaceReference; if (rootNsRef != null) { if (unitNamespace != null) { return(unitNamespace); } if (unit != null) { return(unit.UnitNamespaceRoot); } unit = UnitHelper.Resolve(unitNamespaceReference.Unit, host); if (unit is Dummy) { return(Dummy.UnitNamespace); } return(unit.UnitNamespaceRoot); } var nestedNsRef = unitNamespaceReference as INestedUnitNamespaceReference; if (nestedNsRef == null) { return(Dummy.UnitNamespace); } var containingNsDef = UnitHelper.Resolve(nestedNsRef.ContainingUnitNamespace, host, unit, unitNamespace); if (containingNsDef is Dummy) { return(Dummy.UnitNamespace); } foreach (var nsMem in containingNsDef.GetMembersNamed(nestedNsRef.Name, ignoreCase: false)) { var neNsDef = nsMem as INestedUnitNamespace; if (neNsDef != null) { return(neNsDef); } } return(Dummy.UnitNamespace); }
/// <summary> /// Returns true if the given two unit references are to be considered equivalent. /// </summary> public static bool UnitsAreEquivalent(IUnitReference unit1, IUnitReference unit2) { if (unit1 == null || unit2 == null) { return(false); } if (unit1 == unit2) { return(true); } if (UnitHelper.AssembliesAreEquivalent(unit1 as IAssemblyReference, unit2 as IAssemblyReference)) { return(true); } if (UnitHelper.ModulesAreEquivalent(unit1 as IModuleReference, unit2 as IModuleReference)) { return(true); } return(false); }
/// <summary> /// Computes the AssemblyIdentifier of the PE File. This requires that peFile is an assembly. /// </summary> /// <param name="peFileReader"></param> /// <returns></returns> internal AssemblyIdentity GetAssemblyIdentifier(PEFileReader peFileReader) //^ requires peFileReader.ReaderState >= ReaderState.Metadata && peFileReader.IsAssembly; //^ ensures (result.Location != null && result.Location.Length != 0); { AssemblyRow assemblyRow = peFileReader.AssemblyTable[1]; IName assemblyName = this.metadataReaderHost.NameTable.GetNameFor(peFileReader.StringStream[assemblyRow.Name]); string cultureName = peFileReader.StringStream[assemblyRow.Culture]; Version version = new Version(assemblyRow.MajorVersion, assemblyRow.MinorVersion, assemblyRow.BuildNumber, assemblyRow.RevisionNumber); byte[] publicKeyArray = TypeCache.EmptyByteArray; byte[] publicKeyTokenArray = TypeCache.EmptyByteArray; if (assemblyRow.PublicKey != 0) { publicKeyArray = peFileReader.BlobStream[assemblyRow.PublicKey]; if (publicKeyArray.Length > 0) { publicKeyTokenArray = UnitHelper.ComputePublicKeyToken(publicKeyArray); } } return(new AssemblyIdentity(assemblyName, cultureName, version, publicKeyTokenArray, peFileReader.BinaryDocumentMemoryBlock.BinaryDocument.Location)); }
/// <summary> /// Returns true if the given two unit references are to be considered equivalent as containers. /// </summary> public static bool UnitsAreContainmentEquivalent(IUnitReference unit1, IUnitReference unit2) { if (unit1 == null || unit2 == null) { return(false); } if (unit1 == unit2) { return(true); } IModuleReference /*?*/ moduleRef1 = unit1 as IModuleReference; IModuleReference /*?*/ moduleRef2 = unit2 as IModuleReference; if (moduleRef1 != null && moduleRef2 != null) { if (UnitHelper.AssembliesAreEquivalent(moduleRef1.ContainingAssembly, moduleRef2.ContainingAssembly)) { return(true); } } return(false); }
/// <summary> /// Returns true if the given two module references are to be considered equivalent. /// </summary> public static bool ModulesAreEquivalent(IModuleReference /*?*/ module1, IModuleReference /*?*/ module2) { if (module1 == null || module2 == null) { return(false); } if (module1 == module2) { return(true); } if (module1.ContainingAssembly != null) { if (module2.ContainingAssembly == null) { return(false); } if (!UnitHelper.AssembliesAreEquivalent(module1.ContainingAssembly, module2.ContainingAssembly)) { return(false); } } return(module1.Name.UniqueKeyIgnoringCase == module2.Name.UniqueKeyIgnoringCase); }
/// <summary> /// Returns true if the given two unit namespaces are to be considered equivalent as containers. /// </summary> public static bool UnitNamespacesAreEquivalent(IUnitNamespaceReference /*?*/ unitNamespace1, IUnitNamespaceReference /*?*/ unitNamespace2) { if (unitNamespace1 == null || unitNamespace2 == null) { return(false); } if (unitNamespace1 == unitNamespace2) { return(true); } INestedUnitNamespaceReference /*?*/ nstUnitNamespace1 = unitNamespace1 as INestedUnitNamespaceReference; INestedUnitNamespaceReference /*?*/ nstUnitNamespace2 = unitNamespace2 as INestedUnitNamespaceReference; if (nstUnitNamespace1 != null && nstUnitNamespace2 != null) { return(nstUnitNamespace1.Name.UniqueKey == nstUnitNamespace2.Name.UniqueKey && UnitHelper.UnitNamespacesAreEquivalent(nstUnitNamespace1.ContainingUnitNamespace, nstUnitNamespace2.ContainingUnitNamespace)); } if (nstUnitNamespace1 != null || nstUnitNamespace2 != null) { return(false); } return(UnitHelper.UnitsAreContainmentEquivalent(unitNamespace1.Unit, unitNamespace2.Unit)); }
/// <summary> /// Allocates an object that identifies a .NET assembly, using the IAssembly object /// </summary> /// <param name="assembly"></param> public static AssemblyIdentity GetAssemblyIdentity( IAssembly assembly ) { byte[] pKey = new List <byte>(assembly.PublicKey).ToArray(); if (pKey.Length != 0) { return(new AssemblyIdentity(assembly.Name, assembly.Culture, assembly.Version, UnitHelper.ComputePublicKeyToken(pKey), assembly.Location)); } else { return(new AssemblyIdentity(assembly.Name, assembly.Culture, assembly.Version, new byte[0], assembly.Location)); } }
/// <summary> /// Allocates an object that identifies a .NET assembly, using the IAssembly object /// </summary> /// <param name="assembly"></param> public static AssemblyIdentity GetAssemblyIdentity(IAssembly assembly) { Contract.Requires(assembly != null); Contract.Ensures(Contract.Result <AssemblyIdentity>() != null); byte[] pKey = new List <byte>(assembly.PublicKey).ToArray(); if (pKey.Length != 0) { return(new AssemblyIdentity(assembly.Name, assembly.Culture, assembly.Version, UnitHelper.ComputePublicKeyToken(pKey), assembly.Location)); } else { return(new AssemblyIdentity(assembly.Name, assembly.Culture, assembly.Version, new byte[0], assembly.Location)); } }