public void TestBuilder4() { OopBuilder builder = new OopBuilder(); var script = builder .Access(AccessFlags.Public) .Modifier(ModifierFlags.Static) .Class() .CustomUsing() .Using <OopTemplateTest>() .Using <int>() .InheritanceAppend <int>() .Namespace("TestNamespace") .Name("TestUt1<T>") .Constraint(item => item .SetType("T") .Constraint(ConstraintFlags.New) .Constraint <Int32>() .Constraint(typeof(string)) .Constraint(ConstraintFlags.Class)) .Body("public static void Test(){}") .Script; Assert.Equal($@"using NatashaUT;{Environment.NewLine}using System;{Environment.NewLine}namespace TestNamespace{{public static class TestUt1<T> : System.Int32 where T : class,new(),System.Int32,System.String{{{Environment.NewLine}public static void Test(){{}}}}}}", script); }
public void TestBuilder2() { OopBuilder builder = new OopBuilder(); var script = builder .Namespace("TestNamespace") .ChangeToStruct() .OopAccess(AccessTypes.Private) .OopName("TestUt2") .OopBody(@"public static void Test(){}") .PublicStaticField <string>("Name") .PrivateStaticField <int>("_age") .Builder().Script; Assert.Equal(@"using System;namespace TestNamespace{private struct TestUt2{public static String Name;private static Int32 _age;public static void Test(){}}}", script); }
public void TestBuilder1() { OopBuilder builder = new OopBuilder(); var script = builder .Using <DynamicBuilderTest>() .Namespace("TestNamespace") .OopAccess(AccessTypes.Public) .OopModifier(Modifiers.Static) .OopName("TestUt1") .OopBody(@"public static void Test(){}") .PublicStaticField <string>("Name") .PrivateStaticField <int>("_age") .Builder() .Script; Assert.Equal(@"using NatashaUT;using System;namespace TestNamespace{public static class TestUt1{public static String Name;private static Int32 _age;public static void Test(){}}}", script); Assert.Equal("TestUt1", builder.GetType().Name); }
public void TestBuilder3() { OopBuilder builder = new OopBuilder(); var script = builder .Access(AccessFlags.Public) .Modifier(ModifierFlags.Static) .Class() .CustomUsing() .Using <OopTemplateTest>() .Using <int>() .InheritanceAppend <int>() .Namespace("TestNamespace") .Name("TestUt1<T>") .ConstraintAppendFrom(typeof(InOutInterfaceT <,>)) .Body("public static void Test(){}") .Script; Assert.Equal($@"using NatashaUT;{Environment.NewLine}using System;{Environment.NewLine}namespace TestNamespace{{public static class TestUt1<T> : System.Int32 where T : notnull, NatashaUT.Model.G2, NatashaUT.Model.G3, NatashaUT.Model.G4, new() where S : NatashaUT.Model.G2, NatashaUT.Model.G3, NatashaUT.Model.G4, new() {{{Environment.NewLine}public static void Test(){{}}}}}}", script); }
public void Test1() { OopBuilder builder = new OopBuilder(); builder .Using <DynamicBuilderTest>() .Namespace("TestNamespace") .OopAccess(AccessTypes.Public) .OopModifier(Modifiers.Static) .OopName("TestExceptionUt1") .OopBody(@"public static void 1 Test(){}") .PublicStaticField <string>("Name") .PrivateStaticField <int>("_age") .Builder(); var type = builder.GetType(); Assert.Null(type); Assert.Equal(ComplieError.Assembly, builder.Complier.Exception.ErrorFlag); }
public void TestBuilder1() { OopBuilder builder = new OopBuilder(); var script = builder .Access(AccessFlags.Public) .Modifier(ModifierFlags.Static) .Class() .CustomUsing() .Using <OopTemplateTest>() .Using <int>() .Inheritance <int>() .Namespace("TestNamespace") .Name("TestUt1<T>") .Constraint("where T : class") .Body("public static void Test(){}") .Script; Assert.Equal($@"using NatashaUT;{Environment.NewLine}using System;{Environment.NewLine}namespace TestNamespace{{public static class TestUt1<T> : System.Int32 where T : class{{{Environment.NewLine}public static void Test(){{}}}}}}", script); }
public void TestBuilder3() { OopBuilder builder = new OopBuilder(); var script = builder .Namespace <string>() .OopAccess("") .OopName("TestUt3") .ChangeToInterface() .Ctor(item => item .MemberAccess("public") .Param <string>("name") .Body("this.Name=name;")) .OopBody(@"public static void Test(){}") .PublicStaticField <string>("Name") .PrivateStaticField <int>("_age") .Builder().Script; Assert.Equal(@"using System;namespace System{interface TestUt3{public static String Name;private static Int32 _age;public static void Test(){}public TestUt3(String name){this.Name=name;}}}", script); }
public void TestBuilder2() { OopBuilder builder = new OopBuilder(); var script = builder .Access(AccessFlags.Public) .Modifier(ModifierFlags.Static) .Class() .CustomUsing() .Using <OopTemplateTest>() .Using <int>() .InheritanceAppend <int>() .Namespace("TestNamespace") .Name("TestUt1<T>") .ConstraintAppendFrom(typeof(List <>)) .Body("public static void Test(){}") .Script; #if NET5_0_OR_GREATER Assert.Equal($@"using NatashaUT;{Environment.NewLine}using System;{Environment.NewLine}namespace TestNamespace{{public static class TestUt1<T> : System.Int32 where T : notnull {{{Environment.NewLine}public static void Test(){{}}}}}}", script); #else Assert.Equal($@"using NatashaUT;{Environment.NewLine}using System;{Environment.NewLine}namespace TestNamespace{{public static class TestUt1<T> : System.Int32 {{{Environment.NewLine}public static void Test(){{}}}}}}", script); #endif }
public static T WithCS0104Handler <T>(this OopBuilder <T> builder) where T : OopBuilder <T>, new() { builder.AssemblyBuilder.AddSemanticAnalysistor(AmbiguityUsings.OopBuilderCreator(builder)); return(builder.Link !); }
internal static Func <CSharpCompilation, CSharpCompilation> OopBuilderCreator <T>(OopBuilder <T> oopBuilder) where T : OopBuilder <T>, new() { return(compilation => { var trees = compilation.SyntaxTrees; foreach (var tree in trees) { var semantiModel = compilation.GetSemanticModel(tree); var errors = semantiModel.GetDiagnostics(); CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); var editor = new SyntaxEditor(root, new AdhocWorkspace()); var removeCache = new HashSet <UsingDirectiveSyntax>(); var usings = oopBuilder.Usings; foreach (var diagnostic in errors) { if (diagnostic.Id == "CS0104") { (string str1, string str2) = CS0104Analaysistor.GetUnableUsing(diagnostic); var needToRemove = str1; if (usings.Contains(str1)) { if (usings.Contains(str2)) { if (str2 == "System") { needToRemove = str2; } else { needToRemove = str1; } } else { needToRemove = str2; } } else { needToRemove = str1; } removeCache.UnionWith(from usingDeclaration in diagnostic.Location.SourceTree.GetRoot() .DescendantNodes() .OfType <UsingDirectiveSyntax>() where usingDeclaration.Name.ToFullString() == needToRemove select usingDeclaration); } } foreach (var item in removeCache) { editor.RemoveNode(item); } compilation = compilation.ReplaceSyntaxTree(tree, editor.GetChangedRoot().SyntaxTree); } _usingsCache.Remove(compilation); return compilation; }); }
internal static Func <AssemblyCSharpBuilder, CSharpCompilation, CSharpCompilation> OopBuilderCreator <T>(OopBuilder <T> oopBuilder) where T : OopBuilder <T>, new() { return((builder, compilation) => { var trees = compilation.SyntaxTrees; foreach (var tree in trees) { compilation = HandlerCS0104(tree, compilation, oopBuilder.UsingRecorder); } return compilation; }); }