示例#1
0
        public static bool AreSignaturesEqual(MethodReference x, MethodReference y, TypeComparisonMode comparisonMode = TypeComparisonMode.Exact)
        {
            if (x.HasThis != y.HasThis)
            {
                return(false);
            }

            if (x.Parameters.Count != y.Parameters.Count)
            {
                return(false);
            }

            if (x.GenericParameters.Count != y.GenericParameters.Count)
            {
                return(false);
            }

            for (var i = 0; i < x.Parameters.Count; i++)
            {
                if (!TypeReferenceEqualityComparer.AreEqual(x.Parameters[i].ParameterType, y.Parameters[i].ParameterType, comparisonMode))
                {
                    return(false);
                }
            }

            if (!TypeReferenceEqualityComparer.AreEqual(x.ReturnType, y.ReturnType, comparisonMode))
            {
                return(false);
            }

            return(true);
        }
        public static TypeDefinitionProjection RemoveProjection(TypeDefinition type)
        {
            if (!type.IsWindowsRuntimeProjection)
            {
                return(null);
            }

            TypeDefinitionProjection projection = type.WindowsRuntimeProjection;

            type.WindowsRuntimeProjection = null;

            type.Attributes = projection.Attributes;
            type.Name       = projection.Name;

            if (projection.Treatment == TypeDefinitionTreatment.RedirectImplementedMethods)
            {
                foreach (MethodDefinition method in projection.RedirectedMethods)
                {
                    type.Methods.Remove(method);
                }

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

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

                    redirectedInterfacePair.Value.CustomAttributes.Clear();
                    type.Interfaces.Remove(redirectedInterfacePair.Value);
                }
            }

            return(projection);
        }
示例#3
0
        public static bool AreEqual(MethodReference x, MethodReference y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

            if (x.HasThis != y.HasThis)
            {
                return(false);
            }

            if (x.HasParameters != y.HasParameters)
            {
                return(false);
            }

            if (x.HasGenericParameters != y.HasGenericParameters)
            {
                return(false);
            }

            if (x.Parameters.Count != y.Parameters.Count)
            {
                return(false);
            }

            if (x.Name != y.Name)
            {
                return(false);
            }

            if (!TypeReferenceEqualityComparer.AreEqual(x.DeclaringType, y.DeclaringType))
            {
                return(false);
            }

            var xGeneric = x as GenericInstanceMethod;
            var yGeneric = y as GenericInstanceMethod;

            if (xGeneric != null || yGeneric != null)
            {
                if (xGeneric == null || yGeneric == null)
                {
                    return(false);
                }

                if (xGeneric.GenericArguments.Count != yGeneric.GenericArguments.Count)
                {
                    return(false);
                }

                for (int i = 0; i < xGeneric.GenericArguments.Count; i++)
                {
                    if (!TypeReferenceEqualityComparer.AreEqual(xGeneric.GenericArguments[i], yGeneric.GenericArguments[i]))
                    {
                        return(false);
                    }
                }
            }

            var xResolved = x.Resolve();
            var yResolved = y.Resolve();

            if (xResolved != yResolved)
            {
                return(false);
            }

            if (xResolved == null)
            {
                // We couldn't resolve either method. In order for them to be equal, their parameter types _must_ match. But wait, there's a twist!
                // There exists a situation where we might get into a recursive state: parameter type comparison might lead to comparing the same
                // methods again if the parameter types are generic parameters whose owners are these methods. We guard against these by using a
                // thread static list of all our comparisons carried out in the stack so far, and if we're in progress of comparing them already,
                // we'll just say that they match.

                if (xComparisonStack == null)
                {
                    xComparisonStack = new List <MethodReference> ();
                }

                if (yComparisonStack == null)
                {
                    yComparisonStack = new List <MethodReference> ();
                }

                for (int i = 0; i < xComparisonStack.Count; i++)
                {
                    if (xComparisonStack[i] == x && yComparisonStack[i] == y)
                    {
                        return(true);
                    }
                }

                xComparisonStack.Add(x);

                try {
                    yComparisonStack.Add(y);

                    try {
                        for (int i = 0; i < x.Parameters.Count; i++)
                        {
                            if (!TypeReferenceEqualityComparer.AreEqual(x.Parameters[i].ParameterType, y.Parameters[i].ParameterType))
                            {
                                return(false);
                            }
                        }
                    } finally {
                        yComparisonStack.RemoveAt(yComparisonStack.Count - 1);
                    }
                } finally {
                    xComparisonStack.RemoveAt(xComparisonStack.Count - 1);
                }
            }

            return(true);
        }
        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;
        }