c# - Can I instantiate a type as 'dynamic' from another AppDomain? -


i'm trying load type different assembly (not known @ build time) 'dynamic' , execute method on type. goal disconnect 'plugin' parent application such there no requirement shared code or common interface type. interface implied way of expected method signature on loaded type.

this works:

dynamic myobj = assembly.load("myassembly").createinstance("mytype"); myobj.execute(); 

however load type current appdomain along dependent assemblies. want modify allow me same thing in separate appdomain.

this works doesn't make use of dynamic keyword, need know explicit type instantiating able call execute method:

var appdomain = appdomain.createdomain(domainname, evidence, setup); var myobj = appdomain.createinstanceandunwrap(assembly, type); typeof(imyinterface).invokemember("execute",  bindingflags.invokemethod, null, myobj); 

this target case , have been trying working:

dynamic myobj = ad.createinstanceandunwrap(assembly, type); myobj.execute(); 

i keep ending runtimebinderexception message "'system.marshalbyrefobject' not contain definition 'execute'". message makes sense, sure doesn't contain definition 'execute', know type instantiating indeed contain 'execute' method. imagine there's going on here transparent proxy preventing working i'm not sure what.

my actual class trying instantiate looks this:

[serializable] public class myclass : marshalbyrefobject {   public void execute() {     //   } } 

i have tried shared interface (not primary goal, i'm trying figure out first) like:

[serializable] public class myclass : marshalbyrefobject, iplugin {   public void execute() {     //   } } 

where iplugin known type in parent application , plugin has appropriate reference @ build time doesn't seem work either.

i'm guessing @ point it's not possible load type dynamic across appdomain boundary.

is there way work?

as leppie indicated, you'll have implement idynamicmetaobjectprovider interface wrap proxy that's being returned you, , can use make dynamic calls on that.

in implementation, you'd want take wrapped proxy , forward calls static executemessage method on remotingservices class, take proxy, imethodcallmessage interface implementation.

note implementing imethodcallmessage interface not trivial. also, you'd have interpret imethodreturnmessage interface implementation return value, ref , out parameters correctly (if any).

that said, it's better idea provide assembly contains only interface client , server assume; if method changed in way on server side, though client side uses dynamic, you'd still have change call site accommodate change. @ least interface, some type of compile-time check, preferred run-time error.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -