private void Form1_Load(object sender, EventArgs e)
{
//引用System.Deisgn.dll
DesignSurface ds = new DesignSurface();
//开始加载窗体
ds.BeginLoad(typeof(Form));
Control designerContorl = (Control)ds.View;
designerContorl.Dock = DockStyle.Fill;
this.Controls.Add(designerContorl);
}
protected override CodeCompileUnit Parse()
{
#region 源文件读取
var sw = new StreamReader(@"E:\FrmUser.cs");
var sw_designer = new StreamReader(@"E:\FrmUser.Designer.cs");
string formCodeCS = sw.ReadToEnd();
string formCodeDesigner = sw_designer.ReadToEnd();
List<string> source = new List<string>();
source.Add(formCodeCS);
source.Add(formCodeDesigner);
#endregion
//Rolsyn解析C#
var rootDesigner = Source2CodeDom.Parse(formCodeDesigner);
codeDesingerCompileUnit = Source2CodeDom.GetDesignerCodeComplieUnit(rootDesigner);
var rootCS = Source2CodeDom.Parse(formCodeCS);
codeCSCompileUnit = Source2CodeDom.GetCodeComplieUnit(rootCS);
//MergeFormSource
string mergeS = Source2CodeDom.MergeFormSource(formCodeDesigner, formCodeCS);
codeMergeCompileUnit = Source2CodeDom.GetMergeDesignerCodeComplieUnit(mergeS);
return codeMergeCompileUnit;
public static CodeCompileUnit GetDesignerCodeComplieUnit2(CompilationUnitSyntax root)
{
CodeCompileUnit ccu = new CodeCompileUnit();
var firstMember = root.Members[0];
var namespaceDeclration = (NamespaceDeclarationSyntax)firstMember;
var designClassDeclaration = (ClassDeclarationSyntax)namespaceDeclration.Members[0];
var myDesignerClass = new CodeTypeDeclaration(designClassDeclaration.Identifier.ToString());
var initializeComponent = new CodeMemberMethod();
var ns = new CodeNamespace(namespaceDeclration.Name.ToString());
foreach (var m in designClassDeclaration.Members)
{
if (m is ConstructorDeclarationSyntax)
{
var ctor = ((ConstructorDeclarationSyntax)m);
var codeBody = ctor.Body.ToString();
codeBody = codeBody.Trim().TrimStart('{').TrimEnd('}').Trim().TrimEnd(';');
CodeSnippetExpression csbody = new CodeSnippetExpression(codeBody);
CodeExpressionStatement stmt = new CodeExpressionStatement(csbody);
//Add the expression statements to the method.
// InitializeComponent
var cctor = new CodeConstructor();
cctor.Name = ctor.Identifier.ToString();
//var cmm = new CodeMemberMethod();
//cmm.Name = ctor.Identifier.ToString();
//cmm.Attributes = GetCtoRAttrMapping(ctor);
//cmm.ReturnType = new CodeTypeReference(typeof(void));
cctor.Statements.Add(stmt);
myDesignerClass.Members.Add(cctor);
}
if (m is FieldDeclarationSyntax)
{
var F = ((FieldDeclarationSyntax)m);
var type = F.Declaration.Type;
foreach (var variable in F.Declaration.Variables)
{
var field = new CodeMemberField();
field.Name = variable.Identifier.ToString();
field.Type = new CodeTypeReference(type.ToString());
field.Attributes = GetFieldAttrMapping(F);
//field.InitExpression = new CodePrimitiveExpression(null);
myDesignerClass.Members.Add(field);
}
}
if (m is MethodDeclarationSyntax)
{
var node = m as MethodDeclarationSyntax;
#region xml comments
var xmlTrivia = node.GetLeadingTrivia()
.Select(i => i.GetStructure())
.OfType<DocumentationCommentTriviaSyntax>()
.FirstOrDefault();
#endregion
var method = (MethodDeclarationSyntax)m;
var cmm = new CodeMemberMethod();
cmm.Name = method.Identifier.ToString();
///XML注释
string[] comments = xmlTrivia.ToString().Split("\r\n".ToCharArray());
foreach (string text in comments)
{
if (text.Trim() != "")
{
cmm.Comments.Add(new CodeCommentStatement(text.Trim().TrimStart("///".ToCharArray()).Trim(), true));
}
}
if (cmm.Name == "InitializeComponent")
{
//region
CodeRegionDirective codeRegion = new CodeRegionDirective(CodeRegionMode.Start, "Windows 窗体设计器生成的代码");
CodeRegionDirective codeEndRegion = new CodeRegionDirective(CodeRegionMode.End, "");
cmm.StartDirectives.Add(codeRegion);
cmm.EndDirectives.Add(codeEndRegion);
}
//MemberAttributes.Family is protected
//cmm.Attributes = MemberAttributes.Override | MemberAttributes.Family;
cmm.Attributes = GetMethodAttrMapping(method);
cmm.ReturnType = new CodeTypeReference(method.ReturnType.ToString());
foreach (var p in method.ParameterList.Parameters)
{
CodeParameterDeclarationExpression cpd = new CodeParameterDeclarationExpression();
cpd.Name = p.Identifier.ToString();
cpd.Type = new CodeTypeReference(p.Type.ToString());
cmm.Parameters.Add(cpd);
}
//包含方法{};,会重复生成{};
string codeBody = method.Body.ToString();
codeBody = codeBody.Trim().TrimStart('{').TrimEnd('}').Trim().TrimEnd(';');
if (codeBody != "")
{
CodeSnippetExpression csbody = new CodeSnippetExpression(codeBody);
CodeExpressionStatement stmt = new CodeExpressionStatement(csbody);
//Add the expression statements to the method.
cmm.Statements.Add(stmt);
}
myDesignerClass.Members.Add(cmm);
}
if (m is MemberDeclarationSyntax)
{
}
}
ccu.Namespaces.Add(ns);
//Partial Class
myDesignerClass.IsPartial = true;
ns.Types.Add(myDesignerClass);
return ccu;
}
//直接返回代码,最简单
public string GetTextCSCode()
{
Flush();
return __CSTextCode;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有