Exemplo n.º 1
0
        internal static bool IsCompilerGeneratorEnumerator(this MethodDeclaration methodDeclaration, ref Lambda.Method methodBlock)
        {
            var methodDefinition = methodDeclaration.Annotation <MethodDefinition>();
            var candidateNewObj  = methodDefinition.Body.Instructions[1];

            if (candidateNewObj.OpCode.Equals(OpCodes.Newobj))
            {
                var memberReference = candidateNewObj.Operand as Mono.Cecil.MethodReference;

                if (memberReference != null)
                {
                    var constructorInfo = memberReference.GetActualMethod <ConstructorInfo>();

                    if (constructorInfo != null && constructorInfo.DeclaringType.IsCompilerGenerated() && IsEnumerator(methodDefinition))
                    {
                        Instruction initializer = methodDefinition.Body.Instructions[0];

                        methodBlock = new Lambda.CompilerGeneratedEnumerator(initializer, candidateNewObj);
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
0
 public override TResult VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
 {
     Debug.Assert(context.CurrentMethod == null);
     try {
         context.CurrentMethod = methodDeclaration.Annotation <MethodDef>();
         return(base.VisitMethodDeclaration(methodDeclaration, data));
     } finally {
         context.CurrentMethod = null;
     }
 }
Exemplo n.º 3
0
        internal static Type GetReturnType(this MethodDeclaration methodDeclaration)
        {
            if (methodDeclaration != null)
            {
                var methodReference = methodDeclaration.Annotation <MethodReference>();

                if (methodReference != null)
                {
                    return(methodReference.ReturnType.GetActualType());
                }
            }

            return(TypeSystem.Void);
        }
Exemplo n.º 4
0
        public override AstNode VisitMethodDeclaration(MethodDeclaration methodDeclaration, ICecilArgumentsResolver argumentsResolver)
        {
            TypeReference returnTypeReference = null;
            var           methodReference     = methodDeclaration.Annotation <MethodReference>();

            methodDeclaration.TypeParameters.ForEach(p => p.AcceptVisitor(this, argumentsResolver));
            methodDeclaration.Parameters.ForEach(p => p.AcceptVisitor(this, argumentsResolver));
            methodDeclaration.Body.AcceptVisitor(this, argumentsResolver);
            methodDeclaration.ReturnType.AcceptVisitor(this, argumentsResolver);

            if (methodReference.ReturnType.IsGeneric())
            {
                returnTypeReference        = methodDeclaration.ReturnType.Annotation <TypeReference>();
                methodReference.ReturnType = returnTypeReference;
            }

            return(methodDeclaration);
        }
Exemplo n.º 5
0
		public virtual void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
		{
			StartNode(methodDeclaration);
			var builder = methodDeclaration.Annotation<MethodDebugInfoBuilder>();
			if (builder != null)
				builder.StartPosition = writer.GetLocation();
			WriteAttributes(methodDeclaration.Attributes);
			WriteModifiers(methodDeclaration.ModifierTokens);
			var oldRef = currentMethodReference;
			currentMethodReference = new object();
			methodDeclaration.ReturnType.AcceptVisitor(this);
			Space();
			WritePrivateImplementationType(methodDeclaration.PrivateImplementationType);
			WriteIdentifier(methodDeclaration.NameToken);
			WriteTypeParameters(methodDeclaration.TypeParameters, CodeBracesRangeFlags.AngleBrackets);
			Space(policy.SpaceBeforeMethodDeclarationParentheses);
			WriteCommaSeparatedListInParenthesis(methodDeclaration.Parameters, policy.SpaceWithinMethodDeclarationParentheses, CodeBracesRangeFlags.Parentheses);
			int count = 0;
			foreach (Constraint constraint in methodDeclaration.Constraints) {
				if (count-- <= 0) {
					cancellationToken.ThrowIfCancellationRequested();
					count = CANCEL_CHECK_LOOP_COUNT;
				}
				constraint.AcceptVisitor(this);
			}
			WriteMethodBody(methodDeclaration.Body);
			if (builder != null)
				builder.EndPosition = writer.GetLocation();
			currentMethodReference = oldRef;
			EndNode(methodDeclaration);
		}
Exemplo n.º 6
0
        public override void VisitMethodDeclaration(MethodDeclaration e)
        {
            base.VisitMethodDeclaration(e);

            var md = e.Annotation<MethodDefinition>();
            if (md != null)
            {
                if (!e.PrivateImplementationType.IsNull && md.DeclaringType.IsCompilerGenerated())
                {
                    var convert = true;
                    if (md.DeclaringType.Interfaces.Count > 1)
                    {
                        var trInterface = e.PrivateImplementationType.Annotation<TypeReference>();
                        var hasSameNameInterfaces = md.DeclaringType.Interfaces.Select(x => x.Resolve()).Where(x => x == null || x.FullName != trInterface.FullName && x.Methods.Any(y => IsSameExceptReturnType(y, md)));
                        if (hasSameNameInterfaces.Any())
                        {
                            if (hasSameNameInterfaces.Any(x => x == null) ||
                                !trInterface.HasGenericParameters ||
                                hasSameNameInterfaces.Any(x => x.HasGenericParameters))
                            {
                                convert = false;
                            }
                        }
                    }
                    if (convert)
                    {
                        e.PrivateImplementationType = null;
                        e.Modifiers |= Modifiers.Public;
                    }
                }

                if (md.HasOverrides)
                {
                    var omd = md.Overrides.First().Resolve();
                    if (omd != null)
                    {
                        if (omd.IsGetter || omd.IsSetter)
                        {
                            // 转换为属性
                            if (omd.IsGetter) TryConverterMethodToProperty(e, omd);
                        }
                        else
                        {
                            e.Name = omd.WellName;
                        }
                    }
                }
            }
        }