/// <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 not null) { var isCorModule = manifestModule.IsCoreLibraryModule; if (isCorModule is not null) { return(isCorModule.Value); } } } string asmName; return(asm is not 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> /// 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(); foreach (var c in UTF8String.ToSystemStringOrEmpty(name)) { if (c == ',' || c == '=') { sb.Append('\\'); } sb.Append(c); } if (version is not null) { sb.Append(", Version="); sb.Append(CreateVersionWithNoUndefinedValues(version).ToString()); } if (culture is not null) { sb.Append(", Culture="); sb.Append(UTF8String.IsNullOrEmpty(culture) ? "neutral" : culture.String); } sb.Append(", "); sb.Append(publicKey is null || publicKey is PublicKeyToken ? "PublicKeyToken=" : "PublicKey="); sb.Append(publicKey is 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()); }
static Info?GetInfo(TypeDef td) { if (td is null) { return(null); } if (td.IsWindowsRuntime) { return(null); } UTF8String scope = null, identifier = null; var tia = td.CustomAttributes.Find("System.Runtime.InteropServices.TypeIdentifierAttribute"); if (tia is not 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 asm = td.Module?.Assembly; if (asm is 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 asm = td.Module?.Assembly; if (asm is null) { return(null); } gca = asm.CustomAttributes.Find("System.Runtime.InteropServices.GuidAttribute"); } if (gca is 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)'.', Array2.Empty <byte>())); } else { identifier = new UTF8String(Concat(ns.Data, (byte)'.', name.Data)); } } return(new Info(scope, identifier)); }