Example #1
0
        protected override void Visit(CodeAttributeDeclaration attr)
        {
            base.Visit(attr);

            string[] propsToRemove = null;
            if (CodeDomHelpers.MatchType(attr.AttributeType, typeof(ServiceContractAttribute)))
            {
                propsToRemove = s_serviceContractPropsToRemove;
            }
            else if (CodeDomHelpers.MatchType(attr.AttributeType, typeof(OperationContractAttribute)))
            {
                propsToRemove = s_operationContractPropsToRemove;
            }
            else if (CodeDomHelpers.MatchType(attr.AttributeType, typeof(FaultContractAttribute)))
            {
                propsToRemove = s_faultContractPropsToRemove;
            }
            else if (CodeDomHelpers.MatchType(attr.AttributeType, typeof(GeneratedCodeAttribute)))
            {
                attr.Arguments.Clear();
                attr.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(Tool.ToolName)));
                attr.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(Tool.PackageVersion)));
            }

            if (propsToRemove != null)
            {
                CollectionHelpers.MapList <CodeAttributeArgument>(attr.Arguments,
                                                                  delegate(CodeAttributeArgument arg)
                {
                    return(IsValidProperty(propsToRemove, arg.Name));
                },
                                                                  null
                                                                  );
            }
        }
Example #2
0
 protected override bool IsSpecificType(CodeTypeDeclaration type)
 {
     return(type.IsClass &&
            type.BaseTypes.Count == 2 &&
            (CodeDomHelpers.MatchGenericBaseType(type.BaseTypes[0], typeof(ClientBase <>)) ||
             CodeDomHelpers.MatchGenericBaseType(type.BaseTypes[0], typeof(DuplexClientBase <>))));
 }
Example #3
0
        private static CodeTypeDeclaration GetCallbackContractType(CodeTypeDeclaration iface)
        {
            CodeAttributeDeclaration serviceContractAttribute = null;

            foreach (CodeAttributeDeclaration attr in iface.CustomAttributes)
            {
                if (CodeDomHelpers.MatchType(attr.AttributeType, typeof(ServiceContractAttribute)))
                {
                    serviceContractAttribute = attr;
                    break;
                }
            }
            if (serviceContractAttribute != null)
            {
                foreach (CodeAttributeArgument arg in serviceContractAttribute.Arguments)
                {
                    if (arg.Name == "CallbackContract")
                    {
                        CodeTypeOfExpression exp = (CodeTypeOfExpression)arg.Value;
                        return(CodeDomHelpers.ResolveTypeReference(exp.Type));
                    }
                }
            }
            return(null);
        }
Example #4
0
 protected override bool IsSpecificType(CodeTypeDeclaration type)
 {
     // Check if the current client class is for duplex service.
     return(_taskBasedAsync &&
            base.IsSpecificType(type) &&
            CodeDomHelpers.MatchGenericBaseType(type.BaseTypes[0], typeof(DuplexClientBase <>)));
 }
Example #5
0
        // first-pass looks at each public BeginFoo or EndFoo method and makes it private.
        // for methods that are interface implementations, we need to remember them so that we can update
        // the calling code in the second pass
        private bool MapMethodFirstPass(CodeMemberMethod method)
        {
            if (method != null && (CodeDomHelpers.IsBeginMethod(method) || CodeDomHelpers.IsEndMethod(method)) && IsPublic(method.Attributes))
            {
                if (method.ImplementationTypes.Count == 0)
                {
                    // doesn't impl an iface method, just make it private, and remember it for the second pass
                    method.Attributes = MakePrivate(method.Attributes);

                    // clobber existing entries -- non iface-methods take precedence
                    _privateIfaceMethods[method.Name] = new PrivateInterfaceMethod(null);
                }
                else
                {
                    // impls an iface method, make it a private impl, and remember it for the second pass
                    CodeTypeReference ifaceType = method.ImplementationTypes[0];
                    method.ImplementationTypes.Clear();
                    method.PrivateImplementationType = ifaceType;

                    if (!_privateIfaceMethods.ContainsKey(method.Name))
                    {
                        // only add it if it wasn't already there -- non-iface methods take precedence
                        _privateIfaceMethods.Add(method.Name, new PrivateInterfaceMethod(ifaceType));
                    }
                }
            }
            return(true); // don't remove
        }
Example #6
0
        protected override void VisitClientClass(CodeTypeDeclaration type)
        {
            base.VisitClientClass(type);
            CodeTypeDeclaration serviceContractInterface = CodeDomHelpers.ResolveTypeReference(type.BaseTypes[0].TypeArguments[0]);
            CodeTypeDeclaration callbackInterface        = GetCallbackContractType(serviceContractInterface);

            RemoveAsyncMethods(callbackInterface);
            CreateEventBasedDuplexClass(type, callbackInterface);
        }
Example #7
0
 private static bool IsValidAttribute(CodeAttributeDeclaration attr)
 {
     for (int i = 0; i < s_attrsToRemove.Length; i++)
     {
         if (CodeDomHelpers.MatchType(attr.AttributeType, s_attrsToRemove[i]))
         {
             return(false);
         }
     }
     return(true);
 }
Example #8
0
 private static void RemoveAsyncMethods(CodeTypeDeclaration callbackInterface)
 {
     CollectionHelpers.MapList <CodeMemberMethod>(
         callbackInterface.Members,
         delegate(CodeMemberMethod method)
     {
         return(!CodeDomHelpers.IsTaskAsyncMethod(method));
     },
         null
         );
 }
Example #9
0
 private static bool IsIXmlSerializableType(CodeTypeDeclaration typeDecl)
 {
     foreach (CodeTypeReference typeRef in typeDecl.BaseTypes)
     {
         if (CodeDomHelpers.MatchType <Microsoft.Xml.Serialization.IXmlSerializable>(typeRef))
         {
             return(true);
         }
     }
     return(false);
 }
Example #10
0
 private static bool IsValidConstructor(CodeConstructor ctor, Type[][] validCtors)
 {
     for (int i = 0; i < validCtors.Length; i++)
     {
         if (CodeDomHelpers.MatchSignatures(ctor.Parameters, validCtors[i]))
         {
             return(true);
         }
     }
     return(false);
 }
Example #11
0
        protected override void VisitAttributedType(CodeTypeDeclaration type)
        {
            base.VisitAttributedType(type);

            CollectionHelpers.MapList <CodeMemberMethod>(
                type.Members,
                delegate(CodeMemberMethod method)
            {
                return(CodeDomHelpers.IsBeginMethod(method) ||
                       CodeDomHelpers.IsTaskAsyncMethod(method) ||
                       CodeDomHelpers.IsEndMethod(method));
            },
                this.CopyAttrsToTaskAsyncMethod
                );
        }
        protected override void VisitClientClass(CodeTypeDeclaration type)
        {
            base.VisitClientClass(type);

            CollectionHelpers.MapList <CodeMemberMethod>(
                type.Members,
                delegate(CodeMemberMethod method)
            {
                return(method is CodeConstructor ||
                       CodeDomHelpers.IsBeginMethod(method) ||
                       CodeDomHelpers.IsEndMethod(method) ||
                       CodeDomHelpers.IsTaskAsyncMethod(method));
            },
                null
                );
        }
Example #13
0
        private void CopyAttrsToTaskAsyncMethod(CodeMemberMethod syncMethod, int index)
        {
            CodeMemberMethod taskAyncMethod = CodeDomHelpers.GetTaskAsyncMethodForMethod(CurrentType.Members, syncMethod);

            if (taskAyncMethod != null && !ReferenceEquals(taskAyncMethod, syncMethod))
            {
                foreach (CodeAttributeDeclaration attr in syncMethod.CustomAttributes)
                {
                    // skip [OperationContract] as that appears in both places and is guaranteed to be the same
                    if (!CodeDomHelpers.MatchType <OperationContractAttribute>(attr.AttributeType))
                    {
                        taskAyncMethod.CustomAttributes.Add(attr);
                    }
                }
            }
        }
Example #14
0
        private static bool IsExtensionDataMember(CodeTypeMember member)
        {
            CodeTypeReference memberType = null;

            switch (member.Name)
            {
            case "ExtensionData":     // property
                memberType = member is CodeMemberProperty ? ((CodeMemberProperty)member).Type : null;
                break;

            case "extensionDataField":     // field
                memberType = member is CodeMemberField ? ((CodeMemberField)member).Type : null;
                break;
            }
            return(memberType != null && CodeDomHelpers.MatchType(memberType, typeof(ExtensionDataObject)));
        }
Example #15
0
        private static void AddMethods(CodeTypeDeclaration callbackImpl, CodeTypeDeclaration callbackInterface, Dictionary <string, string> methodNames)
        {
            foreach (CodeMemberMethod method in callbackInterface.Members)
            {
                System.Diagnostics.Debug.Assert((IsSyncOperationContract(method)), "Only support sync callback on immersive project");
                CodeMemberMethod m = CodeDomHelpers.GetImplementationOfMethod(callbackImpl.BaseTypes[1], method);

                // new object[] { msg, timestamp });
                CodeArrayCreateExpression arr = new CodeArrayCreateExpression();
                arr.CreateType = new CodeTypeReference(typeof(object));
                foreach (CodeParameterDeclarationExpression p in method.Parameters)
                {
                    arr.Initializers.Add(new CodeVariableReferenceExpression(p.Name));
                }

                // proxy.OnOnEchoReceived(new object[] { msg, timestamp});
                m.Statements.Add(
                    new CodeMethodInvokeExpression()
                {
                    Method = new CodeMethodReferenceExpression()
                    {
                        TargetObject = new CodeFieldReferenceExpression()
                        {
                            TargetObject = new CodeThisReferenceExpression(),
                            FieldName    = "proxy"
                        },
                        MethodName = methodNames[method.Name],
                    },
                    Parameters =
                    {
                        arr
                    },
                }
                    );

                if (method.ReturnType.BaseType != typeof(void).FullName)
                {
                    m.Statements.Add(new CodeMethodReturnStatement(
                                         new CodeDefaultValueExpression(
                                             method.ReturnType)
                                         ));
                }

                callbackImpl.Members.Add(m);
            }
        }
Example #16
0
        private static bool IsNonFilteredMember(CodeTypeMember member)
        {
            CodeTypeReference memberType = null;

            CodeMemberProperty memberProp = member as CodeMemberProperty;

            if (memberProp != null)
            {
                memberType = memberProp.Type;
            }
            else
            {
                CodeMemberField memberField = member as CodeMemberField;
                if (memberField != null)
                {
                    memberType = memberField.Type;
                }
            }

            return(memberType == null || !CodeDomHelpers.MatchAnyBaseType(memberType, s_filteredTypes));
        }
Example #17
0
        private void MoveConfigIntoCode(CodeTypeDeclaration clientType, Collection <ServiceEndpoint> endpoints, CodeNamespace namespaceDecl)
        {
            string               contractName        = ExtractContract(clientType);
            List <string>        endpointNames       = new List <string>();
            MethodCreationHelper helperMethodCreator = new MethodCreationHelper(clientType);

            foreach (ServiceEndpoint endpoint in endpoints)
            {
                if (contractName.EndsWith(endpoint.Contract.Name, StringComparison.Ordinal) || contractName.EndsWith(UniqueCodeIdentifierScope.MakeValid(endpoint.Contract.CodeName, endpoint.Contract.CodeName), StringComparison.Ordinal))
                {
                    endpoint.Name = CodeDomHelpers.GetValidValueTypeIdentifier(endpoint.Name);

                    // resolve duplicated names.
                    int i = 1;
                    while (endpointNames.Contains(endpoint.Name))
                    {
                        endpoint.Name += i;
                        i++;
                    }

                    if (helperMethodCreator.AddClientEndpoint(endpoint))
                    {
                        endpointNames.Add(endpoint.Name);
                    }
                }
            }

            bool endpointConfigurationExists = false;

            if (endpointNames.Count > 0)
            {
                helperMethodCreator.AddConfigurationEnum(endpointNames);
                helperMethodCreator.AddMethods(endpointNames, this.IsVB);
                endpointConfigurationExists = true;
            }

            bool shouldRemoveDefault = (endpointNames.Count != 1);

            this.FixupConstructors(clientType, shouldRemoveDefault, endpointConfigurationExists, namespaceDecl, endpointNames);
        }
Example #18
0
        protected override void VisitAttributedType(CodeTypeDeclaration type)
        {
            base.VisitAttributedType(type);

            // remove IExtensibleDataObject impl
            CollectionHelpers.MapList <CodeTypeReference>(
                type.BaseTypes,
                delegate(CodeTypeReference typeRef)
            {
                return(!CodeDomHelpers.MatchType <IExtensibleDataObject>(typeRef));
            },
                null
                );

            // remove ExtensionData members
            CollectionHelpers.MapList <CodeTypeMember>(
                type.Members,
                delegate(CodeTypeMember member)
            {
                return(!IsExtensionDataMember(member));
            },
                null
                );
        }
Example #19
0
 protected virtual bool Match(CodeTypeReference typeref)
 {
     return(CodeDomHelpers.MatchBaseType(typeref, this.srcType));
 }
Example #20
0
 internal static CodeMemberMethod GetTaskAsyncMethodForMethod(CodeTypeMemberCollection members, CodeMemberMethod method)
 {
     return(CodeDomHelpers.IsTaskAsyncMethod(method) ? method : CodeDomHelpers.FindMethodByName(members, CodeDomHelpers.GetMethodNameBase(method) + "Async"));
 }
Example #21
0
        public void AddConfigurationEnum(List <string> endpointNames)
        {
            CodeTypeDeclaration configurationsEnum = new CodeTypeDeclaration(ConfigToCodeConstants.EndpointConfigurationEnumTypeName);

            configurationsEnum.IsEnum = true;
            foreach (string endpointName in endpointNames)
            {
                configurationsEnum.Members.Add(new CodeMemberField(ConfigToCodeConstants.EndpointConfigurationEnumTypeName, CodeDomHelpers.EscapeName(endpointName)));
            }

            this.ClientType.Members.Add(configurationsEnum);
        }
 protected override bool IsSpecificType(CodeTypeDeclaration type)
 {
     return(CodeDomHelpers.FindAttribute <T>(type.CustomAttributes) != null);
 }