用Activator.CreatInstance动态的创建同一个Assembly的对象

本文介绍如何利用.NET框架中的Activator类简化工厂模式的实现,通过示例代码展示了如何根据不同类型的构造函数参数动态创建对象,从而避免使用复杂的switch-case结构。
在正常的工厂类中,我们会有很多的switch case之类的,如何避免他们,我们可以利用.Net的Activator帮我们简单的完成这件事情。目前,经过简单大测试,支持public的构造函数,internal private的都还没有仔细看14.gif因为我关心的是public的构造函数。
  研究这个的起因在于,我想用基于配置文件的形式来动态的创建DataAccess对象。
  主要是由于我的构造函数参数是一个自定义的类,不是系统的基本数据类型。昨天测试的时候不知道怎么回事发生了问题,所以,今天早上才写了如下的测试代码,发觉真的很喜欢Snippet Compiler
None.gifusing System;
None.gif
using System.Reflection;
None.gif
using System.Collections;
None.gif
namespace FishSky.Testing
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public class DesireSay
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif   
public DesireSay()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockEnd.gif   }

InBlock.gif   
private string mWords;
InBlock.gif   
public string Words
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
getdot.gifreturn mWords;}
ExpandedSubBlockStart.gifContractedSubBlock.gif     
setdot.gif{ mWords=value;}
ExpandedSubBlockEnd.gif   }
   
ExpandedSubBlockEnd.gif}

InBlock.gif
public class SayHello
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
private string mIWannaSay="";
InBlock.gif    
private DesireSay mDesireSay;
InBlock.gif    
private int mInternalInt=-1;
InBlock.gif    
public SayHello()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public SayHello(string iWannaSay)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       mIWannaSay
=iWannaSay;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public SayHello(DesireSay desireSay)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      mDesireSay
=desireSay;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
internal SayHello(int internalInt)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      mInternalInt
=internalInt;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string Hello()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(mInternalInt!=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
return mInternalInt.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(mDesireSay!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
return mDesireSay.Words;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(mIWannaSay=="")
InBlock.gif                    
return "Hello";
InBlock.gif                
else
InBlock.gif                    
return mIWannaSay;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockEnd.gif}

InBlock.gif
public class MyClass
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string TypeName="FishSky.Testing.SayHello";
InBlock.gif        Type type
=Type.GetType(TypeName,true);
InBlock.gif        SayHello hello
=(SayHello) Activator.CreateInstance(type);
InBlock.gif        WL(
"SayHello says {0}",hello.Hello());
InBlock.gif        Console.WriteLine(
"Next Test with string parameter in constructordot.gif..");
ExpandedSubBlockStart.gifContractedSubBlock.gif        SayHello hello2
=(SayHello) Activator.CreateInstance(type,new Object[]dot.gif{"I wanna say Hello!"});
InBlock.gif        WL(
"SayHello says {0}",hello2.Hello());
InBlock.gif        Console.WriteLine(
"Next Test with Class parameter in constructordot.gif..");
InBlock.gif        DesireSay desireSay
=new DesireSay();
InBlock.gif        desireSay.Words
="I Desire say!Let me say!";
ExpandedSubBlockStart.gifContractedSubBlock.gif        SayHello hello3
=(SayHello) Activator.CreateInstance(type,new Object[]dot.gif{desireSay});
InBlock.gif        WL(
"SayHello says {0}",hello3.Hello());
InBlock.gif
//        Console.WriteLine("Next Test with int parameter in internal constructordot.gif..");
InBlock.gif
//        SayHello hello4=(SayHello) Activator.CreateInstance(type,new Object[]{250});
InBlock.gif
//        //SayHello hello4=(SayHello)Activator.CreateInstance(type,BindingFlags.NonPublic,null,new object[250],new CultureInfo());
InBlock.gif
//        WL("SayHello says {0}",hello4.Hello());        
InBlock.gif
        RL();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Helper methods#region Helper methods
InBlock.gif
InBlock.gif    
private static void WL(object text, params object[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(text.ToString(), args);    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
private static void RL()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.ReadLine();    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
private static void Break() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Diagnostics.Debugger.Break();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/wildfish/archive/2006/01/14/317060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值