Example #1
0
        public MethodReference convert(MethodReference methodRef)
        {
            if (methodRef.GetType() != typeof(MethodReference) && methodRef.GetType() != typeof(MethodDefinition))
            {
                throw new ApplicationException("Invalid method reference type");
            }
            if (isInOurModule(methodRef))
            {
                return(tryGetMethodDefinition(methodRef));
            }

            return(copy(methodRef));
        }
Example #2
0
 private void AssertMethodReferencesAreIdentical(MethodReference expected, MethodReference actual)
 {
     Assert.Equal(expected.GetType(), actual.GetType());
     this.AssertGenericParametersAreIdentical(expected, actual);
     this.AssertTypeReferencesAreIdentical(expected.DeclaringType, actual.DeclaringType);
     if (expected is GenericInstanceMethod)
     {
         this.AssertGenericArgumentsAreIdentical((GenericInstanceMethod)expected, (GenericInstanceMethod)actual);
     }
 }
Example #3
0
        public MethodReference copy(MethodReference methodRef)
        {
            if (methodRef.GetType() != typeof(MethodReference) && methodRef.GetType() != typeof(MethodDefinition))
            {
                throw new ApplicationException("Invalid method reference type");
            }

            var newMethodRef = new MethodReference(methodRef.Name, convert(methodRef.MethodReturnType.ReturnType), convert(methodRef.DeclaringType));

            newMethodRef.HasThis           = methodRef.HasThis;
            newMethodRef.ExplicitThis      = methodRef.ExplicitThis;
            newMethodRef.CallingConvention = methodRef.CallingConvention;
            foreach (var param in methodRef.Parameters)
            {
                newMethodRef.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, convert(param.ParameterType)));
            }
            foreach (var gp in methodRef.GenericParameters)
            {
                newMethodRef.GenericParameters.Add(new GenericParameter(gp.Name, newMethodRef));
            }
            return(newMethodRef);
        }
Example #4
0
        /// <summary>
        /// Resolves a method reference.
        /// </summary>
        /// <param name="methodRef">The method reference to resolve.</param>
        /// <param name="assembly">The assembly that declares the reference.</param>
        /// <returns>The method referred to by the reference.</returns>
        internal IMethod Resolve(MethodReference methodRef, ClrAssembly assembly)
        {
            if (methodRef is MethodSpecification)
            {
                if (methodRef is GenericInstanceMethod)
                {
                    var genInstMethod = (GenericInstanceMethod)methodRef;
                    var elemMethod    = Resolve(genInstMethod.ElementMethod, assembly);
                    return(elemMethod.MakeGenericMethod(
                               genInstMethod.GenericArguments
                               .Select(arg => Resolve(arg, assembly))
                               .Zip(elemMethod.GenericParameters, BoxTypeArgumentIfNecessary)
                               .ToArray()));
                }
                else
                {
                    throw new NotSupportedException(
                              "Cannot resolve unsupported method specification type " +
                              $"'{methodRef.GetType()}' for method reference '{methodRef}'.");
                }
            }

            var declaringType = Resolve(methodRef.DeclaringType, assembly);
            var name          = NameConversion.ParseSimpleName(methodRef.Name);

            var standinReplacer = CreateStandinReplacingVisitor(declaringType);

            var standinRetType = Resolve(methodRef.ReturnType, assembly, declaringType, true);
            var returnType     = TypeHelpers.BoxIfReferenceType(
                standinReplacer.Visit(standinRetType));

            var parameterTypes = methodRef.Parameters
                                 .Select(param =>
                                         TypeHelpers.BoxIfReferenceType(
                                             standinReplacer.Visit(
                                                 Resolve(param.ParameterType, assembly, declaringType, true))))
                                 .ToArray();

            return(PickSingleResolvedMember(
                       methodRef,
                       methodIndex.GetAll(
                           declaringType,
                           ClrMethodSignature.Create(
                               name,
                               methodRef.GenericParameters.Count,
                               returnType,
                               parameterTypes))));
        }