/// <summary>
        /// 字符串生成特性
        /// </summary>
        /// <param name="attrCode"></param>
        /// <returns></returns>
        /// <example>
        /// <code>
        /// "[Display(Name = \"a\")]"
        /// </code>
        /// </example>
        public static AttributeListSyntax CreateAttributeList(string attrCode)
        {
            var result = CodeSyntax.CreateAttribute(attrCode);

            return(SyntaxFactory.AttributeList(
                       SyntaxFactory.SingletonSeparatedList <AttributeSyntax>(result.BuildSyntax())));
        }
Beispiel #2
0
        public void 特性_T1()
        {
            AttributeBuilder builder = CodeSyntax.CreateAttribute("Key");

            var result = builder.ToFormatCode();

#if Log
            _tempOutput.WriteLine(result.WithUnixEOL());
#endif
            Assert.Equal("Key", result.WithUnixEOL());
        }
Beispiel #3
0
        public void 特性_T3()
        {
            AttributeBuilder builder = CodeSyntax.CreateAttribute("DisplayName")
                                       .WithProperty(new string[] { "Name = \"a\"" });

            var result = builder.ToFormatCode();

#if Log
            _tempOutput.WriteLine(result.WithUnixEOL());
#endif
            Assert.Equal(@"DisplayName(Name = ""a"")", result.WithUnixEOL());
        }
Beispiel #4
0
        public void 特性_T2()
        {
            AttributeBuilder builder = CodeSyntax.CreateAttribute("DisplayName")
                                       .WithCtor("666");

            var result = builder.ToFormatCode();

#if Log
            _tempOutput.WriteLine(result.WithUnixEOL());
#endif
            Assert.Equal("DisplayName(666)", result.WithUnixEOL());
        }
        /// <summary>
        /// 字符串生成特性列表
        /// <para>
        /// <example>
        /// <code>
        /// string[] code = new string[]
        /// {
        ///     "[Display(Name = \"a\")]",
        ///     "[Display(Name = \"b\")]"
        /// };
        /// Cbuilder.reateAttributeList(code);
        /// </code>
        /// </example>
        /// </para>
        /// </summary>
        /// <param name="attrCode"></param>
        /// <returns></returns>
        public static SyntaxList <AttributeListSyntax> CreateAttributeList(params string[] attrsCode)
        {
            List <AttributeListSyntax> syntaxes = new List <AttributeListSyntax>();

            foreach (var item in attrsCode)
            {
                var tmp = CodeSyntax.CreateAttribute(item);
                syntaxes.Add(
                    SyntaxFactory.AttributeList(
                        SyntaxFactory.SingletonSeparatedList <AttributeSyntax>(tmp.BuildSyntax())));
            }

            return(SyntaxFactory.List <AttributeListSyntax>(syntaxes.ToArray()));
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            AttributeBuilder builder = CodeSyntax.CreateAttribute("Key");

            var result = builder.ToFormatCode();

            Console.WriteLine(result);
            //var a = DependencyContext.Default.CompileLibraries;
            //var b = a.Count;

            //foreach (var item in DependencyContext.Default.CompileLibraries.SelectMany(cl=>cl.ResolveReferencePaths()))
            //{
            //    Console.WriteLine(item);
            //}

            //GenericBuilder generic = new GenericBuilder();
            //generic.AddConstarint(new GenericScheme("T1", GenericConstraintsType.Struct));
            //generic.AddConstarint(new GenericScheme("T2", GenericConstraintsType.Class));
            //generic.AddConstarint(new GenericScheme("T3", GenericConstraintsType.Notnull));
            //generic.AddConstarint(new GenericScheme("T4", GenericConstraintsType.Unmanaged));
            //generic.AddConstarint(new GenericScheme("T5", GenericConstraintsType.New));
            //// 如果能够反射拿到 Type
            //generic.AddConstarint(new GenericScheme("T6", GenericConstraintsType.BaseClass, typeof(int)));
            //// 如果要以字符串定义基类类型,请使用 此API
            //generic.AddBaseClassConstarint("T7", " IEnumerable<int>");
            //generic.AddTUConstarint("T8", "T2");
            //generic.AddConstarint(new GenericScheme("T9", GenericConstraintsType.Class, GenericConstraintsType.New));
            //var syntax = generic.Build();
            //var result = syntax.ToFullString();
            //Console.WriteLine(result);

            //ClassBuilder buidler = new ClassBuilder();
            //var build = buidler.SetVisibility(ClassVisibilityType.Public)
            //    .SetName("Test")
            //    .AddMethodMember(b =>
            //    {
            //        b.SetVisibility(MemberVisibilityType.Public)
            //        .SetRondomName()
            //        .SetBlock("System.Console.WriteLine(\"111\");");
            //    })
            //    .Build();

            //CompilationBuilder compilation = new CompilationBuilder();
            //compilation.Test(build);
            Console.ReadKey();
        }