Reputation: 9489
I am trying to dynamically load my authentication server type based on a setting. I am hung up on how to cast to a type when I don't know the type.
Type t = Type.GetType(WebConfigurationManager.AppSettings.Get("AuthenticationSvcImpl"));
IAuthenticationService authCli = Activator.CreateInstance(t);
return authCli.AuthenticateUser(login);
I know there is Convert.ChangeType(), but that just converts to an object...
Upvotes: 4
Views: 610
Reputation: 27929
Is that what you are looking for ?
Type t = Type.GetType(WebConfigurationManager.AppSettings.Get("AuthenticationSvcImpl"));
IAuthenticationService authCli = (IAuthenticationService) Activator.CreateInstance(t);
return authCli.AuthenticateUser(login);
Upvotes: 0
Reputation: 117360
var authCli = Activator.CreateInstance(t) as IAuthenticationService;
Upvotes: 3