473,473 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create an object via reflection without knowing the assembly

81 New Member
Hey all,
I have a bit of peculiar problem. I need to create an instance of a class, but I do not know which assembly that class belongs to. I do know it is from an assembly loaded within the app domain though. I was hoping I could have it work in the following way:
Given a classpath, attempt to create the object from one of the assemblies currently loaded in the app domain.

Any way of doing this?

Cheers,
Nelson
Feb 6 '09 #1
10 2486
Plater
7,872 Recognized Expert Expert
Can't you open an assembly by a class path?
Or maybe you can open up the collection of assemblies and search for the class inside them?
Feb 6 '09 #2
nelsonbrodyk
81 New Member
hmm interesting. I'm not sure how to do that, or that I could even do that!
Do you know how that works? I'll see what I can find.
Thanks,
Cheers
Feb 6 '09 #3
Plater
7,872 Recognized Expert Expert
Well what do you mean by class path?
Are you saying you know "System.Net.Sockets.Socket" and want to create an instance of Socket, but aren't sure what dll/assembly to open?
You can use the Type class to create an instace that way
Feb 6 '09 #4
nelsonbrodyk
81 New Member
I have a framework that loads other modules we also build. So the class path I have is not a part of mscorelib, or a part of the current executing assembly. However I know the assembly will be loaded within the app domain. So I need a way of going through the assemblies of my current app domain (custom ones really), and seeing if I can create an instance of the class.

ie:
Project A has namespace A.B.C

Project B get s a string that says "A.B.C". Project B then needs to create an instance of "A.B.C", but does not know which assembly it belongs too.
Feb 6 '09 #5
Plater
7,872 Recognized Expert Expert
Well the Type class can be used to create an object from a string "A.B.C", but I think the assembly were "A.B.C" is located has to be in the build.
Unless you "open" it with the Assembly class and load it out of there.

This sounds a lot like plugin developement, I think Balabaster has more experiance doing this and might of better help.
Feb 6 '09 #6
vekipeki
229 Recognized Expert New Member
If your "class path" is equal or somehow related to the actual assembly name (.dll name), then you can load it using Assembly.Load - just pass the full path to the assembly and you're there.

If you want to get a list of currently loaded assemblies (but they have to be loaded, i.e. already accessed by you running assembly!), you can use:

Expand|Select|Wrap|Line Numbers
  1. Assembly[] listOfAssemblies =
  2.      AppDomain.CurrentDomain.GetAssemblies();
  3.  
Note that if your assembly is already loaded, then you have already referenced it somewhere, so loading it using Assembly.Load is a more general way to do it (the way to go when using "plugin" assemblies, as Plater said). Try to google for ".net plugin" and you'll get some examples.
Feb 8 '09 #7
nelsonbrodyk
81 New Member
Correct, this is a plugin framework piece that I'm working on. I can't just use the standard load this directory because we will have more then 1 directory that needs to be loaded.

That being said, is it more efficient in .NET to actually load all the dlls at once, or to use the assembly resolve event, and when it doesn't find a dll, you tell the event what directories to look in, and it loads it then? In this application, some dll's won't even be used unless a user clicks on a certain button, so why load it ahead of time? But if that is the best approach, let me know.

Thanks,
Nelson
Feb 9 '09 #8
nelsonbrodyk
81 New Member
Does anyone know what the performance implications of this are?
Feb 9 '09 #9
vekipeki
229 Recognized Expert New Member
You can only unload assemblies if they are running in a separate app domain (http://msdn.microsoft.com/en-us/libr...01(VS.80).aspx) - so, if you're just loading them using Assembly.Load, they will remain loaded during entire application lifetime.

You could certainly load them only when they are needed (lazy init). If there is several assemblies but you expect only some of them to be loaded, this will use less memory.

You can always expect a short delay the first time an assembly is accessed, even if it is already referenced in your exe, so loading it using Assembly.Load shouldn't make a greater difference (but there is a delay on load, so you can make a quick performance test app to see if you consider it to be annoying).

You can find samples for this (e.g. http://www.codeproject.com/KB/cs/Ass...ppdomains.aspx).
Feb 10 '09 #10
nelsonbrodyk
81 New Member
Awesome thanks. This is what I had assumed. Thanks for the confirmation.
Feb 10 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
2
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType();...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
8
by: J.S. | last post by:
I was under the impression that frames could be used in Windows forms in earlier version of VB. However, in VB 2005 Express I don't see any such tool/control. Is SplitContainer used for this...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
13
by: docschnipp | last post by:
Hi, I have a bunch of object derived from the same base class. They all share the same constructor with some parameters. Now, instead of using a large switch() statement where I call every...
5
by: Sergio Montero | last post by:
I have a MustInherits Base class that implements a custom IDataLayer interfase. IDataLayer expose CRUD methods. Base class constructor requires two parameters: ConnectionString TableName ...
0
by: pso19 | last post by:
Hi, I am trying to access all the properties, methods of an assembly using reflection. But what i want to do is invoke a method which contains custom parametrs like enumeration defined in a...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.