Exemple #1
0
        public static IEnumerable<CodeTypeMember> CreateMethodsWrapper(
            this INamedTypeSymbol typeToWrap,
            WrapsAttrNameChangeView methodToWrapNameChangeView,
            IList<string> namespaces
        )
        {
            // get all the methods of this name
            IEnumerable<IMethodSymbol> wrappedMethodsInfos =
                typeToWrap.GetInstanceMethodsOfName(methodToWrapNameChangeView.Name);

            List<CodeTypeMember> result = new List<CodeTypeMember>();

            foreach(IMethodSymbol methodSymbol in wrappedMethodsInfos)
            {
                CodeMemberMethod method = new CodeMemberMethod();

                method.Attributes = methodSymbol.GetFlags(methodToWrapNameChangeView.TheEncapsulationLevel);

                method.Name = methodToWrapNameChangeView.WrapperName;

                if (!methodSymbol.ReturnsVoid)
                {
                    ITypeSymbol returnType = methodSymbol.ReturnType;

                    method.ReturnType = returnType.GetTypeReference(namespaces);
                }

                List<CodeExpression> passedParameters =
                    method.SetMethodParams(methodSymbol, namespaces);

                string wrappedObjectName = typeToWrap.Name.GetWrappedObjectFieldName();
                // invoking the function
                CodeMethodInvokeExpression methodInvokeExpression =
                    new CodeMethodInvokeExpression
                    (
                        new CodeTypeReferenceExpression(wrappedObjectName),
                        methodToWrapNameChangeView.Name,
                        passedParameters.ToArray()
                     );

                if (!methodSymbol.ReturnsVoid)
                {
                    ITypeSymbol returnType = methodSymbol.ReturnType;
                    CodeVariableDeclarationStatement variableDeclarationStatement =
                        new CodeVariableDeclarationStatement
                    (
                        returnType.GetTypeReference(namespaces),
                        "result",
                        methodInvokeExpression
                    );

                    method.Statements.Add(variableDeclarationStatement);

                    CodeMethodReturnStatement returnStatement =
                        new CodeMethodReturnStatement(new CodeVariableReferenceExpression("result"));

                    method.Statements.Add(returnStatement);
                }
                else
                {
                    method.Statements.Add(methodInvokeExpression);
                }

                result.Add(method);
            }

            return result;
        }
Exemple #2
0
        public static CodeTypeMember CreateHandlerMethod(
            this OneWayEventWrapsView oneWayEventWrapsView,
            IList<string> namespaces
        )
        {
            string methodName = oneWayEventWrapsView.GetHandlerMethodName();

            CodeMemberMethod method = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Private,
                Name = methodName
            };

            INamedTypeSymbol wrappedType = oneWayEventWrapsView.TheWrappedType;

            string wrappedEventName = oneWayEventWrapsView.Name;
            string wrapperEventName = oneWayEventWrapsView.WrapperName;

            IEventSymbol eventSymbol = wrappedType.GetMembers(wrappedEventName).FirstOrDefault() as IEventSymbol;

            ITypeSymbol eventTypeSymbol = eventSymbol.Type;

            CodeTypeReference eventTypeReference = eventTypeSymbol.GetTypeReference(namespaces);

            IMethodSymbol invokeMethodSymbol = eventTypeSymbol.GetMembers("Invoke").FirstOrDefault() as IMethodSymbol;

            List<CodeExpression> passedParameters =
                method.SetMethodParams(invokeMethodSymbol, namespaces, oneWayEventWrapsView.ThisArgumentName);

            CodeConditionStatement codeConditionStatement = GetNotNullCondition(wrapperEventName);

            CodeDelegateInvokeExpression methodInvokeExpression =
                new CodeDelegateInvokeExpression
                (
                    new CodeEventReferenceExpression { EventName = wrapperEventName },
                    passedParameters.ToArray()
                );

            codeConditionStatement.TrueStatements.Add(methodInvokeExpression);

            method.Statements.Add(codeConditionStatement);

            return method;
        }