接口编程,反射创建对象
using System;
using System.Collections.Generic;
using System.Text;
namespace ABC.Factory
{
public class BLLFactory
{
/// <summary>
/// 创建实例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="TypeName"></param>
/// <returns></returns>
public static T CreateInstant<T>(string TypeName)
{
T ret = default(T);
try
{
string dd = typeof(string).ToString();
string FullTypeName = "ABC.BLL." + TypeName;
Type type = Type.GetType(FullTypeName);
object obj = Activator.CreateInstance(type);
ret = (T)obj;
return ret;
}
catch
{
return ret;
}
}
/// <summary>
/// 创建实例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="TypeName"></param>
/// <param name="Args">构造函数的参数</param>
/// <returns></returns>
public static T CreateInstant<T>(string TypeName,object[] Args)
{
T ret = default(T);
try
{
string dd = typeof(string).ToString();
string FullTypeName = "ABC.BLL." + TypeName;
Type type = Type.GetType(FullTypeName);
object obj = Activator.CreateInstance(type, Args);
ret = (T)obj;
return ret;
}
catch
{
return ret;
}
}
}
}
//接口编程,反射创建对象
// 类库IBLL里定义有接口ICXbll ,
// 类库 BLL里定义有类CXbll 。类FPCXbll实现 接口ICXbll
// 为使得能够在只引用接口库IBLL而不引用类库 BLL的条件下创建类 BLL.CXbll
IBLL.ICXbll cxBLL = Factory.BLLFactory.CreateInstant<IBLL.ICXbll>("CXbll");
BLL.CXbll cxBLL = new ABC.BLL.CXbll();