コード例 #1
0
ファイル: CSharp.cs プロジェクト: nagyistge/NClass
        /// <summary>
        /// Gets the <see cref="NRParameter"/> as a C# string.
        /// </summary>
        /// <param name="parameter">The parameter to get the code for.</param>
        /// <returns>A string representing the parameter.</returns>
        public static string Declaration(this NRParameter parameter)
        {
            string extensionParam = parameter.IsExtensionParameter ? "this " : "";
            string attributes     = parameter.Attributes.Aggregate("", (current, nrAttribute) => current + nrAttribute.Declaration());

            attributes = AddSpace(attributes);

            string modifier = AddSpace(parameter.ParameterModifier.Declaration());

            string type = parameter.Type.Declaration();

            if (type.EndsWith("&"))
            {
                type = type.Substring(0, type.Length - 1);
            }

            string defaultValue = "";

            if (!String.IsNullOrWhiteSpace(parameter.DefaultValue))
            {
                // There is a default value
                if (parameter.TypeFullName == typeof(string).FullName)
                {
                    // It is a string. So we have to escape it.
                    using (var writer = new StringWriter())
                    {
                        using (var provider = CodeDomProvider.CreateProvider("CSharp"))
                        {
                            provider.GenerateCodeFromExpression(new CodePrimitiveExpression(parameter.DefaultValue), writer, null);
                            defaultValue = " = " + writer;
                        }
                    }
                }
                else if (parameter.TypeFullName == typeof(char).FullName)
                {
                    // It is a char. So we have to escape it.
                    using (var writer = new StringWriter())
                    {
                        using (var provider = CodeDomProvider.CreateProvider("CSharp"))
                        {
                            provider.GenerateCodeFromExpression(new CodePrimitiveExpression(parameter.DefaultValue[0]), writer, null);
                            defaultValue = " = " + writer;
                        }
                    }
                }
                else
                {
                    // It is something else. Just print it.
                    defaultValue = " = " + parameter.DefaultValue;
                }
            }

            return(String.Format("{0}{1}{2}{3} {4}{5}", attributes, extensionParam, modifier, type, parameter.Name,
                                 defaultValue));
        }
コード例 #2
0
 /// <summary>
 /// Visit a <see cref="NRParameter"/>.
 /// </summary>
 /// <param name="nrParameter">The <see cref="NRParameter"/> to visit.</param>
 public void Visit(NRParameter nrParameter)
 {
     foreach (NRAttribute nrAttribute in nrParameter.Attributes)
     {
         Output(GetAttribute(nrAttribute) + " ", 0);
     }
     Output(ToString(nrParameter.ParameterModifier) + ToString(nrParameter.Type) + " " + nrParameter.Name, 0);
     if (nrParameter.ParameterModifier == ParameterModifier.Optional)
     {
         Output(" = " + nrParameter.DefaultValue, 0);
     }
 }
コード例 #3
0
 /// <summary>
 /// Visit a <see cref="NRParameter"/>.
 /// </summary>
 /// <param name="nrParameter">The <see cref="NRParameter"/> to visit.</param>
 public void Visit(NRParameter nrParameter)
 {
     OutputLine("NRParameter");
     indent++;
     nrParameter.Attributes.ForEach(nrAttribute => nrAttribute.Accept(this));
     OutputLine("DefaultValue: " + nrParameter.DefaultValue);
     OutputLine("Name: " + nrParameter.Name);
     OutputLine("ParameterModifier: " + nrParameter.ParameterModifier);
     if (nrParameter.Type != null)
     {
         OutputLine("Type:");
         indent++;
         nrParameter.Type.Accept(this);
         indent--;
     }
     OutputLine("TypeFullName: " + nrParameter.TypeFullName);
     indent--;
 }
コード例 #4
0
 public NRParameterDeclaration(NRParameter parameter)
 {
     this.parameter = parameter;
 }
コード例 #5
0
 /// <summary>
 /// Visit a <see cref="NRParameter"/>.
 /// </summary>
 /// <param name="nrParameter">The <see cref="NRParameter"/> to visit.</param>
 public void Visit(NRParameter nrParameter)
 {
     OutputLine(nrParameter.Declaration());
 }