<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      從Component對象到CodeDom——舞動你的Code系列(1)

      我們經常會有這樣的需求或者想法:動態的生成或者修改代碼。當然,我們可以把代碼看成字符串而直接修改,但是這種做法也未免太生硬了,能解決的問題有限;而另一個方式就是CodeDom。

      CodeDom是強大的!我們感謝微軟,給我們提供了強大的描述面向對象語言的框架;我們感謝微軟,給我們提供了能夠根據CodeDom生成代碼或者程序集的CodeDomProvider;可惜微軟沒有給我們提供能夠從object或者代碼生成CodeDom的能力。

      關于CodeDom的知識本文不過多涉及、感興趣的童鞋可以閱讀MSDN或者博客園的其它文章學習了解。本系列期望解決的問題就是如何將對象或者代碼生成CodeDom。當然,由于微軟并沒有提供這樣的支持,而我也不可能寫一個解析C#語言或者VB語言的CodeParser,所以本文提供的方案也能力有限,但愿能夠解決你的一部分問題或者給您能學到點知識。

      這是本系列的第一篇文章,如何讓一個Component對象生成CodeDom。核心思想就是虛擬一個DesignTime的環境,并將Component添加到Designer中,然后使用ComponentTypeCodeDomSerializer將Component序列化成CodeTypeDeclaration。本方案可以在任意程序下執行,不依賴IDE,也不需要引用各種奇怪的dll。

      下面就是具體實現:

      首先,創建一個WindowsControlLibrary,名叫WindowsControlLibrary1。

      然后,添加一個類取名MyComponent1,類中有一個GetSet的屬性IntProperty,還有一個設置了背景色的TextBox:

       

      public class MyComponent1 : Component    
      {        
      	public MyComponent1()        
      	{           
      		 textBox1 = new TextBox();           
      		 textBox1.BackColor = Color.Red;        
      	}
      
      	private int int1;        
      	private TextBox textBox1;        
      	
      	public int IntProperty        
      	{            
      		get { return int1; }            
      		set { int1 = value; } 
      	}        
      
      	public TextBox TextBoxProperty        
      	{
      		get { return textBox1; }            
      	}
      }

       

      接著創建另一個WindowsFormsApplication項目:CodeDomSample,并引用System.Design和WindowsControlLibrary1項目(當然,你也可以把WindowsControlLibrary1編譯成dll并引用這個dll)

      現在,創建我們的核心類CodeTypeConverter,對于具體實現我不做過多的說明,你不必要關心實現的具體細節,只要這個實現能夠滿足你的需求就行了。如果你有看不明白的地方請提問,我會認真回答。

       

          public class CodeTypeConverter
          {
              private IServiceProvider _serviceProvider;
      
              private IDesignerHost DesignerHost
              {
                  get
                  {
                      return this._serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;
                  }
              }
      
              //將Component Load到DesignerHost中并返回
              private IComponent LoadComponent(IComponent component)
              {
                  DesignSurfaceManager manager = new DesignSurfaceManager();
                  DesignSurface surface = manager.CreateDesignSurface();
                  surface.BeginLoad(component.GetType());
                  this._serviceProvider = surface;
                  IComponent newComponent = DesignerHost.RootComponent;
                  //暴力克隆,將component上的所有Field設到newComponent上
                  FieldInfo[] fields = component.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
                  foreach (FieldInfo field in fields)
                  {
                      object fieldValue = field.GetValue(component);
                      //將所有子Component Load到DesignerHost中
                      if (fieldValue != null && fieldValue is IComponent)
                      {
                          DesignerHost.Container.Add(fieldValue as IComponent, field.Name);
                      }
                      field.SetValue(newComponent, fieldValue);
                  }
                  return newComponent;
              }
          
              //將DesignerHost中的Component轉成CodeType
              public CodeTypeDeclaration ConvertComponentToCodeType(IComponent component)
              {
                  component = this.LoadComponent(component) as Component;
                  DesignerSerializationManager manager = new DesignerSerializationManager(this._serviceProvider);
                  //這句Code是必須的,必須要有一個session,DesignerSerializationManager才能工作
                  IDisposable session = manager.CreateSession();
                  TypeCodeDomSerializer serializer = manager.GetSerializer(component.GetType(), typeof(TypeCodeDomSerializer)) as TypeCodeDomSerializer;
                  List<object> list = new List<object>();
                  foreach (IComponent item in this.DesignerHost.Container.Components)
                  {
                      list.Add(item);
                  }
                  CodeTypeDeclaration declaration = serializer.Serialize(manager, component, list);
                  session.Dispose();
                  return declaration;
              }
      }

       

      好了,CodeTypeConverter實現完成。現在在Form1中寫一個Test方法測試:

       

              public Form1()
              {
                  InitializeComponent();
                  Test();
              }
      
              public void Test()
              {
                  CodeTypeConverter designerHost = new CodeTypeConverter();
                  MyComponent1 component = new MyComponent1();
                  component.IntProperty = 10;
                  component.TextBoxProperty.Text = "Hello World";
      
                  CodeTypeDeclaration componentType = designerHost.ConvertComponentToCodeType(component);
                  componentType.Name = component.GetType().Name + "1";
      
                  StringBuilder bulder = new StringBuilder();
                  StringWriter writer = new StringWriter(bulder, CultureInfo.InvariantCulture);
                  CodeGeneratorOptions option = new CodeGeneratorOptions();
                  option.BracingStyle = "C";
                  option.BlankLinesBetweenMembers = false;
                  CSharpCodeProvider codeDomProvider = new CSharpCodeProvider();
                  codeDomProvider.GenerateCodeFromType(componentType, writer, option);
                  Debug.WriteLine(bulder.ToString());
                  writer.Close();
              }

       

      CodeDomSample跑起來以后,就可以在輸出窗口看到如下的輸出:

       

      public class MyComponent11 : WindowsControlLibrary1.MyComponent1
      {
          private System.Windows.Forms.TextBox textBox1;
          private MyComponent11()
          {
              this.InitializeComponent();
          }
          private void InitializeComponent()
          {
              this.textBox1 = new System.Windows.Forms.TextBox();
              // 
              // textBox1
              // 
              this.textBox1.BackColor = System.Drawing.Color.Red;
              this.textBox1.Location = new System.Drawing.Point(0, 0);
              this.textBox1.Name = "textBox1";
              this.textBox1.Size = new System.Drawing.Size(100, 20);
              this.textBox1.TabIndex = 0;
              this.textBox1.Text = "Hello World";
              // 
              // 
              // 
              this.IntProperty = 10;
          }
      }

       

      搞定收工。歡迎提問以及拍磚灌水,更歡迎掌聲鮮花。

      posted @ 2010-08-24 15:14  葡萄城技術團隊  閱讀(1852)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 亚州少妇无套内射激情视频| 亚洲成a人片在线观看中文 | 国产国语对白露脸正在播放| 亚洲无av中文字幕在线| 忘忧草www日本韩国| 在线成人国产天堂精品av| 2020国产欧洲精品网站| 一二三三免费观看视频| 夜夜躁狠狠躁日日躁视频| 久久精品国产国产精品四凭| 黔南| 亚洲中文字幕五月五月婷| 亚洲精品一区二区美女| 国产美女高潮流白浆视频| 激情综合五月网| 亚洲精品国产一二三区| 一日本道伊人久久综合影| 欧洲尺码日本尺码专线美国又| 日韩精品av一区二区三区| 黑人巨大精品欧美| 国内自拍偷拍一区二区三区| 五月丁香啪啪| 国产按头口爆吞精在线视频| 亚洲春色在线视频| 国产乱妇乱子视频在播放| 黄色大全免费看国产精品| 新闻| 东京一本一道一二三区| 欧美激情一区二区久久久| 亚洲AV日韩AV高清在线观看| 黄色三级亚洲男人的天堂| 久久久久免费看黄a片app| 国产成人亚洲精品自产在线 | 国产亚洲av产精品亚洲| 国产乱码日产乱码精品精| 性视频一区| 国产亚洲精品综合99久久| 精品自拍自产一区二区三区 | 亚洲一级特黄大片在线观看| 色偷偷久久一区二区三区| 欧洲精品免费一区二区三区|