示例#1
0
 public TypeDefinitionProjection(TypeDefinition type, TypeDefinitionTreatment treatment, Collection <MethodDefinition> redirectedMethods, Collection <KeyValuePair <InterfaceImplementation, InterfaceImplementation> > redirectedInterfaces)
 {
     Attributes           = type.Attributes;
     Name                 = type.Name;
     Treatment            = treatment;
     RedirectedMethods    = redirectedMethods;
     RedirectedInterfaces = redirectedInterfaces;
 }
        public static void Project(TypeDefinition type)
        {
            TypeDefinitionTreatment       treatment         = TypeDefinitionTreatment.None;
            MetadataKind                  metadata_kind     = type.Module.MetadataKind;
            Collection <MethodDefinition> redirectedMethods = null;
            Collection <KeyValuePair <InterfaceImplementation, InterfaceImplementation> > redirectedInterfaces = null;

            if (type.IsWindowsRuntime)
            {
                if (metadata_kind == MetadataKind.WindowsMetadata)
                {
                    treatment = GetWellKnownTypeDefinitionTreatment(type);
                    if (treatment != TypeDefinitionTreatment.None)
                    {
                        ApplyProjection(type, new TypeDefinitionProjection(type, treatment, redirectedMethods, redirectedInterfaces));
                        return;
                    }

                    TypeReference base_type = type.BaseType;
                    if (base_type != null && IsAttribute(base_type))
                    {
                        treatment = TypeDefinitionTreatment.NormalAttribute;
                    }
                    else
                    {
                        treatment = GenerateRedirectionInformation(type, out redirectedMethods, out redirectedInterfaces);
                    }
                }
                else if (metadata_kind == MetadataKind.ManagedWindowsMetadata && NeedsWindowsRuntimePrefix(type))
                {
                    treatment = TypeDefinitionTreatment.PrefixWindowsRuntimeName;
                }

                if (treatment == TypeDefinitionTreatment.PrefixWindowsRuntimeName || treatment == TypeDefinitionTreatment.NormalType)
                {
                    if (!type.IsInterface && HasAttribute(type, "Windows.UI.Xaml", "TreatAsAbstractComposableClassAttribute"))
                    {
                        treatment |= TypeDefinitionTreatment.Abstract;
                    }
                }
            }
            else if (metadata_kind == MetadataKind.ManagedWindowsMetadata && IsClrImplementationType(type))
            {
                treatment = TypeDefinitionTreatment.UnmangleWindowsRuntimeName;
            }

            if (treatment != TypeDefinitionTreatment.None)
            {
                ApplyProjection(type, new TypeDefinitionProjection(type, treatment, redirectedMethods, redirectedInterfaces));
            }
        }
        private static TypeDefinitionTreatment GetWellKnownTypeDefinitionTreatment(TypeDefinition type)
        {
            if (!Projections.TryGetValue(type.Name, out ProjectionInfo projectionInfo))
            {
                return(TypeDefinitionTreatment.None);
            }
            TypeDefinitionTreatment typeDefinitionTreatment = projectionInfo.Attribute ? TypeDefinitionTreatment.RedirectToClrAttribute : TypeDefinitionTreatment.RedirectToClrType;

            if (type.Namespace == projectionInfo.ClrNamespace)
            {
                return(typeDefinitionTreatment);
            }
            if (type.Namespace == projectionInfo.WinRTNamespace)
            {
                return(typeDefinitionTreatment | TypeDefinitionTreatment.Internal);
            }
            return(TypeDefinitionTreatment.None);
        }
        public static void ApplyProjection(TypeDefinition type, TypeDefinitionProjection projection)
        {
            if (projection != null)
            {
                TypeDefinitionTreatment treatment = projection.Treatment;
                switch (treatment & TypeDefinitionTreatment.KindMask)
                {
                case TypeDefinitionTreatment.NormalType:
                    type.Attributes |= (TypeAttributes.Import | TypeAttributes.WindowsRuntime);
                    break;

                case TypeDefinitionTreatment.NormalAttribute:
                    type.Attributes |= (TypeAttributes.Sealed | TypeAttributes.WindowsRuntime);
                    break;

                case TypeDefinitionTreatment.UnmangleWindowsRuntimeName:
                    type.Attributes = (TypeAttributes)(((int)type.Attributes & -1025) | 1);
                    type.Name       = type.Name.Substring("<CLR>".Length);
                    break;

                case TypeDefinitionTreatment.PrefixWindowsRuntimeName:
                    type.Attributes = (TypeAttributes)(((int)type.Attributes & -2) | 0x1000);
                    type.Name       = "<WinRT>" + type.Name;
                    break;

                case TypeDefinitionTreatment.RedirectToClrType:
                    type.Attributes = (TypeAttributes)(((int)type.Attributes & -2) | 0x1000);
                    break;

                case TypeDefinitionTreatment.RedirectToClrAttribute:
                    type.Attributes &= ~TypeAttributes.Public;
                    break;
                }
                if ((treatment & TypeDefinitionTreatment.Abstract) != 0)
                {
                    type.Attributes |= TypeAttributes.Abstract;
                }
                if ((treatment & TypeDefinitionTreatment.Internal) != 0)
                {
                    type.Attributes &= ~TypeAttributes.Public;
                }
                type.WindowsRuntimeProjection = projection;
            }
        }
        public static void Project(TypeDefinition type)
        {
            TypeDefinitionTreatment typeDefinitionTreatment = TypeDefinitionTreatment.None;
            MetadataKind            metadataKind            = type.Module.MetadataKind;

            if (type.IsWindowsRuntime)
            {
                switch (metadataKind)
                {
                case MetadataKind.WindowsMetadata:
                {
                    typeDefinitionTreatment = GetWellKnownTypeDefinitionTreatment(type);
                    if (typeDefinitionTreatment != 0)
                    {
                        ApplyProjection(type, new TypeDefinitionProjection(type, typeDefinitionTreatment));
                        return;
                    }
                    TypeReference baseType = type.BaseType;
                    typeDefinitionTreatment = ((baseType == null || !IsAttribute(baseType)) ? TypeDefinitionTreatment.NormalType : TypeDefinitionTreatment.NormalAttribute);
                    break;
                }

                case MetadataKind.ManagedWindowsMetadata:
                    if (NeedsWindowsRuntimePrefix(type))
                    {
                        typeDefinitionTreatment = TypeDefinitionTreatment.PrefixWindowsRuntimeName;
                    }
                    break;
                }
                if ((typeDefinitionTreatment == TypeDefinitionTreatment.PrefixWindowsRuntimeName || typeDefinitionTreatment == TypeDefinitionTreatment.NormalType) && !type.IsInterface && HasAttribute(type, "Windows.UI.Xaml", "TreatAsAbstractComposableClassAttribute"))
                {
                    typeDefinitionTreatment |= TypeDefinitionTreatment.Abstract;
                }
            }
            else if (metadataKind == MetadataKind.ManagedWindowsMetadata && IsClrImplementationType(type))
            {
                typeDefinitionTreatment = TypeDefinitionTreatment.UnmangleWindowsRuntimeName;
            }
            if (typeDefinitionTreatment != 0)
            {
                ApplyProjection(type, new TypeDefinitionProjection(type, typeDefinitionTreatment));
            }
        }
示例#6
0
 public TypeDefinitionProjection(TypeDefinition type, TypeDefinitionTreatment treatment)
 {
     this.Attributes = type.Attributes;
     this.Name       = type.Name;
     this.Treatment  = treatment;
 }
 public TypeDefinitionProjection(TypeDefinition type, TypeDefinitionTreatment treatment, Collection<MethodDefinition> redirectedMethods, Collection<KeyValuePair<InterfaceImplementation, InterfaceImplementation>> redirectedInterfaces)
 {
     Attributes = type.Attributes;
     Name = type.Name;
     Treatment = treatment;
     RedirectedMethods = redirectedMethods;
     RedirectedInterfaces = redirectedInterfaces;
 }
        public static void ApplyProjection(TypeDefinition type, TypeDefinitionProjection projection)
        {
            if (projection == null)
            {
                return;
            }

            TypeDefinitionTreatment treatment = projection.Treatment;

            switch (treatment & TypeDefinitionTreatment.KindMask)
            {
            case TypeDefinitionTreatment.NormalType:
                type.Attributes |= TypeAttributes.WindowsRuntime | TypeAttributes.Import;
                break;

            case TypeDefinitionTreatment.NormalAttribute:
                type.Attributes |= TypeAttributes.WindowsRuntime | TypeAttributes.Sealed;
                break;

            case TypeDefinitionTreatment.UnmangleWindowsRuntimeName:
                type.Attributes = type.Attributes & ~TypeAttributes.SpecialName | TypeAttributes.Public;
                type.Name       = type.Name.Substring("<CLR>".Length);
                break;

            case TypeDefinitionTreatment.PrefixWindowsRuntimeName:
                type.Attributes = type.Attributes & ~TypeAttributes.Public | TypeAttributes.Import;
                type.Name       = "<WinRT>" + type.Name;
                break;

            case TypeDefinitionTreatment.RedirectToClrType:
                type.Attributes = type.Attributes & ~TypeAttributes.Public | TypeAttributes.Import;
                break;

            case TypeDefinitionTreatment.RedirectToClrAttribute:
                type.Attributes = type.Attributes & ~TypeAttributes.Public;
                break;

            case TypeDefinitionTreatment.RedirectImplementedMethods:
            {
                type.Attributes |= TypeAttributes.WindowsRuntime | TypeAttributes.Import;

                foreach (KeyValuePair <InterfaceImplementation, InterfaceImplementation> redirectedInterfacePair in projection.RedirectedInterfaces)
                {
                    type.Interfaces.Add(redirectedInterfacePair.Value);

                    foreach (CustomAttribute customAttribute in redirectedInterfacePair.Key.CustomAttributes)
                    {
                        redirectedInterfacePair.Value.CustomAttributes.Add(customAttribute);
                    }

                    redirectedInterfacePair.Key.CustomAttributes.Clear();

                    foreach (MethodDefinition method in type.Methods)
                    {
                        foreach (MethodReference @override in method.Overrides)
                        {
                            if (TypeReferenceEqualityComparer.AreEqual(@override.DeclaringType, redirectedInterfacePair.Key.InterfaceType))
                            {
                                @override.DeclaringType = redirectedInterfacePair.Value.InterfaceType;
                            }
                        }
                    }
                }

                foreach (MethodDefinition method in projection.RedirectedMethods)
                {
                    type.Methods.Add(method);
                }
            }
            break;
            }

            if ((treatment & TypeDefinitionTreatment.Abstract) != 0)
            {
                type.Attributes |= TypeAttributes.Abstract;
            }

            if ((treatment & TypeDefinitionTreatment.Internal) != 0)
            {
                type.Attributes &= ~TypeAttributes.Public;
            }

            type.WindowsRuntimeProjection = projection;
        }