Esempio n. 1
0
        /// <summary>
        /// Checks whether this assembly is a friend assembly of <paramref name="targetAsm"/>
        /// </summary>
        /// <param name="targetAsm">Target assembly</param>
        public bool IsFriendAssemblyOf(AssemblyDef targetAsm)
        {
            if (targetAsm == null)
            {
                return(false);
            }
            if (this == targetAsm)
            {
                return(true);
            }

            // Both must be unsigned or both must be signed according to the
            // InternalsVisibleToAttribute documentation.
            if (PublicKeyBase.IsNullOrEmpty2(publicKey) != PublicKeyBase.IsNullOrEmpty2(targetAsm.PublicKey))
            {
                return(false);
            }

            foreach (var ca in targetAsm.CustomAttributes.FindAll("System.Runtime.CompilerServices.InternalsVisibleToAttribute"))
            {
                if (ca.ConstructorArguments.Count != 1)
                {
                    continue;
                }
                var arg = ca.ConstructorArguments.Get(0, default(CAArgument));
                if (arg.Type.GetElementType() != ElementType.String)
                {
                    continue;
                }
                var asmName = arg.Value as UTF8String;
                if (UTF8String.IsNull(asmName))
                {
                    continue;
                }

                var asmInfo = new AssemblyNameInfo(asmName);
                if (asmInfo.Name != name)
                {
                    continue;
                }
                if (!PublicKeyBase.IsNullOrEmpty2(publicKey))
                {
                    if (!PublicKey.Equals(asmInfo.PublicKeyOrToken as PublicKey))
                    {
                        continue;
                    }
                }
                else if (!PublicKeyBase.IsNullOrEmpty2(asmInfo.PublicKeyOrToken))
                {
                    continue;
                }

                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        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));
        }