Skip to content

Commit cf215dd

Browse files
committed
add convert to csharp
1 parent 3ea9f24 commit cf215dd

11 files changed

Lines changed: 665 additions & 8 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
5+
static SyntaxTree GenerateProxyTree()
6+
{
7+
return SyntaxFactory.CompilationUnit()
8+
.WithMembers(
9+
SyntaxFactory.SingletonList<MemberDeclarationSyntax>(
10+
SyntaxFactory.GlobalStatement(
11+
SyntaxFactory.LocalFunctionStatement(
12+
SyntaxFactory.IdentifierName("opt"),
13+
SyntaxFactory.MissingToken(SyntaxKind.IdentifierToken))
14+
.WithModifiers(
15+
SyntaxFactory.TokenList(
16+
new[]{
17+
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
18+
SyntaxFactory.Token(SyntaxKind.StaticKeyword)}))
19+
.WithBody(
20+
SyntaxFactory.Block(
21+
SyntaxFactory.SingletonList<StatementSyntax>(
22+
SyntaxFactory.ReturnStatement(
23+
SyntaxFactory.LiteralExpression(
24+
SyntaxKind.StringLiteralExpression,
25+
SyntaxFactory.Literal("a")))))))))
26+
.NormalizeWhitespace().SyntaxTree;
27+
}
28+
29+
Console.WriteLine(GenerateProxyTree().ToString());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
12+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
13+
</ItemGroup>
14+
15+
</Project>

RoslynSyntaxTool.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30711.63
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32228.430
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoslynSyntaxTool", "RoslynSyntaxTool\RoslynSyntaxTool.csproj", "{E47C5908-E1AC-49D8-9B04-451D329CEEFE}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoslynSyntaxTool.Process", "RoslynSyntaxTool.Process\RoslynSyntaxTool.Process.csproj", "{A9FE95F2-0C82-4C79-987F-40B33F58C42D}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{E47C5908-E1AC-49D8-9B04-451D329CEEFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{E47C5908-E1AC-49D8-9B04-451D329CEEFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{E47C5908-E1AC-49D8-9B04-451D329CEEFE}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{A9FE95F2-0C82-4C79-987F-40B33F58C42D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{A9FE95F2-0C82-4C79-987F-40B33F58C42D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{A9FE95F2-0C82-4C79-987F-40B33F58C42D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{A9FE95F2-0C82-4C79-987F-40B33F58C42D}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

RoslynSyntaxTool/MainWindow.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
99
mc:Ignorable="d"
1010
WindowState="Maximized"
11-
Title="Roslyn Syntax Tool 版本1.0" Height="720" Width="1080">
11+
Title="Roslyn Syntax Tool 版本1.1" Height="720" Width="1080">
1212
<mah:MetroWindow.RightWindowCommands>
1313
<mah:WindowCommands>
1414
<Button Content="关于" Click="ButtonBase_OnClick"></Button>
@@ -141,9 +141,10 @@
141141
<ContentControl Grid.Row="1" Focusable="False" >
142142

143143
<Frame x:Name="MainFrame"
144+
144145
Navigated="MainFrame_OnNavigated"
145146
Source="{Binding ElementName=HamburgerMenuControl,Path=SelectedItem.Tag}"
146-
NavigationUIVisibility="Visible" />
147+
NavigationUIVisibility="Hidden" />
147148

148149

149150
</ContentControl>
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
10+
namespace Workshop.Manager
11+
{
12+
public static class CompilationUtilitys
13+
{
14+
#region Public Method
15+
16+
public static MemoryStream CompileClientProxy(IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> references)
17+
{
18+
#if !NET
19+
var assemblys = new[]
20+
{
21+
"System.Runtime",
22+
"mscorlib",
23+
"System.Threading.Tasks",
24+
"System.Collections"
25+
};
26+
references = assemblys.Select(i => MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName(i)).Location)).Concat(references).Distinct();
27+
#endif
28+
references = new[]
29+
{
30+
MetadataReference.CreateFromFile(typeof(SyntaxTree).GetTypeInfo().Assembly.Location),
31+
MetadataReference.CreateFromFile(typeof(StatementSyntax).GetTypeInfo().Assembly.Location),
32+
MetadataReference.CreateFromFile(typeof(SyntaxFactory).GetTypeInfo().Assembly.Location)
33+
}.Concat(references).Distinct();
34+
return Compile(AssemblyInfo.Create("RoslynSyntaxTool.Proxy.ConvertToCSharpProxy"), trees, references);
35+
}
36+
37+
public static MemoryStream Compile(AssemblyInfo assemblyInfo, IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> references)
38+
{
39+
return Compile(assemblyInfo.Title, assemblyInfo, trees, references);
40+
}
41+
42+
public static MemoryStream Compile(string assemblyName, AssemblyInfo assemblyInfo, IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> references)
43+
{
44+
trees = trees.Concat(new[] { GetAssemblyInfo(assemblyInfo) });
45+
var compilation = CSharpCompilation.Create(assemblyName, trees, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
46+
var stream = new MemoryStream();
47+
var result = compilation.Emit(stream);
48+
if (!result.Success)
49+
{
50+
foreach (var message in result.Diagnostics.Select(i => i.ToString()))
51+
{
52+
System.Console.WriteLine(message);
53+
}
54+
return null;
55+
}
56+
stream.Seek(0, SeekOrigin.Begin);
57+
return stream;
58+
}
59+
60+
#endregion Public Method
61+
62+
#region Private Method
63+
64+
private static SyntaxTree GetAssemblyInfo(AssemblyInfo info)
65+
{
66+
return SyntaxFactory.CompilationUnit()
67+
.WithUsings(
68+
SyntaxFactory.List(
69+
new[]
70+
{
71+
SyntaxFactory.UsingDirective(
72+
SyntaxFactory.QualifiedName(
73+
SyntaxFactory.IdentifierName("System"),
74+
SyntaxFactory.IdentifierName("Reflection"))),
75+
SyntaxFactory.UsingDirective(
76+
SyntaxFactory.QualifiedName(
77+
SyntaxFactory.QualifiedName(
78+
SyntaxFactory.IdentifierName("System"),
79+
SyntaxFactory.IdentifierName("Runtime")),
80+
SyntaxFactory.IdentifierName("InteropServices"))),
81+
SyntaxFactory.UsingDirective(
82+
SyntaxFactory.QualifiedName(
83+
SyntaxFactory.QualifiedName(
84+
SyntaxFactory.IdentifierName("System"),
85+
SyntaxFactory.IdentifierName("Runtime")),
86+
SyntaxFactory.IdentifierName("Versioning")))
87+
}))
88+
.WithAttributeLists(
89+
SyntaxFactory.List(
90+
new[]
91+
{
92+
SyntaxFactory.AttributeList(
93+
SyntaxFactory.SingletonSeparatedList(
94+
SyntaxFactory.Attribute(
95+
SyntaxFactory.IdentifierName("TargetFramework"))
96+
.WithArgumentList(
97+
SyntaxFactory.AttributeArgumentList(
98+
SyntaxFactory.SeparatedList<AttributeArgumentSyntax>(
99+
new SyntaxNodeOrToken[]{
100+
SyntaxFactory.AttributeArgument(
101+
SyntaxFactory.LiteralExpression(
102+
SyntaxKind.StringLiteralExpression,
103+
SyntaxFactory.Literal("net6.0"))),
104+
SyntaxFactory.Token(SyntaxKind.CommaToken),
105+
SyntaxFactory.AttributeArgument(
106+
SyntaxFactory.LiteralExpression(
107+
SyntaxKind.StringLiteralExpression,
108+
SyntaxFactory.Literal("net6.0")))
109+
.WithNameEquals(
110+
SyntaxFactory.NameEquals(
111+
SyntaxFactory.IdentifierName("FrameworkDisplayName")))})))))
112+
.WithTarget(
113+
SyntaxFactory.AttributeTargetSpecifier(
114+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
115+
SyntaxFactory.AttributeList(
116+
SyntaxFactory.SingletonSeparatedList(
117+
SyntaxFactory.Attribute(
118+
SyntaxFactory.IdentifierName("AssemblyTitle"))
119+
.WithArgumentList(
120+
SyntaxFactory.AttributeArgumentList(
121+
SyntaxFactory.SingletonSeparatedList(
122+
SyntaxFactory.AttributeArgument(
123+
SyntaxFactory.LiteralExpression(
124+
SyntaxKind.StringLiteralExpression,
125+
SyntaxFactory.Literal(info.Title))))))))
126+
.WithTarget(
127+
SyntaxFactory.AttributeTargetSpecifier(
128+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
129+
SyntaxFactory.AttributeList(
130+
SyntaxFactory.SingletonSeparatedList(
131+
SyntaxFactory.Attribute(
132+
SyntaxFactory.IdentifierName("AssemblyProduct"))
133+
.WithArgumentList(
134+
SyntaxFactory.AttributeArgumentList(
135+
SyntaxFactory.SingletonSeparatedList(
136+
SyntaxFactory.AttributeArgument(
137+
SyntaxFactory.LiteralExpression(
138+
SyntaxKind.StringLiteralExpression,
139+
SyntaxFactory.Literal(info.Product))))))))
140+
.WithTarget(
141+
SyntaxFactory.AttributeTargetSpecifier(
142+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
143+
SyntaxFactory.AttributeList(
144+
SyntaxFactory.SingletonSeparatedList(
145+
SyntaxFactory.Attribute(
146+
SyntaxFactory.IdentifierName("AssemblyCopyright"))
147+
.WithArgumentList(
148+
SyntaxFactory.AttributeArgumentList(
149+
SyntaxFactory.SingletonSeparatedList(
150+
SyntaxFactory.AttributeArgument(
151+
SyntaxFactory.LiteralExpression(
152+
SyntaxKind.StringLiteralExpression,
153+
SyntaxFactory.Literal(info.Copyright))))))))
154+
.WithTarget(
155+
SyntaxFactory.AttributeTargetSpecifier(
156+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
157+
SyntaxFactory.AttributeList(
158+
SyntaxFactory.SingletonSeparatedList(
159+
SyntaxFactory.Attribute(
160+
SyntaxFactory.IdentifierName("ComVisible"))
161+
.WithArgumentList(
162+
SyntaxFactory.AttributeArgumentList(
163+
SyntaxFactory.SingletonSeparatedList(
164+
SyntaxFactory.AttributeArgument(
165+
SyntaxFactory.LiteralExpression(info.ComVisible
166+
? SyntaxKind.TrueLiteralExpression
167+
: SyntaxKind.FalseLiteralExpression)))))))
168+
.WithTarget(
169+
SyntaxFactory.AttributeTargetSpecifier(
170+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
171+
SyntaxFactory.AttributeList(
172+
SyntaxFactory.SingletonSeparatedList(
173+
SyntaxFactory.Attribute(
174+
SyntaxFactory.IdentifierName("Guid"))
175+
.WithArgumentList(
176+
SyntaxFactory.AttributeArgumentList(
177+
SyntaxFactory.SingletonSeparatedList(
178+
SyntaxFactory.AttributeArgument(
179+
SyntaxFactory.LiteralExpression(
180+
SyntaxKind.StringLiteralExpression,
181+
SyntaxFactory.Literal(info.Guid))))))))
182+
.WithTarget(
183+
SyntaxFactory.AttributeTargetSpecifier(
184+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
185+
SyntaxFactory.AttributeList(
186+
SyntaxFactory.SingletonSeparatedList(
187+
SyntaxFactory.Attribute(
188+
SyntaxFactory.IdentifierName("AssemblyVersion"))
189+
.WithArgumentList(
190+
SyntaxFactory.AttributeArgumentList(
191+
SyntaxFactory.SingletonSeparatedList(
192+
SyntaxFactory.AttributeArgument(
193+
SyntaxFactory.LiteralExpression(
194+
SyntaxKind.StringLiteralExpression,
195+
SyntaxFactory.Literal(info.Version))))))))
196+
.WithTarget(
197+
SyntaxFactory.AttributeTargetSpecifier(
198+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword))),
199+
SyntaxFactory.AttributeList(
200+
SyntaxFactory.SingletonSeparatedList(
201+
SyntaxFactory.Attribute(
202+
SyntaxFactory.IdentifierName("AssemblyFileVersion"))
203+
.WithArgumentList(
204+
SyntaxFactory.AttributeArgumentList(
205+
SyntaxFactory.SingletonSeparatedList(
206+
SyntaxFactory.AttributeArgument(
207+
SyntaxFactory.LiteralExpression(
208+
SyntaxKind.StringLiteralExpression,
209+
SyntaxFactory.Literal(info.FileVersion))))))))
210+
.WithTarget(
211+
SyntaxFactory.AttributeTargetSpecifier(
212+
SyntaxFactory.Token(SyntaxKind.AssemblyKeyword)))
213+
}))
214+
.NormalizeWhitespace()
215+
.SyntaxTree;
216+
}
217+
218+
#endregion Private Method
219+
220+
#region Help Class
221+
222+
public class AssemblyInfo
223+
{
224+
public string Title { get; set; }
225+
public string Product { get; set; }
226+
public string Copyright { get; set; }
227+
public string Guid { get; set; }
228+
public string Version { get; set; }
229+
public string FileVersion { get; set; }
230+
public bool ComVisible { get; set; }
231+
232+
public static AssemblyInfo Create(string name, string copyright = "Copyright © MatoApp", string version = "0.0.0.1")
233+
{
234+
return new AssemblyInfo
235+
{
236+
Title = name,
237+
Product = name,
238+
Copyright = copyright,
239+
Guid = System.Guid.NewGuid().ToString("D"),
240+
ComVisible = false,
241+
Version = version,
242+
FileVersion = version
243+
};
244+
}
245+
}
246+
247+
#endregion Help Class
248+
}
249+
}

RoslynSyntaxTool/RoslynSyntaxTool.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<Compile Update="View\ErrorPage.xaml.cs">
6161
<DependentUpon>ErrorPage.xaml</DependentUpon>
6262
</Compile>
63+
<Compile Update="View\IndexPage - 复制.xaml.cs">
64+
<DependentUpon>IndexPage.xaml</DependentUpon>
65+
</Compile>
6366
<Compile Update="View\IndexPage.xaml.cs">
6467
<DependentUpon>IndexPage.xaml</DependentUpon>
6568
</Compile>
@@ -91,6 +94,10 @@
9194
<Page Update="View\ErrorPage.xaml">
9295
<SubType>Designer</SubType>
9396
</Page>
97+
<Page Update="View\ConvertToCSharpPage.xaml">
98+
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
99+
<SubType>Designer</SubType>
100+
</Page>
94101
<Page Update="View\IndexPage.xaml">
95102
<SubType>Designer</SubType>
96103
</Page>

0 commit comments

Comments
 (0)