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 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); }
internal static Func <CSharpCompilation, CSharpCompilation> NDelegateCreator(NDelegate nDelegate) { 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 = nDelegate.Usings; OopBuilder oopBuilder = new OopBuilder(); oopBuilder.Using(usings); var usingSets = oopBuilder.Usings; foreach (var diagnostic in errors) { if (diagnostic.Id == "CS0104") { (string str1, string str2) = CS0104Analaysistor.GetUnableUsing(diagnostic); var needToRemove = str1; if (usingSets.Contains(str1)) { if (usingSets.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; }); }