Inheritance: ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable
コード例 #1
0
        public static FrameworkName GetFrameworkName(Assembly assembly, TargetFrameworkAttribute target)
        {
            if (target == null) {
                Match match = Regex.Match(assembly.ImageRuntimeVersion, @"v(?<Version>\d+(\.\d+)+)");
                Version version = Version.Parse(match.Groups["Version"].Value);
                return new FrameworkName(".NETFramework", version);

            } else {
                return new FrameworkName(target.FrameworkName);

            }
        }
コード例 #2
0
        internal static IEnumerable<KeyValuePair<string, object>> EnumerateMetadata(Assembly assembly)
        {
            ComponentName cname = ComponentName.FromAssemblyName(assembly.GetName());

            var data = assembly.GetCustomAttributesData();
            TargetFrameworkAttribute targetAttr = null;
            AssemblyConfigurationAttribute configAttr = null;
            AssemblyFileVersionAttribute fileAttr = null;

            foreach (var m in data) {
                var decl = m.Constructor.DeclaringType;
                if (decl == typeof(TargetFrameworkAttribute))
                    targetAttr = new TargetFrameworkAttribute((string) m.ConstructorArguments[0].Value);

                else if (decl == typeof(AssemblyConfigurationAttribute))
                    configAttr = new AssemblyConfigurationAttribute((string) m.ConstructorArguments[0].Value);

                else if (decl == typeof(AssemblyFileVersionAttribute))
                    fileAttr = new AssemblyFileVersionAttribute((string) m.ConstructorArguments[0].Value);
            }

            FrameworkName target = GetFrameworkName(assembly, targetAttr);
            string config = GetConfiguration(configAttr);
            string platform = GetPlatform(assembly);
            Version version = assembly.GetName().Version;

            ICustomAttributeProvider attributes;

            if (assembly.ReflectionOnly)
                attributes = new ReflectOnlyAssemblyAttributeProvider(assembly);
            else
                attributes = assembly;

            Uri myBase = null;
            Uri url = null;
            Uri license = null;

            var baseAttribute = CustomAttributeProvider.GetCustomAttribute<BaseAttribute>(attributes, false);
            if (baseAttribute != null) {
                myBase = baseAttribute.Source;
            }

            var licenseAttribute = CustomAttributeProvider.GetCustomAttribute<LicenseAttribute>(attributes, false);
            if (licenseAttribute != null) {
                license = licenseAttribute.Uri;
            }

            var ua = CustomAttributeProvider.GetCustomAttribute<UrlAttribute>(attributes, false);
            if (ua != null) {
                url = ua.Url;
            }

            return Properties.FromValue(new {
                                            name = cname.Name,
                                            configuration = config,
                                            assemblyName = cname,
                                            platform = platform,
                                            targetFramework = target,
                                            @base = myBase,
                                            license = license,
                                            url = url,
                                            version = version, });
        }
コード例 #3
0
        private static string GetPlatform(Assembly assembly)
        {
            // TODO Might be other ways to detect this using informational version
            Module module = assembly.GetModules(false).FirstOrDefault();
            if (module == null)
                return "anycpu";

            PortableExecutableKinds pe;
            ImageFileMachine machine;
            module.GetPEKind(out pe, out machine);

            if (pe.HasFlag(PortableExecutableKinds.ILOnly))
                return "anycpu";
            if (pe.HasFlag(PortableExecutableKinds.Unmanaged32Bit))
                return "x86";

            switch (machine) {
                case ImageFileMachine.I386:
                    return "x86";
                case ImageFileMachine.IA64:
                case ImageFileMachine.AMD64:
                    return "x64";

                default:
                    return machine.ToString().ToLowerInvariant();
            }
        }