473,241 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Dynamically Loading Assembly and Accessing its Types (namespaces are different)

Using 3.5, I am stuck in attempting to:

1. Dynamically load an assembly
2. Instantiate a class from that assembly (the client code is in a different
namespace than the namespace of the dynamically loaded assembly)

so far so good (per my code below)... but here is where I'm getting hung up:
3. Call methods of that type (see comments in my code)

If the types in the dynamically loaded assembly were in the same namespace
as the namespace of the code that does the loading, then life would be good.
I could just define an interface and implement it in the dynamically loaded
type. Then the client code could operate on the loaded type as its interface
type. But the namespaces are different (and they need to stay different -
and there cannot be a project reference to the dynamically loaded type's
project).

How do I get around that? How can I call methods of the type when (1) the
client code is in a different namespace and (2) the type gets loaded at
runtime - thus no ability to cast to the necessary type?

//************************************************** ***********************
string assemblyName = "CompanyNameHere.CopyFileInstaller";
string typeName = "CompanyNameHere.Installers.CopyFileInstaller" ; //
includes namespace

System.Reflection.Assembly loadedAssembly =
System.Reflection.Assembly.Load(assemblyName);

Type theType = null;
theType = loadedAssembly.GetType(typeName);

object instance = null; // ultimately I don't want an object type here if
possible
if (theType != null)
{
instance = Activator.CreateInstance(theType); // this works.

// ??? what goes here so I can call methods of the CopyFileInstaller type?
// instance is of object - and I don't know how to cast to the Type
of theType.
// That type is defined a different namespace than this executing
code
// and that namespace is located in an assembly loaded only at
runtime,
// so I can't just set a project reference...
}
//************************************************** ***********************

Thanks!

- "Smithers"
Aug 3 '07 #1
2 3057
Hi,

"Smithers" <A@B.comwrote in message
news:uZ**************@TK2MSFTNGP04.phx.gbl...
Using 3.5, I am stuck in attempting to:

1. Dynamically load an assembly
2. Instantiate a class from that assembly (the client code is in a
different namespace than the namespace of the dynamically loaded assembly)

so far so good (per my code below)... but here is where I'm getting hung
up:
3. Call methods of that type (see comments in my code)

It has nothing to do with the namespace with rather the definition of the
interfaces of those types dynamically loaded.
If those types implement an interface known by the calling code you are ok,
you can cast the CreateInstance to the correct type.
For example if any type implement System.Windows.Form you can access it.

A possible solution in your case is to define a set of interfaces in a
separate DLL that both the calling code as the called code types implement.
Then you can access them using these interfaces.
Aug 3 '07 #2
RE:
<< A possible solution in your case is to define a set of interfaces in a
separate DLL that both the calling code as the called code types implement.
Then you can access them using these interfaces.>>

I just did this and it works beautifully.

A quick followup question:
Is doing the above considered in any way to be a "hack" or somehow a "bad
thing"?

While it creates another project and .dll to maintain (as little as that
maintenance is over time), it seems to be *less* of a hack than using
Reflection (which I also got to work) - at least in my "plugin application".
I'd be interested in your thoughts on this perspective.

Thanks again.

Aug 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Che | last post by:
Greetings, I am writing an application that uses an extendible XML file. in order to validate that XML I use a main XSD and in additional - few more extensions XSD's that uses the types in the...
1
by: Vivek | last post by:
HI Everyone, I am trying to dynamically load a custom deployed assembly from the GAC of a remote machine so that i could call its method from my code using reflection. Can anyone tell me how do i...
1
by: Benjamin | last post by:
Hi, I'm currently writing a Web Services that interacts with a database. To allow me to use not just one database provider (for example, I could use MS Access, SQL Server or MySQL), the Web...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
1
by: mirage83 | last post by:
Hi, Is it true that the CLR does not see any namespaces but only the assembly while executing? can i have two types which have the same name in different namespaces inside the same assembly and...
3
by: Tim Mavers | last post by:
I have created a DLL (class library) in .NET that I need to dynamically load from my ASP.NET application. I am trying to use Assembly.Load(...) to load the file, which is in the /bin directory...
0
by: Ashok Mistry | last post by:
Hi all, I have a problem in loading Assembly dynamically through the Reflection. I want to Load an assembly, say, Main.dll. The Main.dll is dependent on a DLL, say Abc.dll, which is placed in...
1
by: icfai | last post by:
hi friends.... I have got a problem regarding loading of multiple assemblies, actually its required for an editor which implements the intellisenseas in vb or dotnet. for that it is required to...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.