Esempio n. 1
0
        /// <summary>
        /// 构建方法
        /// </summary>
        /// <returns></returns>
        public MethodDeclarationSyntax BuildSyntax()
        {
            var code = $@"class Test66666666
{{
                {ToFullCode()}
}}";
            MethodDeclarationSyntax memberDeclaration = default;
            var syntaxNodes = CSharpSyntaxTree.ParseText(code)
                              .GetRoot()
                              .DescendantNodes();

            memberDeclaration = syntaxNodes
                                .OfType <MethodDeclarationSyntax>().FirstOrDefault();

            if (memberDeclaration is null)
            {
                throw new InvalidOperationException("未能构建方法,请检查代码是否有语法错误!");
            }

            // 添加特性
            if (_member.Atributes.Count != 0)
            {
                var tmp = CodeSyntax.CreateAttributeList(_member.Atributes.ToArray());
                memberDeclaration = memberDeclaration.WithAttributeLists(tmp);
            }

            return(memberDeclaration);
        }
Esempio n. 2
0
        /// <summary>
        /// 通过代码直接生成
        /// </summary>
        /// <param name="Code">字段</param>
        /// <param name="attrs">特性注解列表</param>
        /// <returns></returns>
        public static FieldDeclarationSyntax BuildSyntax(string Code, string[] attrs = null)
        {
            var syntaxNodes = CSharpSyntaxTree.ParseText(Code).GetRoot().DescendantNodes();
            FieldDeclarationSyntax memberDeclaration = syntaxNodes
                                                       .OfType <FieldDeclarationSyntax>()
                                                       .FirstOrDefault();

            if (memberDeclaration == null)
            {
                memberDeclaration = SyntaxFactory.FieldDeclaration(syntaxNodes.OfType <VariableDeclarationSyntax>().FirstOrDefault());
            }

            if (memberDeclaration is null)
            {
                throw new InvalidOperationException("未能构建字段,请检查代码是否有语法错误!");
            }


            if (attrs != null)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(attrs));
            }

            return(memberDeclaration);
        }
        /// <summary>
        /// 生成语法树
        /// </summary>
        /// <returns></returns>
        public PropertyDeclarationSyntax BuildSyntax()
        {
            //if (_base.UseCode)
            //    return SyntaxFactory.PropertyDeclaration(BuildCodeSyntax().Declaration);

            PropertyDeclarationSyntax memberDeclaration;
            var syntaxNodes = CSharpSyntaxTree.ParseText(ToFullCode())
                              .GetRoot()
                              .DescendantNodes();

            foreach (var item in syntaxNodes)
            {
                Console.WriteLine(item.NormalizeWhitespace().ToFullString());
            }

            memberDeclaration = syntaxNodes
                                .OfType <PropertyDeclarationSyntax>()
                                .Single();

            // 添加特性

            if (!_base.UseCode)
            {
                if (_member.Atributes.Count != 0)
                {
                    var tmp = CodeSyntax.CreateAttributeList(_member.Atributes.ToArray());
                    memberDeclaration = memberDeclaration.WithAttributeLists(tmp);
                }
            }

            return(memberDeclaration);
        }
        public EventFieldDeclarationSyntax BuildSyntax()
        {
            var syntaxNodes = CSharpSyntaxTree.ParseText(ToFullCode())
                              .GetRoot()
                              .DescendantNodes();

            EventFieldDeclarationSyntax memberDeclaration;

            memberDeclaration = syntaxNodes
                                .OfType <EventFieldDeclarationSyntax>()
                                .FirstOrDefault();

            if (memberDeclaration is null)
            {
                throw new InvalidOperationException("无法构建事件,请检查代码语法是否有错误!");
            }


            if (_member.Atributes.Count != 0)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(_member.Atributes.ToArray()));
            }
            return(memberDeclaration);
        }
Esempio n. 5
0
        public static ConstructorDeclarationSyntax BuildSyntax(string Code, string[] attrs = null)
        {
            var syntaxNodes = CSharpSyntaxTree.ParseText(Code).GetRoot().DescendantNodes();
            ConstructorDeclarationSyntax memberDeclaration = syntaxNodes
                                                             .OfType <ConstructorDeclarationSyntax>()
                                                             .FirstOrDefault();

            if (attrs != null)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(attrs));
            }

            return(memberDeclaration);
        }
Esempio n. 6
0
        /// <summary>
        /// 生成语法树
        /// </summary>
        /// <returns></returns>
        public EnumDeclarationSyntax BuildSyntax()
        {
            var syntaxs = CSharpSyntaxTree.ParseText(ToFullCode())
                          .GetRoot()
                          .DescendantNodes();

            var memberDeclaration = syntaxs.OfType <EnumDeclarationSyntax>().Single();

            if (_member.Atributes.Count != 0)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(_member.Atributes.ToArray()));
            }
            return(memberDeclaration);
        }
        /// <summary>
        /// 字符串直接生成事件
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="attrs"></param>
        /// <returns></returns>
        public static EventFieldDeclarationSyntax BuildSyntax(string Code, string[] attrs = null)
        {
            var syntaxs = CSharpSyntaxTree.ParseText(Code)
                          .GetRoot()
                          .DescendantNodes();

            var memberDeclaration = SyntaxFactory.EventFieldDeclaration(syntaxs.OfType <VariableDeclarationSyntax>().Single());

            if (attrs != null)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(attrs));
            }
            return(memberDeclaration);
        }
        /// <summary>
        /// 通过字符串代码直接生成方法
        /// <example>
        /// <code>
        /// public int A 
        /// { 
        ///   get{return B;}
        ///   set{B = value;}
        ///   }
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="attrs"></param>
        /// <returns></returns>
        public static MethodDeclarationSyntax BuildSyntax(string Code, string[] attrs = null)
        {
            MethodDeclarationSyntax memberDeclaration;
            var syntaxNodes = CSharpSyntaxTree.ParseText(Code)
                .GetRoot()
                .DescendantNodes();
            memberDeclaration = syntaxNodes
            .OfType<MethodDeclarationSyntax>()
            .Single();

            if (attrs != null)
                memberDeclaration = memberDeclaration
                    .WithAttributeLists(CodeSyntax.CreateAttributeList(attrs));

            return memberDeclaration;
        }
Esempio n. 9
0
        /// <summary>
        /// 通过字符串代码生成类
        /// </summary>
        /// <param name="Code">字符串代码</param>
        /// <param name="attrs">类型的特性注解</param>
        /// <returns></returns>
        public static ClassDeclarationSyntax Build(string Code, string[] attrs = null)
        {
            ClassDeclarationSyntax memberDeclaration;

            memberDeclaration = CSharpSyntaxTree.ParseText(Code)
                                .GetRoot()
                                .DescendantNodes()
                                .OfType <ClassDeclarationSyntax>()
                                .Single();

            if (attrs != null)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(attrs));
            }

            return(memberDeclaration);
        }
Esempio n. 10
0
        /// <summary>
        /// 获取字段语法树
        /// <para>如果使用字符串代码一次性构建,请使用 <see cref="BuildCodeSyntax()"/> 方法</para>
        /// </summary>
        /// <returns></returns>
        public FieldDeclarationSyntax BuildSyntax()
        {
            if (_base.UseCode)
            {
                return(SyntaxFactory.FieldDeclaration(BuildCodeSyntax().Declaration));
            }


            bool isCanCreate = string.IsNullOrEmpty(_member.Access)?false:true;

            FieldDeclarationSyntax memberDeclaration = default;
            var syntaxNodes = CSharpSyntaxTree.ParseText(ToFullAttriCode())
                              .GetRoot()
                              .DescendantNodes();

            if (isCanCreate)
            {
                memberDeclaration = syntaxNodes
                                    .OfType <FieldDeclarationSyntax>()
                                    .FirstOrDefault();
            }
            else
            {
                memberDeclaration = SyntaxFactory.FieldDeclaration(syntaxNodes.OfType <VariableDeclarationSyntax>().FirstOrDefault());
            }


            if (memberDeclaration is null)
            {
                throw new InvalidOperationException("未能构建字段,请检查代码是否有语法错误!");
            }


            if (_member.Atributes.Count != 0)
            {
                var tmp = CodeSyntax.CreateAttributeList(_member.Atributes.ToArray());
                memberDeclaration = memberDeclaration.WithAttributeLists(tmp);
            }


            return(memberDeclaration);
        }
Esempio n. 11
0
        /// <summary>
        /// 通过字符串直接生成委托
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="attrs"></param>
        /// <returns></returns>
        /// <example>
        /// <code>
        /// var tmp = "public delegate void Test(string a,string b)";
        /// Build(tmp);
        /// </code>
        /// </example>
        public static DelegateDeclarationSyntax BuildSyntax(string Code, string[] attrs = null)
        {
            var memberDeclaration = CSharpSyntaxTree.ParseText(Code)
                                    .GetRoot()
                                    .DescendantNodes()
                                    .OfType <DelegateDeclarationSyntax>().FirstOrDefault();

            if (memberDeclaration is null)
            {
                throw new InvalidOperationException("请检查代码语法是否有错误!");
            }

            if (attrs != null)
            {
                memberDeclaration = memberDeclaration
                                    .WithAttributeLists(CodeSyntax.CreateAttributeList(attrs));
            }

            return(memberDeclaration);
        }
Esempio n. 12
0
        /// <summary>
        /// 构建委托
        /// </summary>
        /// <returns></returns>
        public DelegateDeclarationSyntax BuildSyntax()
        {
            DelegateDeclarationSyntax memberDeclaration = default;
            var syntaxNodes = CSharpSyntaxTree.ParseText(ToFullCode())
                              .GetRoot()
                              .DescendantNodes();

            memberDeclaration = syntaxNodes
                                .OfType <DelegateDeclarationSyntax>().FirstOrDefault();

            if (memberDeclaration is null)
            {
                throw new InvalidOperationException("请检查代码语法是否有错误!");
            }

            // 添加特性
            if (_member.Atributes.Count != 0)
            {
                var tmp = CodeSyntax.CreateAttributeList(_member.Atributes.ToArray());
                memberDeclaration = memberDeclaration.WithAttributeLists(tmp);
            }

            return(memberDeclaration);
        }
Esempio n. 13
0
 /// <summary>
 /// 构建特性注解
 /// </summary>
 /// <returns></returns>
 public AttributeListSyntax BuildAttributeListSyntax()
 {
     return(CodeSyntax.CreateAttributeList(BuildSyntax()));
 }
Esempio n. 14
0
 /// <summary>
 /// 构建 AttributeSyntax
 /// </summary>
 /// <returns></returns>
 public AttributeSyntax BuildSyntax()
 {
     return(CodeSyntax.CreateCodeAttribute(ToFullCode()));
 }