Install-Package Microsoft.CodeAnalysis –Pre
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis;
SyntaxTree tree = CSharpSyntaxTree.ParseText(codeString);
var walker = new Walker(); walker.Visit(tree.GetRoot());
var compilation = CSharpCompilation.Create("1ccode").WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)).AddReferences(new MetadataFileReference(typeof(object).Assembly.Location)).AddSyntaxTrees(tree); var Model = compilation.GetSemanticModel(tree);
var classSymbol = Model.GetDeclaredSymbol(classDeclarationSyntax);
var type = type.BaseType;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis; namespace Roslyn { public class Walker : CSharpSyntaxWalker { SyntaxTree Tree { get; set; } CSharpCompilation Compilation { get; set; } SemanticModel Model { get; set; } TextWriter Writer { get; set; } public Walker(TextWriter writer, SyntaxTree tree, CSharpCompilation compilation) : base() { Writer = writer; Tree = tree; Compilation = compilation; Model = Compilation.GetSemanticModel(tree); } Dictionary<ClassDeclarationSyntax, FieldDeclarationSyntax[]> _classFields = new Dictionary<ClassDeclarationSyntax, FieldDeclarationSyntax[]>(); NamespaceDeclarationSyntax _currentNamespace; ClassDeclarationSyntax _currentClass; PropertyDeclarationSyntax _currentProperty; private int Tabs = 0; public override void Visit(SyntaxNode node) { //Tabs++; //var indents = new String('\t', Tabs); //Writer.WriteLine(indents + node.GetType().Name + "/" + node.CSharpKind()); base.Visit(node); //Tabs--; } public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) { _currentNamespace = node; Writer.WriteLine(" " + node.Name.ToString().Replace(".", "_")); base.VisitNamespaceDeclaration(node); } public override void VisitClassDeclaration(ClassDeclarationSyntax node) { _currentClass = node; var fields = node.ChildNodes().OfType<FieldDeclarationSyntax>().ToArray(); _classFields[node] = fields; Writer.WriteLine(); Writer.WriteLine(string.Format("// {0}", node.Identifier)); base.VisitClassDeclaration(node); DeclareBaseClassPropertiesToImplement(node); DeclareBaseClassMethodsToImplement(node); } void DeclareBaseClassMethodsToImplement(ClassDeclarationSyntax classNode) { var classSymbol = Model.GetDeclaredSymbol(classNode); List<string> processedMembers = new List<string>(); var type = classSymbol; while (type != null) { foreach(var member in type.GetMembers()) { var declarators = member.DeclaringSyntaxReferences; if (declarators == null || declarators.Length == 0) continue; if (declarators.Length != 1) throw new NotImplementedException(); var memberNode = declarators[0].GetSyntax() as MethodDeclarationSyntax; if (memberNode == null) continue; if (processedMembers.Any(m=>m == member.Name)) continue; processedMembers.Add(member.Name); if (type == classSymbol) //Skip original class members. Declare only base classes continue; Writer.WriteLine(); Writer.WriteLine(string.Format(" {0}_{1}(_this)", _currentClass.Identifier, memberNode.Identifier)); Writer.WriteLine(string.Format(" {0}_{1}(_this);", type.Name, member.Name)); Writer.WriteLine(string.Format(";")); } type = type.BaseType; } } void DeclareBaseClassPropertiesToImplement(ClassDeclarationSyntax classNode) { var classSymbol = Model.GetDeclaredSymbol(classNode); List<string> processedMembers = new List<string>(); var type = classSymbol; while (type != null) { foreach(var member in type.GetMembers()) { var declarators = member.DeclaringSyntaxReferences; if (declarators == null || declarators.Length == 0) continue; if (declarators.Length != 1) throw new NotImplementedException(); var memberNode = declarators[0].GetSyntax() as PropertyDeclarationSyntax; if (memberNode == null) continue; if (processedMembers.Any(m => m == memberNode.Identifier.ToString())) continue; processedMembers.Add(memberNode.Identifier.ToString()); if (type == classSymbol) //Skip original class members. Declare only base classes continue; Writer.WriteLine(); Writer.WriteLine(string.Format(" {0}__{1}(_this)", _currentClass.Identifier, memberNode.Identifier)); Writer.WriteLine(string.Format(" {0}__{1}(_this);", type.Name, member.Name)); Writer.WriteLine(string.Format(";")); Writer.WriteLine(); Writer.WriteLine(string.Format(" {0}__{1}(_this, value)", _currentClass.Identifier, memberNode.Identifier)); Writer.WriteLine(string.Format(" {0}__{1}(_this);", type.Name, member.Name)); Writer.WriteLine(string.Format(";")); } type = type.BaseType; } } public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node) { Writer.WriteLine(); var symbol = Model.GetDeclaredSymbol(node); List<string> parameters = new List<string>(); parameters.Add("_this"); parameters.AddRange(node.ParameterList.Parameters.Select(m => m.Identifier.ToString()).ToArray()); Writer.WriteLine(string.Format(" {0}({1}){2}", node.Identifier, String.Join(", ", parameters), " ")); Writer.WriteLine(); Tabs++; var indents = new String('\t', Tabs); //Initialize members first if no this constructor initializer (:this()) call if (!node.DescendantNodes().OfType<ConstructorInitializerSyntax>().Any(m=>m.CSharpKind() == SyntaxKind.ThisConstructorInitializer) && _classFields.ContainsKey(_currentClass)) { Writer.WriteLine(indents + String.Format("// ")); //Writer.WriteLine(String.Format("_this = ();")); foreach (var field in _classFields[_currentClass]) { Writer.WriteLine(String.Format(indents + "_this.(\"{0}\", {1})", field.Declaration.Variables[0].Identifier, field.Declaration.Variables[0].Initializer.Value)); } } if (node.Initializer != null) { List<string> arguments = new List<string>(); arguments.Add("_this"); arguments.AddRange(node.Initializer.ArgumentList.Arguments.Select(m => m.Expression.ToString()).ToArray()); if (node.Initializer.ThisOrBaseKeyword.CSharpKind() == SyntaxKind.BaseKeyword) { Writer.WriteLine(indents + String.Format("// ")); Writer.WriteLine(indents + String.Format("{0}({1});", _currentClass.BaseList.Types[0], String.Join(", ", arguments))); } else if (node.Initializer.CSharpKind() == SyntaxKind.ThisConstructorInitializer) { Writer.WriteLine(indents + String.Format("// ")); Writer.WriteLine(indents + String.Format("{0}({1});", _currentClass.Identifier, String.Join(", ", arguments))); } } Writer.WriteLine(String.Format(indents + "_this.(\"__type\", \"{0}.{1}\")", symbol.ContainingNamespace.Name, symbol.ContainingType.Name)); base.VisitConstructorDeclaration(node); Tabs--; Writer.WriteLine(indents + string.Format(" _this;")); Writer.WriteLine(string.Format("; //{0}({1}){2}", node.Identifier, String.Join(", ", parameters), " ")); } public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node) { _currentProperty = node; var symbol = Model.GetDeclaredSymbol(node); base.VisitPropertyDeclaration(node); } public override void VisitAccessorDeclaration(AccessorDeclarationSyntax node) { Writer.WriteLine(); if (node.CSharpKind() == SyntaxKind.GetAccessorDeclaration) { Writer.WriteLine(string.Format(" {0}__{1}(_this)", _currentClass.Identifier, _currentProperty.Identifier)); } else if (node.CSharpKind() == SyntaxKind.SetAccessorDeclaration) { Writer.WriteLine(string.Format(" {0}__{1}(_this, value)", _currentClass.Identifier, _currentProperty.Identifier)); } Tabs++; if (node.Body == null) { //auto implemented var indents = new String('\t', Tabs); if (node.CSharpKind() == SyntaxKind.GetAccessorDeclaration) { Writer.WriteLine(indents + string.Format(" _this._private_{0}_{1};", _currentClass.Identifier, _currentProperty.Identifier)); } else if (node.CSharpKind() == SyntaxKind.SetAccessorDeclaration) { Writer.WriteLine(indents + string.Format("_this._private_{0}_{1} = value;", _currentClass.Identifier, _currentProperty.Identifier)); } } base.VisitAccessorDeclaration(node); Tabs--; if (node.CSharpKind() == SyntaxKind.GetAccessorDeclaration) { Writer.WriteLine(string.Format(";")); } else if (node.CSharpKind() == SyntaxKind.SetAccessorDeclaration) { Writer.WriteLine(string.Format(";")); } } public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { Writer.WriteLine(); Writer.WriteLine(string.Format(" {0}_{1}(_this)", _currentClass.Identifier, node.Identifier)); Tabs++; base.VisitMethodDeclaration(node); Tabs--; Writer.WriteLine(string.Format(";")); } public override void VisitExpressionStatement(ExpressionStatementSyntax node) { var indents = new String('\t', Tabs); Writer.WriteLine(("\r\n" + node.ToString()).Replace("\r\n", "\r\n" + indents + "//")); base.VisitExpressionStatement(node); } public override void VisitReturnStatement(ReturnStatementSyntax node) { var indents = new String('\t', Tabs); Writer.WriteLine(("\r\n" + node.ToString()).Replace("\r\n", "\r\n" + indents + "//")); base.VisitReturnStatement(node); } //public override void VisitBlock(BlockSyntax node) //{ // Writer.WriteLine(node.ToString()); // base.VisitBlock(node); //} } }
namespace 1.2 { public class { public () { 1 = " "; } private int _1 = 10; public int 1 {get {return _1;} set {_1 = value;}} public string 1 {get; set;} public void 1() { 1 = "1"; } } public class : { private int _1 = 20; public () : base() { 1 = " "; 1(); } public (int i) : this() { 1 = " (int i)"; 1(); } } }
1_2 // (_this) // _this.("_1", 10) _this.("__type", "2.") //1 = " "; _this; ; //(_this) __1(_this) //return _1; ; __1(_this, value) //_1 = value; ; __1(_this) _this._private__1; ; __1(_this, value) _this._private__1 = value; ; _1(_this) //1 = "1"; ; // (_this) // _this.("_1", 20) // (_this); _this.("__type", "2.") //1 = " "; //1(); _this; ; //(_this) (_this, i) // (_this); _this.("__type", "2.") //1 = " (int i)"; //1(); _this; ; //(_this, i) __1(_this) __1(_this); ; __1(_this, value) __1(_this); ; __1(_this) __1(_this); ; __1(_this, value) __1(_this); ; _1(_this) _1(_this); ;
Source: https://habr.com/ru/post/245453/
All Articles