研究这个的起因在于,我想用基于配置文件的形式来动态的创建DataAccess对象。
主要是由于我的构造函数参数是一个自定义的类,不是系统的基本数据类型。昨天测试的时候不知道怎么回事发生了问题,所以,今天早上才写了如下的测试代码,发觉真的很喜欢Snippet Compiler
using 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
}
}
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
}
}