c# 通过反射 实例化类
1. 用Type
在 website 下
C#代码
//定义参数类型数组
Type[] tps = new Type[2];
tps[0] = typeof(int);
tps[1] = typeof(string);
//定义参数数组
object[] obj = new object[2];
obj[0] = (object)100;
obj[1] = (object)"Param Example";
string UserDaoPath = System.Configuration.ConfigurationSettings.AppSettings["UserDao"];
UserDao userDao = (UserDao)Type.GetType(UserDaoPath).
GetConstructor(tps).Invoke(obj );
2. 用Assembly
在 website 下 这个是导入一个 程序集 然后在程序集里再 实例化类, 我这里其实就是实例化一个类。
C#代码
string UserServicePath = System.Configuration.ConfigurationSettings
.AppSettings["UserService"];
UserService userService = (IUserService)Assembly.Load(UserServicePath).
CreateInstance("UserService");
C#代码
<appSettings >
<add key="UserService" value="App_Code.Service.UserService"></add>
</appSettings>
浙公网安备 33010602011771号