mercredi 29 juin 2016

Interface from string to use as generic type

I'm using Service Fabric in Azure and set up a proxy to an actor like this:

var proxy = ActorProxy.Create<T>(actorId);

Where T must be specified as the interface of the actor I'm calling.

Let's say I have the name of the interface as a string:

var interfaceName = "IUserActor";

Is there a way to instatiate a generic type by this string name? And if there is, how do I call a method specified in given interface by it's string name?

All actor interfaces inherits from IActor which is a part of Service Fabric.

Now I understand that this is not recommended, the point is to be able to access the actor state for a given actor, from tests and administrative purposes. Speed is insignificant in this case, so any reflection approach will do.

So, a basic usage example, not using the dynamic interface name:

public async Task<string> AdminGetState(ActorId actorId, string interfaceName){
   var proxy = ActorProxy.Create<IUserActor>(actorId);
   var state = await proxy.AdminGetStateJson();
   return JsonConvert.SerializeObject(state);
}





Aucun commentaire:

Enregistrer un commentaire