2016-05-07 9 views
7

के साथ कस्टम क्लास का उपयोग कैसे करें मेरे पास एक ऐसा फॉर्म है जिसे मैं गतिशील रूप से संकलित करता हूं और मेरे पास स्टाइल क्लास है। जब मैं इस शैली वर्ग को अपने फॉर्म स्रोत में कॉपी करता हूं और संकलित करता हूं तो यह सब ठीक काम करता है। लेकिन मैं इस शैली वर्ग का उपयोग अपने फॉर्म स्रोत पर कॉपी किए बिना कैसे कर सकता हूं। मेरा मुख्य कार्यक्रम जो इस फॉर्म को संकलित करता है, इस वर्ग में है, मैं इसका उपयोग कैसे कर सकता हूं? हो सकता है कि मैं स्टाइल क्लास को इस पर संकलित कर सकूं, जैसे कि मैं इसे एक संकलित करता हूं?डायनामिक संकलन

कार्यक्रम स्रोत:

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"); 
      } 
     } 
    } 
} 

शैली फ़ोल्डर: enter image description here

फार्म स्रोत:

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 फ़ाइल पर स्टाइल से फॉर्म को अलग किया। मेरे पास इस मुख्य शैली में मेरा मुख्य कार्यक्रम है (आप स्क्रीनशॉट पर देख सकते हैं)। मैं इस शैली वर्ग को अपने गतिशील रूप से संकलित रूप में कैसे भेज सकता हूं, इसलिए फ़ॉर्म इसका उपयोग कर सकता है।

उत्तर

1

अंदर Form.txt क्रम में कक्षाओं के बजाय फाइल मैक्रो का प्रावधान लोड शैली फ़ाइलों और उन्हें कोड के रूप में बदल देते हैं:

//Load style files 
var NSThemeSource = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Class\Style\NSTheme.cs")); 
var themeBaseSource = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Class\Style\ThemeBase154.cs")); 
var source = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Form.txt")); 

//Substitute in the form styles 
source = source.Replace("<%THEME%>", NSThemeSource); 
source = source.Replace("<%THEMEBASE%>", themeBaseSource); 

पीएस आप एम्बेडेड संसाधन पर स्टाइल फाइलों के लिए बिल्ड एक्शन स्विच कर सकते हैं ताकि आप स्टाइल क्लास exe से प्राप्त कर सकें। और यह सहायक का उपयोग करें - http://www.vcskicks.com/embedded-resource.php

से आप के लिए कोड को बदलने की जरूरत:

var NSThemeSource = ResourceHelper.GetEmbeddedResource("Class/Style/NSTheme.cs"); 
var themeBaseSource = ResourceHelper.GetEmbeddedResource("Class/Style/ThemeBase154.cs"); 
4

आप बस इतना की तरह स्रोत के लिए शैली संलग्न करने के लिए सक्षम होना चाहिए:

namespace myForm 
{ 
<%THEME%> 
} 

namespace myForm 
{ 
<%THEMEBASE%> 
} 

संकलन से पहले:

var source = File.ReadAllText("form.txt"); 

source += Environment.NewLine; 
source += File.ReadAllText("style.txt"); 
+0

ओएमजी, अच्छा विचार, मैं इसे कैसे नहीं देखता? : डी धन्यवाद – SLI

1

सबसे पहले, आप कक्षाएं, enums बनाने की जरूरत है, और आदि सार्वजनिक NSTheme.cs और ThemeBase154.cs में । (वैश्विक (गतिशील) नामस्थान में रखने के बजाय, कुछ नामस्थान में रखना बेहतर है)।

फिर, पैरामीटर जोड़ें। संदर्भित Assemblies.Add ("dynamic.exe"); आप exe फ़ाइल (आप अपनी शैलियों को एक अलग डीएल में रखना चाहते हैं)।

इसके बाद, अपने form.txt में using dynamic; जोड़ें और new myForm.NSTheme() से क्वालीफायर हटाएं (इसलिए, यह new NSTheme() बन जाता है)।

अब आप अपनी शैलियों का उपयोग अपनी फॉर्म कक्षा में कॉपी किए बिना कर सकते हैं।

यदि आपको कुछ याद आती है, तो CompilerResults results विशेष समस्याओं के लिए त्रुटियों की जांच करें।

संबंधित मुद्दे