用Activator.CreatInstance動態建立同一個Assembly的對象

來源:互聯網
上載者:User
在正常的工廠類中,我們會有很多的switch case之類的,如何避免他們,我們可以利用.Net的Activator幫我們簡單的完成這件事情。目前,經過簡單大測試,支援public的建構函式,internal private的都還沒有仔細看因為我關心的是public的建構函式。
  研究這個的起因在於,我想用基於設定檔的形式來動態建立DataAccess對象。
  主要是由於我的建構函式參數是一個自訂的類,不是系統的基礎資料型別 (Elementary Data Type)。昨天測試的時候不知道怎麼回事發生了問題,所以,今天早上才寫了如下的測試代碼,發覺真的很喜歡Snippet Compilerusing System;
using System.Reflection;
using System.Collections;
namespace FishSky.Testing
{
public class DesireSay
{
   public DesireSay()
   {
   }
   private string mWords;
   public string Words
   {
     get{ return mWords;}
     set{ mWords=value;}
   }   
}
public class SayHello
{
    private string mIWannaSay="";
    private DesireSay mDesireSay;
    private int mInternalInt=-1;
    public SayHello()
    {
    }
    public SayHello(string iWannaSay)
    {
       mIWannaSay=iWannaSay;
    }
    public SayHello(DesireSay desireSay)
    {
      mDesireSay=desireSay;
    }
    internal SayHello(int internalInt)
    {
      mInternalInt=internalInt;
    }
    public string Hello()
    {
        if(mInternalInt!=-1)
        {
           return mInternalInt.ToString();
        }
        else
        {
            if(mDesireSay!=null)
            {
            return mDesireSay.Words;
            }
            else
            {
                if(mIWannaSay=="")
                    return "Hello";
                else
                    return mIWannaSay;
            }
        }
    }
    
}
public class MyClass
{
    public static void Main()
    {
        string TypeName="FishSky.Testing.SayHello";
        Type type=Type.GetType(TypeName,true);
        SayHello hello=(SayHello) Activator.CreateInstance(type);
        WL("SayHello says {0}",hello.Hello());
        Console.WriteLine("Next Test with string parameter in constructor..");
        SayHello hello2=(SayHello) Activator.CreateInstance(type,new Object[]{"I wanna say Hello!"});
        WL("SayHello says {0}",hello2.Hello());
        Console.WriteLine("Next Test with Class parameter in constructor..");
        DesireSay desireSay=new DesireSay();
        desireSay.Words="I Desire say!Let me say!";
        SayHello hello3=(SayHello) Activator.CreateInstance(type,new Object[]{desireSay});
        WL("SayHello says {0}",hello3.Hello());
//        Console.WriteLine("Next Test with int parameter in internal constructor..");
//        SayHello hello4=(SayHello) Activator.CreateInstance(type,new Object[]{250});
//        //SayHello hello4=(SayHello)Activator.CreateInstance(type,BindingFlags.NonPublic,null,new object[250],new CultureInfo());
//        WL("SayHello says {0}",hello4.Hello());        
        RL();
    }
    
    Helper methods#region Helper methods

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);    
    }
    
    private static void RL()
    {
        Console.ReadLine();    
    }
    
    private static void Break() 
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.