예제 #1
0
파일: TIAHelper.cs 프로젝트: zquans/dnSpy
        static Info?GetInfo(DmdType td)
        {
            if ((object)td == null)
            {
                return(null);
            }
            if (td.IsWindowsRuntime)
            {
                return(null);
            }

            string scope = null, identifier = null;
            var    tia = td.FindCustomAttribute("System.Runtime.InteropServices.TypeIdentifierAttribute", inherit: false);

            if (tia != null)
            {
                if (tia.ConstructorArguments.Count >= 2)
                {
                    if (tia.ConstructorArguments[0].ArgumentType != td.AppDomain.System_String)
                    {
                        return(null);
                    }
                    if (tia.ConstructorArguments[1].ArgumentType != td.AppDomain.System_String)
                    {
                        return(null);
                    }
                    scope      = tia.ConstructorArguments[0].Value as string;
                    identifier = tia.ConstructorArguments[1].Value as string;
                }
            }
            else
            {
                bool isTypeLib = td.Module.Assembly.IsDefined("System.Runtime.InteropServices.ImportedFromTypeLibAttribute", inherit: false) ||
                                 td.Module.Assembly.IsDefined("System.Runtime.InteropServices.PrimaryInteropAssemblyAttribute", inherit: false);
                if (!isTypeLib)
                {
                    return(null);
                }
            }

            if (identifier == null)
            {
                DmdCustomAttributeData gca;
                if (td.IsInterface && td.IsImport)
                {
                    gca = td.FindCustomAttribute("System.Runtime.InteropServices.GuidAttribute", inherit: false);
                }
                else
                {
                    gca = td.Module.Assembly.FindCustomAttribute("System.Runtime.InteropServices.GuidAttribute", inherit: false);
                }
                if (gca == null)
                {
                    return(null);
                }
                if (gca.ConstructorArguments.Count < 1)
                {
                    return(null);
                }
                if (gca.ConstructorArguments[0].ArgumentType != td.AppDomain.System_String)
                {
                    return(null);
                }
                scope = gca.ConstructorArguments[0].Value as string;
                var ns   = td.MetadataNamespace;
                var name = td.MetadataName;
                if (string.IsNullOrEmpty(ns))
                {
                    identifier = name;
                }
                else
                {
                    identifier = ns + "." + name;
                }
            }
            return(new Info(scope, identifier));
        }