Help with Assembly.GetType

N

nomad

Hi,

I have the following code:

Assembly a = Assembly.LoadFrom("c:\MyAssembly.dll");

The assembly is a project which has numerous different folders with
classes in them. I want to be able to get a hook in to a particular
class within one of these folders. How would I be able to do this?

Any help is much appreciated.
 
A

Arne Vajhøj

I have the following code:

Assembly a = Assembly.LoadFrom("c:\MyAssembly.dll");

The assembly is a project which has numerous different folders with
classes in them. I want to be able to get a hook in to a particular
class within one of these folders. How would I be able to do this?

Type t = a.GetType("My.Name.Space.MyClass");

will give you the type you asked for.

object o = a.CreateInstance("My.Name.Space.MyClass");

will give you an object of that class (assuming public
no arg constructor).

Folders does not matter.

Namespace matters.

Multiple folders may or may not be reflected in multiple
namespaces.

Arne
 
N

nomad

That depends on what you mean.  Are you trying to find the class
according to the folder in which it's contained?  If so, then the answer
is you don't.  The folders you see in the Solution Explorer have nothing
to do with the compiled output.  That information simply doesn't exist
in the assembly.

If you follow the IDE convention, and correlate the folders with
namespaces, then you can organize the types found in the assembly by
namespace, which will then necessarily also match the folders.  But the
moment you or someone else neglects to ensure that every type in every
source file contained in every folder of the solution is declared in the
namespace that corresponds with the folder hierarchy, that approach will
fail to show you the folder hierarchy.

If all you want is to use a type for which you already know the name or
other criteria, then that's easy.  Just call the GetTypes() on the
Assembly instance you've loaded and search for the type you want in the
returned list based on whatever criteria is relevant to your needs
(name, class members, interface implementations, etc.).

Pete

Thanks for the response. I have obviously set up the name spaces in
each and every class but I am new to reflection and am having trouble
with GetTypes. I can load the assembly using Assembly.LoadFrom("D:
\Test\MyAssembly.dll") but when I call GetTypes I am receiving the
following error: {"Could not load file or assembly
'SomeAssembly.Interfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' or one of its dependencies. The system cannot
find the file specified.":"SomeAssembly.Interfaces, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"}. This is one of many dependant
assemblies within the main assembly I loaded above. If anyone can
help explain this error is would be appreciated.
 
A

Arne Vajhøj

Thanks for the response. I have obviously set up the name spaces in
each and every class but I am new to reflection and am having trouble
with GetTypes. I can load the assembly using Assembly.LoadFrom("D:
\Test\MyAssembly.dll") but when I call GetTypes I am receiving the
following error: {"Could not load file or assembly
'SomeAssembly.Interfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' or one of its dependencies. The system cannot
find the file specified.":"SomeAssembly.Interfaces, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"}. This is one of many dependant
assemblies within the main assembly I loaded above. If anyone can
help explain this error is would be appreciated.

Where are SomeAssembly.Interfaces.dll? In same dir as EXE and
MyAssembly.dll ?

Arne
 
N

nomad

Where are SomeAssembly.Interfaces.dll? In same dir as EXE and
MyAssembly.dll ?

Arne

The SomeAssembly.Interfaces.dll is in a completely different
directory. Does this dll and any other dll referenced by my main
assembly have to be in the same directory as my main assembly for me
to be able to reflect over it?
 
A

Arne Vajhøj

The SomeAssembly.Interfaces.dll is in a completely different
directory. Does this dll and any other dll referenced by my main
assembly have to be in the same directory as my main assembly for me
to be able to reflect over it?

Even for refletion depending assemblies need to be loaded.

By far the easiest is if everything is in the same dir.

Otherwise you need to do things with app config or
GAC etc..

Arne
 
N

nomad

Even for refletion depending assemblies need to be loaded.

By far the easiest is if everything is in the same dir.

Otherwise you need to do things with app config or
GAC etc..

Arne

I have never had to use reflection up until now so didn't realise all
the dependant assemblies also had to be in the same directory; this
explains the issues I have been seeing.

Thanks very much Arne.
 
A

Arne Vajhøj

I have never had to use reflection up until now so didn't realise all
the dependant assemblies also had to be in the same directory; this
explains the issues I have been seeing.

It does not have to.

But it sure makes things a lot easier.

Loading via reflection is not that different from loading
the normal way when it comes to location and dependencies.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top