Home All Groups Group Topic Archive Search About

Loading assemblies by name

Author
24 May 2006 4:46 PM
Ludwig Wittgenstein
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?



Thanks,

Author
24 May 2006 7:23 PM
Andrew Faust
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
Author
24 May 2006 8:34 PM
Ludwig Wittgenstein
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
Author
24 May 2006 9:04 PM
Andrew Faust
Ludwig Wittgenstein wrote:
Show quote
> 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).

Do you happen to know what directory the assembly will be in, or can the
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
Author
24 May 2006 8:34 PM
Ludwig Wittgenstein
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

AddThis Social Bookmark Button