It is necessary to apply to the object, not knowing what type of object

I am trying to dynamically load my type of authentication server based on settings. I hung up on how to cast 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 it just converts to an object ...

+4
source share
2 answers
 var authCli = Activator.CreateInstance(t) as IAuthenticationService; 
+3
source

Is this what you are looking for?

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

Source: https://habr.com/ru/post/1307649/


All Articles