473,382 Members | 1,658 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,382 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 3066
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.