c# – 如何使用动态编译的自定义类

c# – 如何使用动态编译的自定义类,第1张

概述我有一个动态编译的表单,我有一个样式类.当我将这个样式类复制到我的表单源并编译它时,一切正常.但是我如何使用这个样式类而不将其复制到我的表单源.我编译此表单的主程序有这个类,我该如何使用它?也许我可以将样式类传递给它,因为我编译它,就像一个var? 节目来源: using System;using System.CodeDom.Compiler;using System.Collections 我有一个动态编译的表单,我有一个样式类.当我将这个样式类复制到我的表单源并编译它时,一切正常.但是我如何使用这个样式类而不将其复制到我的表单源.我编译此表单的主程序有这个类,我该如何使用它?也许我可以将样式类传递给它,因为我编译它,就像一个var?

节目来源:

using System;using System.CodeDom.Compiler;using System.Collections.Generic;using System.IO;using System.Threading;using System.windows.Forms;using Microsoft.CSharp;namespace dynamic{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            new Thread(newForm).Start();        }        public voID newForm()        {            using (CSharpCodeProvIDer provIDer = new CSharpCodeProvIDer(new Dictionary<string,string>                {                    {"CompilerVersion","v4.0"}                }))            {                var parameters = new CompilerParameters                {                    GenerateExecutable = false,// Create a dll                    GenerateInMemory = true,// Create it in memory                    WarningLevel = 3,// Default warning level                    CompilerOptions = "/optimize",// Optimize code                    TreatWarningsAsErrors = false // Better be false to avoID break in warnings                };                parameters.ReferencedAssemblIEs.Add("mscorlib.dll");                parameters.ReferencedAssemblIEs.Add("System.dll");                parameters.ReferencedAssemblIEs.Add("System.Core.dll");                parameters.ReferencedAssemblIEs.Add("System.Data.dll");                parameters.ReferencedAssemblIEs.Add("System.Drawing.dll");                parameters.ReferencedAssemblIEs.Add("System.Xml.dll");                parameters.ReferencedAssemblIEs.Add("System.windows.Forms.dll");                var source = file.ReadAllText("form.txt");                CompilerResults results = provIDer.CompileAssemblyFromSource(parameters,source);                Type type = results.CompiledAssembly.GetType("myForm.Form1");                object compiledobject = Activator.CreateInstance(type);                type.getmethod("ShowDialog",new Type[0]).Invoke(compiledobject,new object[] {});                MessageBox.Show("formClosed");            }        }    }}

样式文件夹:

表格来源:

using System;using System.windows.Forms;namespace myForm{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            var newTmr = new Timer { Interval = 1000 };            newTmr.Tick += count;            newTmr.Enabled = true;        }        private voID count(Object myObject,EventArgs myEventArgs)        {            timer.Value2 = (Int32.Parse(timer.Value2) + 1).ToString();        }        private voID button1_Click(object sender,System.EventArgs e)        {            MessageBox.Show("clicked");        }        private voID nsbutton1_Click(object sender,EventArgs e)        {            MessageBox.Show("button");        }    }}namespace myForm{    partial class Form1    {        /// <summary>        /// required designer variable.        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// Clean up any resources being used.        /// </summary>        /// <param name="disposing">true if managed resources should be disposed; otherwise,false.</param>        protected overrIDe voID dispose(bool disposing)        {            if (disposing && (components != null))            {                components.dispose();            }            base.dispose(disposing);        }        #region windows Form Designer generated code        /// <summary>        /// required method for Designer support - do not modify        /// the contents of this method with the code editor.        /// </summary>        private voID InitializeComponent()        {            this.nstheme1 = new myForm.NStheme();            this.nsbutton1 = new myForm.NSbutton();            this.timer = new myForm.NSLabel();            this.nsControlbutton1 = new myForm.NSControlbutton();            this.nstheme1.SuspendLayout();            this.SuspendLayout();            //             // nstheme1            //             this.nstheme1.AccentOffset = 0;            this.nstheme1.Backcolor = System.Drawing.color.FromArgb(((int)(((byte)(50)))),((int)(((byte)(50)))),((int)(((byte)(50)))));            this.nstheme1.borderStyle = System.windows.Forms.FormborderStyle.None;            this.nstheme1.colors = new myForm.Bloom[0];            this.nstheme1.Controls.Add(this.nsControlbutton1);            this.nstheme1.Controls.Add(this.timer);            this.nstheme1.Controls.Add(this.nsbutton1);            this.nstheme1.Customization = "";            this.nstheme1.Dock = System.windows.Forms.DockStyle.Fill;            this.nstheme1.Font = new System.Drawing.Font("Verdana",8F);            this.nstheme1.Image = null;            this.nstheme1.Location = new System.Drawing.Point(0,0);            this.nstheme1.Movable = true;            this.nstheme1.name = "nstheme1";            this.nstheme1.norounding = false;            this.nstheme1.Sizable = true;            this.nstheme1.Size = new System.Drawing.Size(284,274);            this.nstheme1.SmartBounds = true;            this.nstheme1.Startposition = System.windows.Forms.FormStartposition.windowsDefaultLocation;            this.nstheme1.TabIndex = 0;            this.nstheme1.Text = "nstheme1";            this.nstheme1.TransparencyKey = System.Drawing.color.Empty;            this.nstheme1.transparent = false;            //             // nsbutton1            //             this.nsbutton1.Location = new System.Drawing.Point(100,166);            this.nsbutton1.name = "nsbutton1";            this.nsbutton1.Size = new System.Drawing.Size(75,23);            this.nsbutton1.TabIndex = 0;            this.nsbutton1.Text = "nsbutton1";            this.nsbutton1.Click += new System.EventHandler(this.nsbutton1_Click);            //             // timer            //             this.timer.Font = new System.Drawing.Font("Segoe UI",11.25F,System.Drawing.FontStyle.Bold);            this.timer.Location = new System.Drawing.Point(91,82);            this.timer.name = "timer";            this.timer.Size = new System.Drawing.Size(101,23);            this.timer.TabIndex = 1;            this.timer.Text = "nsLabel1";            this.timer.Value1 = "Timer: ";            this.timer.Value2 = "0";            //             // nsControlbutton1            //             this.nsControlbutton1.Anchor = ((System.windows.Forms.AnchorStyles)((System.windows.Forms.AnchorStyles.top | System.windows.Forms.AnchorStyles.Right)));            this.nsControlbutton1.Controlbutton = myForm.NSControlbutton.button.Close;            this.nsControlbutton1.Location = new System.Drawing.Point(262,4);            this.nsControlbutton1.margin = new System.windows.Forms.padding(0);            this.nsControlbutton1.MaximumSize = new System.Drawing.Size(18,20);            this.nsControlbutton1.MinimumSize = new System.Drawing.Size(18,20);            this.nsControlbutton1.name = "nsControlbutton1";            this.nsControlbutton1.Size = new System.Drawing.Size(18,20);            this.nsControlbutton1.TabIndex = 2;            this.nsControlbutton1.Text = "nsControlbutton1";            //             // Form1            //             this.autoScaleDimensions = new System.Drawing.Sizef(6F,13F);            this.autoScaleMode = System.windows.Forms.autoScaleMode.Font;            this.ClIEntSize = new System.Drawing.Size(284,274);            this.Controls.Add(this.nstheme1);            this.FormborderStyle = System.windows.Forms.FormborderStyle.None;            this.name = "Form1";            this.Text = "Form1";            this.nstheme1.ResumeLayout(false);            this.ResumeLayout(false);        }        #endregion        private NStheme nstheme1;        private NSbutton nsbutton1;        private NSControlbutton nsControlbutton1;        private NSLabel timer;    }}

风格来源:
http://pastebin.com/CjmQQ9ND

项目来源 – https://yadi.sk/d/ChtMacrsraD4g
如果你编译这个源代码,一切都会正常工作.那是因为我在form.txt文件中使用了样式.我在form.txt文件中将表单与样式分开.我在我的主程序中有这种风格(你可以在屏幕截图中看到).如何将此样式类发送到动态编译的表单,因此表单可以使用它.

解决方法 您应该能够简单地将样式附加到源代码,如下所示:
var source = file.ReadAllText("form.txt");source += Environment.Newline;source += file.ReadAllText("style.txt");
总结

以上是内存溢出为你收集整理的c# – 如何使用动态编译的自定义类全部内容,希望文章能够帮你解决c# – 如何使用动态编译的自定义类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1247413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存