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);
        }
Esempio n. 2
0
        public static TypeDefinitionProjection RemoveProjection(TypeDefinition type)
        {
            if (!type.IsWindowsRuntimeProjection)
            {
                return(null);
            }

            var projection = type.WindowsRuntimeProjection;

            type.WindowsRuntimeProjection = null;

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

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

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

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

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

            return(projection);
        }
        public static int GetHashCodeFor(MethodReference obj)
        {
            // a very good prime number
            const int hashCodeMultiplier = 486187739;

            var genericInstanceMethod = obj as GenericInstanceMethod;

            if (genericInstanceMethod != null)
            {
                var hashCode = GetHashCodeFor(genericInstanceMethod.ElementMethod);
                for (var i = 0; i < genericInstanceMethod.GenericArguments.Count; i++)
                {
                    hashCode = hashCode * hashCodeMultiplier + TypeReferenceEqualityComparer.GetHashCodeFor(genericInstanceMethod.GenericArguments[i]);
                }
                return(hashCode);
            }

            return(TypeReferenceEqualityComparer.GetHashCodeFor(obj.DeclaringType) * hashCodeMultiplier + obj.Name.GetHashCode());
        }
        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);
        }
Esempio n. 5
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);
                    }
                }
            }

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

            return(true);
        }
Esempio n. 6
0
        public static void ApplyProjection(TypeDefinition type, TypeDefinitionProjection projection)
        {
            if (projection == null)
            {
                return;
            }

            var 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 (var redirectedInterfacePair in projection.RedirectedInterfaces)
                {
                    type.Interfaces.Add(redirectedInterfacePair.Value);

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

                    redirectedInterfacePair.Key.CustomAttributes.Clear();

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

                foreach (var 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;
        }