Loading an assembly dynamically and using types/functions in it
-
I'm trying to load an assembly dynamically (IE. extracting it as an embedded resource, and then using Assembly.Load() to load it) and then use the types and functions that are built into it, but I can't figure out how to get access to them, nor could I find any good tutorials. Any hints, or am I trying the impossible?
-
I'm trying to load an assembly dynamically (IE. extracting it as an embedded resource, and then using Assembly.Load() to load it) and then use the types and functions that are built into it, but I can't figure out how to get access to them, nor could I find any good tutorials. Any hints, or am I trying the impossible?
You can use Assembly.Load or Assembly.LoadFrom(). Those function will return you an Assembly object. You can then call GetTypes() on that object to get the list of all types. For each Type object, you can then call Activator.CreateInstance() to create an instance and then Type's InvokeMember() to call methods. Something like
Assembly assembly = Assembly.Load("..."); foreach(Type t in assembly.GetTypes()) { Object obj = Activator.CreateInstance(t); t.InvokeMember("MyMethod", BindingFlags.Instance, null, obj, null); }
Hope this helps. Regards Senthil My Blog