/// <summary> /// Returns an assembly name string /// </summary> /// <param name="name">Simple assembly name</param> /// <param name="version">Version or <c>null</c></param> /// <param name="culture">Culture or <c>null</c></param> /// <param name="publicKey">Public key / public key token or <c>null</c></param> /// <param name="attributes">Assembly attributes</param> /// <returns>An assembly name string</returns> internal static string GetAssemblyNameString(UTF8String name, Version version, UTF8String culture, PublicKeyBase publicKey, AssemblyAttributes attributes) { var sb = new StringBuilder(); sb.Append(UTF8String.ToSystemStringOrEmpty(name)); if (version != null) { sb.Append(", Version="); sb.Append(CreateVersionWithNoUndefinedValues(version).ToString()); } if ((object)culture != null) { sb.Append(", Culture="); sb.Append(UTF8String.IsNullOrEmpty(culture) ? "neutral" : culture.String); } sb.Append(", "); sb.Append(publicKey == null || publicKey is PublicKeyToken ? "PublicKeyToken=" : "PublicKey="); sb.Append(publicKey == null ? "null" : publicKey.ToString()); if ((attributes & AssemblyAttributes.Retargetable) != 0) { sb.Append(", Retargetable=Yes"); } if ((attributes & AssemblyAttributes.ContentType_Mask) == AssemblyAttributes.ContentType_WindowsRuntime) { sb.Append(", ContentType=WindowsRuntime"); } return(sb.ToString()); }
/// <summary> /// Checks whether <paramref name="asm"/> appears to be the core library (eg. /// mscorlib, System.Runtime or corefx). /// /// If <paramref name="asm"/> is a reference to a private corlib (eg. System.Private.CoreLib), /// this method returns false unless <paramref name="asm"/> is an <see cref="AssemblyDef"/> /// whose manifest (first) module defines <c>System.Object</c>. This check is performed in /// the constructor and the result can be found in <see cref="ModuleDef.IsCoreLibraryModule"/>. /// /// Note that this method also returns true if it appears to be a 'public' corlib, /// eg. mscorlib, etc, even if it internally possibly references a private corlib. /// </summary> /// <param name="asm">The assembly</param> public static bool IsCorLib(this IAssembly asm) { if (asm is AssemblyDef asmDef) { var manifestModule = asmDef.ManifestModule; if (!(manifestModule is null)) { var isCorModule = manifestModule.IsCoreLibraryModule; if (!(isCorModule is null)) { return(isCorModule.Value); } } } string asmName; return(!(asm is null) && UTF8String.IsNullOrEmpty(asm.Culture) && ((asmName = UTF8String.ToSystemStringOrEmpty(asm.Name)).Equals("mscorlib", StringComparison.OrdinalIgnoreCase) || asmName.Equals("System.Runtime", StringComparison.OrdinalIgnoreCase) || // This name could change but since CoreCLR is used a lot, it's worth supporting asmName.Equals("System.Private.CoreLib", StringComparison.OrdinalIgnoreCase) || asmName.Equals("netstandard", StringComparison.OrdinalIgnoreCase) || asmName.Equals("corefx", StringComparison.OrdinalIgnoreCase))); }
/// <summary> /// Checks whether <paramref name="asm"/> appears to be the core library (eg. /// mscorlib, System.Runtime or corefx) /// </summary> /// <param name="asm">The assembly</param> public static bool IsCorLib(this IAssembly asm) { string asmName; return asm != null && UTF8String.IsNullOrEmpty(asm.Culture) && ((asmName = UTF8String.ToSystemStringOrEmpty(asm.Name)).Equals("mscorlib", StringComparison.OrdinalIgnoreCase) || asmName.Equals("System.Runtime", StringComparison.OrdinalIgnoreCase) || asmName.Equals("corefx", StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Constructor /// </summary> /// <param name="assembly">Assembly</param> public AssemblyRefUser(IAssembly assembly) { if (assembly == null) { throw new ArgumentNullException("asmName"); } this.version = assembly.Version ?? new Version(0, 0, 0, 0); this.publicKeyOrToken = assembly.PublicKeyOrToken; this.name = UTF8String.IsNullOrEmpty(assembly.Name) ? UTF8String.Empty : assembly.Name; this.culture = assembly.Culture; this.attributes = (int)(publicKeyOrToken is PublicKey ? AssemblyAttributes.PublicKey : AssemblyAttributes.None); }
/// <summary> /// Constructor /// </summary> /// <param name="asmName">Assembly name info</param> /// <exception cref="ArgumentNullException">If <paramref name="asmName"/> is <c>null</c></exception> public AssemblyRefUser(AssemblyNameInfo asmName) { if (asmName == null) { throw new ArgumentNullException("asmName"); } this.version = asmName.Version ?? new Version(0, 0, 0, 0); this.publicKeyOrToken = asmName.PublicKeyOrToken; this.name = UTF8String.IsNullOrEmpty(asmName.Name) ? UTF8String.Empty : asmName.Name; this.locale = asmName.Locale; this.flags = publicKeyOrToken is PublicKey ? AssemblyAttributes.PublicKey : AssemblyAttributes.None; }
/// <summary> /// Constructor /// </summary> /// <param name="asm">The assembly</param> public AssemblyNameInfo(IAssembly asm) { if (asm == null) { return; } var asmDef = asm as AssemblyDef; this.hashAlgId = asmDef == null ? 0 : asmDef.HashAlgorithm; this.version = asm.Version ?? new Version(0, 0, 0, 0); this.flags = asm.Attributes; this.publicKeyOrToken = asm.PublicKeyOrToken; this.name = UTF8String.IsNullOrEmpty(asm.Name) ? UTF8String.Empty : asm.Name; this.culture = UTF8String.IsNullOrEmpty(asm.Culture) ? UTF8String.Empty : asm.Culture; }
/// <summary> /// Returns an assembly name string /// </summary> /// <param name="name">Simple assembly name</param> /// <param name="version">Version or <c>null</c></param> /// <param name="culture">Culture or <c>null</c></param> /// <param name="publicKey">Public key / public key token or <c>null</c></param> /// <returns>An assembly name string</returns> internal static string GetAssemblyNameString(UTF8String name, Version version, UTF8String culture, PublicKeyBase publicKey) { var sb = new StringBuilder(); sb.Append(UTF8String.ToSystemStringOrEmpty(name)); if (version != null) { sb.Append(", Version="); sb.Append(CreateVersionWithNoUndefinedValues(version).ToString()); } if ((object)culture != null) { sb.Append(", Culture="); sb.Append(UTF8String.IsNullOrEmpty(culture) ? "neutral" : culture.String); } sb.Append(", "); sb.Append(publicKey == null || publicKey is PublicKeyToken ? "PublicKeyToken=" : "PublicKey="); sb.Append(publicKey == null ? "null" : publicKey.ToString()); return(sb.ToString()); }
private static Info?GetInfo(TypeDef td) { if (td == null) { return(null); } if (td.IsWindowsRuntime) { return(null); } UTF8String scope = null, identifier = null; var tia = td.CustomAttributes.Find("System.Runtime.InteropServices.TypeIdentifierAttribute"); if (tia != null) { if (tia.ConstructorArguments.Count >= 2) { if (tia.ConstructorArguments[0].Type.GetElementType() != ElementType.String) { return(null); } if (tia.ConstructorArguments[1].Type.GetElementType() != ElementType.String) { return(null); } scope = tia.ConstructorArguments[0].Value as UTF8String ?? tia.ConstructorArguments[0].Value as string; identifier = tia.ConstructorArguments[1].Value as UTF8String ?? tia.ConstructorArguments[1].Value as string; } } else { var mod = td.Module; var asm = mod == null ? null : mod.Assembly; if (asm == null) { return(null); } bool isTypeLib = asm.CustomAttributes.IsDefined("System.Runtime.InteropServices.ImportedFromTypeLibAttribute") || asm.CustomAttributes.IsDefined("System.Runtime.InteropServices.PrimaryInteropAssemblyAttribute"); if (!isTypeLib) { return(null); } } if (UTF8String.IsNull(identifier)) { CustomAttribute gca; if (td.IsInterface && td.IsImport) { gca = td.CustomAttributes.Find("System.Runtime.InteropServices.GuidAttribute"); } else { var mod = td.Module; var asm = mod == null ? null : mod.Assembly; if (asm == null) { return(null); } gca = asm.CustomAttributes.Find("System.Runtime.InteropServices.GuidAttribute"); } if (gca == null) { return(null); } if (gca.ConstructorArguments.Count < 1) { return(null); } if (gca.ConstructorArguments[0].Type.GetElementType() != ElementType.String) { return(null); } scope = gca.ConstructorArguments[0].Value as UTF8String ?? gca.ConstructorArguments[0].Value as string; var ns = td.Namespace; var name = td.Name; if (UTF8String.IsNullOrEmpty(ns)) { identifier = name; } else if (UTF8String.IsNullOrEmpty(name)) { identifier = new UTF8String(Concat(ns.Data, (byte)'.', empty)); } else { identifier = new UTF8String(Concat(ns.Data, (byte)'.', name.Data)); } } return(new Info(scope, identifier)); }