|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Loading assemblies by nameHello, all.
I was wondering what is the right way to load an assembly programmatiacally by name. For example, if it's Company.Library. Is there a way in C# where I can load that into an Assembly object if I only have the name; does it have to be in the GAC in this case? Thanks, Ludwig Wittgenstein wrote:
> Hello, all. You can load the assembly from disk like this:> > > I was wondering what is the right way to load an assembly > programmatiacally by name. For example, if it's Company.Library. Is > there a way in C# where I can load that into an Assembly object if I > only have the name; does it have to be in the GAC in this case? Assembly a = System.Reflection.Assembly.LoadFrom(<FileName>); Type[] types = assembly.GetTypes(); You can then loop through all the types to find the class or classes you need to work with. If you need to instantiate the object you'll need to use the InvokeMember method on the type object. Andrew Faust I know how to use Assembly.LoadFrom, however, what I'm wondering about
is how to accomplish the following: Ask the user to enter a namespace, and then load it's assembly, example: Console.WriteLine("Enter name space:); // eg: System.Collections string nameSpace = Console.ReadLine(); Assembly asm = ???????? Console.WriteLine("Loaded assembly: " + asm.FullName); How is that possible? (note: without using an app.config xml file). Ali Andrew Faust wrote: Show quote > Ludwig Wittgenstein wrote: > > Hello, all. > > > > > > I was wondering what is the right way to load an assembly > > programmatiacally by name. For example, if it's Company.Library. Is > > there a way in C# where I can load that into an Assembly object if I > > only have the name; does it have to be in the GAC in this case? > > > You can load the assembly from disk like this: > > Assembly a = System.Reflection.Assembly.LoadFrom(<FileName>); > Type[] types = assembly.GetTypes(); > > You can then loop through all the types to find the class or classes you > need to work with. > > If you need to instantiate the object you'll need to use the > InvokeMember method on the type object. > > Andrew Faust Ludwig Wittgenstein wrote:
Show quote > I know how to use Assembly.LoadFrom, however, what I'm wondering about Do you happen to know what directory the assembly will be in, or can the > is how to accomplish the following: > > > Ask the user to enter a namespace, and then load it's assembly, > example: > > > Console.WriteLine("Enter name space:); // eg: System.Collections > string nameSpace = Console.ReadLine(); > Assembly asm = ???????? > > Console.WriteLine("Loaded assembly: " + asm.FullName); > > > How is that possible? (note: without using an app.config xml file). assembly be anywhere on the system? If you know the directory, I do something similar by iterating through all the assemblies, and checking the names of all classes until we find the one we want. If you don't know the directory of the assemblies, then other than scanning the entire system, I don't know. Andrew Faust I know how to use Assembly.LoadFrom, however, what I'm wondering about
is how to accomplish the following: Ask the user to enter a namespace, and then load it's assembly, example: Console.WriteLine("Enter name space:); // eg: System.Collections string nameSpace = Console.ReadLine(); Assembly asm = ???????? Console.WriteLine("Loaded assembly: " + asm.FullName); How is that possible? (note: without using an app.config xml file). Andrew Faust wrote: Show quote > Ludwig Wittgenstein wrote: > > Hello, all. > > > > > > I was wondering what is the right way to load an assembly > > programmatiacally by name. For example, if it's Company.Library. Is > > there a way in C# where I can load that into an Assembly object if I > > only have the name; does it have to be in the GAC in this case? > > > You can load the assembly from disk like this: > > Assembly a = System.Reflection.Assembly.LoadFrom(<FileName>); > Type[] types = assembly.GetTypes(); > > You can then loop through all the types to find the class or classes you > need to work with. > > If you need to instantiate the object you'll need to use the > InvokeMember method on the type object. > > Andrew Faust |
|||||||||||||||||||||||