模拟Asp.Net Forums 2.0 数据提供者类
using System;
using System.Reflection;
namespace AbstractClass
{
//===============================================================
// 模拟Asp.Net Forums 2.0 数据提供者类实现,
// 以实现多数据库支持特性.
// 2004.8.2 by venjiang.
// ==============================================================
#region 主程序调用
class Class1
{
[STAThread]
static void
{
// 所有方法调用,都在BaseAbstact类进行,不涉及子类调用.
// 这样只要派生类(SubClass)能够重写基类相应方法就可以了.
// 调用基类静态方法
BaseAbstract.BaseOut();
// 调用派生类SubClassA
BaseAbstract ba=BaseAbstract.Instance("AbstractClass.SubClassA", "Power by venjiang.");
ba.BaseOut1();
ba=BaseAbstract.Instance("AbstractClass.SubClassA", "venjiang", 28);
ba.BaseOut2();
// 调用派生类SubClassB
ba=BaseAbstract.Instance("AbstractClass.SubClassB", "Power by venjiang.");
ba.BaseOut1();
ba=BaseAbstract.Instance("AbstractClass.SubClassB", "venjiang", 28);
ba.BaseOut2();
Console.Read();
}
}
#endregion
#region 相当于ForumsDataProvider类
public abstract class BaseAbstract
{
public static BaseAbstract Instance(string subClassType, string name)
{
Type myType=null;
// 获取派生类的类型,在Forums2中subClassType这个类型由Web.config文件中<providers>下的type指定.
myType = Type.GetType( subClassType );
// 构建构造函数参数类型数组
Type[] types = new Type[1];
types[0] = typeof(string);
// 获取指定参数的派生类的构造函数
ConstructorInfo ci = myType.GetConstructor(types);
// 指定派生类参数值
object[] paramArray = new object[1];
paramArray[0] = name;
return (BaseAbstract)ci.Invoke(paramArray);
}
public static BaseAbstract Instance(string subClassType, string name, int height)
{
Type myType=null;
// 获取派生类的类型
myType = Type.GetType( subClassType );
// 构建构造函数参数类型数组
Type[] types = new Type[2];
types[0] = typeof(string);
types[1] = typeof(int);
// 获取指定参数的派生类的构造函数
ConstructorInfo ci = myType.GetConstructor(types);
// 指定派生类参数值
object[] paramArray = new object[2];
paramArray[0] = name;
paramArray[1] = height;
return (BaseAbstract)ci.Invoke(paramArray);
}
public static void BaseOut()
{
// Type myType=null;
// myType = Type.GetType("AbstractClass.SubClassA");
Console.WriteLine("[基类]这是抽象类的静态方法");
// Console.WriteLine("测试{0}",myType.ToString());
}
public abstract void BaseOut1();
public abstract void BaseOut2();
}
#endregion
#region 相当于SqlDataProvider类
public class SubClassA:BaseAbstract
{
private string _name="";
private int _height=0;
public SubClassA(string name)
{
_name=name;
}
public SubClassA(string name,int height)
{
_name=name;
_height=height;
}
public override void BaseOut1()
{
Console.WriteLine("[派生类A]重写基类BaseOut1方法,"+ _name);
}
public override void BaseOut2()
{
Console.WriteLine("[派生类A]重写基类BaseOut2方法,"+ _name + "," + _height.ToString());
}
}
#endregion
#region 相当于其它数据库支持类
public class SubClassB:BaseAbstract
{
private string _name="";
private int _height=0;
public SubClassB(string name)
{
_name=name;
}
public SubClassB(string name,int height)
{
_name=name;
_height=height;
}
public override void BaseOut1()
{
Console.WriteLine("[派生类B]重写基类BaseOut1方法,"+ _name);
}
public override void BaseOut2()
{
Console.WriteLine("[派生类B]重写基类BaseOut2方法,"+ _name + "," + _height.ToString());
}
}
#endregion
}
运行结果如下: