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

标签 c# reflection interface casting

我正在尝试根据设置动态加载我的身份验证服务器类型。当我不知道类型时,我会挂断如何转换为类型。

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

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

最佳答案

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

关于c# - 需要在不知道对象是什么类型的情况下转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2689795/

相关文章:

c# - 确定 IDisposable 是否应该扩展接口(interface)或在实现所述接口(interface)的类上实现

c# - Type.GetType 不适用于泛型类?

java - 根据 JVM 版本调用函数

reflection - 如何将 reflect.Value 转换为它的类型?

c# - 什么是集合语义(在.NET中)?

c# - 使用 visual studios 2010 的 asp.net mvc4 教程中的 app_data 中未显示数据库文件

c# - .NET 中 CIL 知识的优势

c# - 如何从azure函数获取json对象的输出?

java - 扩展已经实现接口(interface)的抽象类的类是否需要接口(interface)?

objective-c - 在 Cocoa 中使用许多界面元素