示例#1
0
        /// <summary>
        /// Generates a notification partial method for the specified method, this is of the form
        /// OnCreated(args...) for the Created(args...) method.
        /// </summary>
        /// <param name="methodName">The name of the method to create a notification method for.</param>
        /// <param name="parameters">the method parameters, if any.</param>
        /// <returns>Code snippet for the notification partial method.</returns>
        private CodeSnippetTypeMember CreateNotificationPartialMethod(string methodName, CodeParameterDeclarationExpressionCollection parameters)
        {
            CodeMemberMethod method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            method.Name       = "On" + methodName;
            method.Parameters.AddRange(parameters);

            if (this.proxyGenerator.ClientProxyCodeGenerationOptions.UseFullTypeNames)
            {
                foreach (CodeParameterDeclarationExpression paramExp in parameters.Cast <CodeParameterDeclarationExpression>())
                {
                    SetGlobalTypeReference(paramExp.Type);
                }
            }

            StringBuilder snippet = null;

            using (CodeDomProvider provider = CodeDomProvider.CreateProvider(this.isCSharp ? "CSharp" : "VisualBasic"))
            {
                using (StringWriter snippetWriter = new StringWriter(System.Globalization.CultureInfo.CurrentCulture))
                {
                    provider.GenerateCodeFromMember(method, snippetWriter, new CodeGeneratorOptions());
                    snippet = snippetWriter.GetStringBuilder();
                }
            }

            // replace 'public' with 'partial' - partial methods cannot be public.
            // observe we replace 'ublic' only to get the proper language keyword capitalization.
            snippet.Replace("\r\n", string.Empty);
            snippet.Replace("ublic", "artial", 1, "ublic".Length);

            if (this.isCSharp)
            {
                int idx = snippet.ToString().LastIndexOf(')');
                snippet.Remove(idx + 1, snippet.Length - idx - 1);
                snippet.Append(";");
            }
            else // VB.net
            {
                snippet.Insert(0, "Private ");
                int idx = snippet.ToString().IndexOf("End Sub", StringComparison.Ordinal);
                snippet.Insert(idx, string.Concat("\r\n", this.indent));
            }

            snippet.Insert(0, this.indent);

            return(new CodeSnippetTypeMember(snippet.ToString()));
        }
        private void RemoveDuplicateParameters(CodeParameterDeclarationExpressionCollection parameters)
        {
            var toKeep = parameters
                         .Cast <CodeParameterDeclarationExpression>()
                         .GroupBy(x => new { x.Name })
                         .SelectMany(x => x.Take(1))
                         .ToArray();

            parameters.Clear();

            foreach (var objToKeep in toKeep)
            {
                parameters.Add(objToKeep);
            }
        }
示例#3
0
 private void RemovePreviousOverload(CodeParameterDeclarationExpressionCollection args, string methodName)
 {
     if (methodName == type.Name)
     {
         methodName = ".ctor";
     }
     if (args.Count > 0 && args[args.Count - 1].Name.Contains(" = "))
     {
         IEnumerable <CodeParameterDeclarationExpression> parameters = args.Cast <CodeParameterDeclarationExpression>().Take(args.Count - 1);
         foreach (CodeMemberMethod method in (from method in this.type.Members.OfType <CodeMemberMethod>()
                                              where method.Name == methodName &&
                                              (method.Attributes & MemberAttributes.Override) == 0 &&
                                              method.Parameters.Cast <CodeParameterDeclarationExpression>()
                                              .SequenceEqual(parameters, new ParameterTypeComparer())
                                              select method).ToList())
         {
             this.type.Members.Remove(method);
         }
     }
 }
示例#4
0
 private void RemovePreviousOverload(CodeParameterDeclarationExpressionCollection args, string methodName)
 {
     if (methodName == type.Name)
     {
         methodName = ".ctor";
     }
     if (args.Count > 0 && args[args.Count - 1].Name.Contains(" = "))
     {
         IEnumerable<CodeParameterDeclarationExpression> parameters = args.Cast<CodeParameterDeclarationExpression>().Take(args.Count - 1);
         foreach (CodeMemberMethod method in (from method in this.type.Members.OfType<CodeMemberMethod>()
                                              where method.Name == methodName &&
                                                    (method.Attributes & MemberAttributes.Override) == 0 &&
                                                    method.Parameters.Cast<CodeParameterDeclarationExpression>()
                                                          .SequenceEqual(parameters, new ParameterTypeComparer())
                                              select method).ToList())
         {
             this.type.Members.Remove(method);
         }
     }
 }
 private void DisambiguateMethodParameterNames(CodeParameterDeclarationExpressionCollection parameters)
 {
     var list = parameters.Cast<CodeParameterDeclarationExpression>().ToList<CodeParameterDeclarationExpression>();
     list.ForEach(p => DisambiguateMethodParameterName(p.Name, list));
 }