jle
jle

Reputation: 9489

Need to cast to an object without knowing what type the object is

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

Answers (2)

Laurent Etiemble
Laurent Etiemble

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

leppie
leppie

Reputation: 117360

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

Upvotes: 3

Related Questions