需要在不知道对象是什么类型的情况下强制转换为对象

时间:2010-04-22 10:01:23

标签: c# reflection interface casting

我正在尝试根据设置动态加载我的身份验证服务器类型。当我不知道这种类型的时候,我很想知道如何施放类型。

   Type t = Type.GetType(WebConfigurationManager.AppSettings.Get("AuthenticationSvcImpl"));
    IAuthenticationService authCli = Activator.CreateInstance(t);
    return authCli.AuthenticateUser(login);

我知道有Convert.ChangeType(),但只是转换为对象......

2 个答案:

答案 0 :(得分:3)

var authCli = Activator.CreateInstance(t) as IAuthenticationService;

答案 1 :(得分:0)

这就是你要找的东西吗?

Type t = Type.GetType(WebConfigurationManager.AppSettings.Get("AuthenticationSvcImpl"));
IAuthenticationService authCli = (IAuthenticationService) Activator.CreateInstance(t);
return authCli.AuthenticateUser(login);