예제 #1
0
        private MethodMemberInstance CreateMethodMemberFromMethodReference(
            [NotNull] ITypeInstance <IType> typeInstance, [NotNull] MethodReference methodReference)
        {
            if (methodReference.IsGenericInstance)
            {
                var elementMethod =
                    CreateMethodMemberFromMethodReference(typeInstance, methodReference.GetElementMethod()).Member;

                var genericInstanceMethod = (GenericInstanceMethod)methodReference;
                var genericArguments      = genericInstanceMethod.GenericArguments
                                            .Select(CreateGenericArgumentFromTypeReference)
                                            .Where(argument => !argument.Type.IsCompilerGenerated);

                return(new MethodMemberInstance(elementMethod, typeInstance.GenericArguments, genericArguments));
            }

            var returnTypeReference = methodReference.ReturnType;
            var returnType          = GetOrCreateStubTypeInstanceFromTypeReference(returnTypeReference);

            var        name                = methodReference.BuildMethodMemberName();
            var        fullName            = methodReference.BuildFullName();
            var        isGeneric           = methodReference.HasGenericParameters;
            var        isCompilerGenerated = methodReference.IsCompilerGenerated();
            MethodForm methodForm;
            Visibility visibility;
            bool       isStub;

            MethodDefinition methodDefinition;

            try
            {
                methodDefinition = methodReference.Resolve();
            }
            catch (AssemblyResolutionException)
            {
                methodDefinition = null;
            }

            if (methodDefinition == null)
            {
                visibility = Public;
                methodForm = methodReference.HasConstructorName() ? MethodForm.Constructor : MethodForm.Normal;
                isStub     = true;
            }
            else
            {
                visibility = methodDefinition.GetVisibility();
                methodForm = methodDefinition.GetMethodForm();
                isStub     = false;
            }

            var methodMember = new MethodMember(name, fullName, typeInstance.Type, visibility, returnType,
                                                false, methodForm, isGeneric, isStub, isCompilerGenerated);

            var parameters = methodReference.GetParameters(this).ToList();

            methodMember.ParameterInstances.AddRange(parameters);

            var genericParameters = GetGenericParameters(methodReference);

            methodMember.GenericParameters.AddRange(genericParameters);

            return(new MethodMemberInstance(methodMember, typeInstance.GenericArguments,
                                            Enumerable.Empty <GenericArgument>()));
        }