Loading assemblies by name

  • Thread starter Thread starter Ludwig Wittgenstein
  • Start date Start date
L

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,
 
Ludwig said:
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
 
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 said:
Ludwig said:
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
 
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 said:
Ludwig said:
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 said:
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
 
Back
Top