Exemple #1
0
        internal static MethodMember CreateStubMethodMemberFromMethodReference(this TypeFactory typeFactory, IType type,
                                                                               MethodReference methodReference)
        {
            if (type == null || methodReference == null)
            {
                return(null);
            }

            var typeReference     = methodReference.ReturnType;
            var returnType        = typeFactory.GetOrCreateStubTypeFromTypeReference(typeReference);
            var parameters        = methodReference.GetParameters(typeFactory).ToList();
            var genericParameters = methodReference.GenericParameters
                                    .Select(parameter => new GenericParameter(parameter.Name)).ToList();

            var methodForm = methodReference.HasConstructorName() ? MethodForm.Constructor : MethodForm.Normal;

            return(new MethodMember(methodReference.BuildMethodMemberName(), methodReference.FullName, type,
                                    Public, parameters, returnType, false, methodForm, genericParameters));
        }
Exemple #2
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>()));
        }