473,791 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic type load "problem"

I want to dynamicly load a type (typename is defined in the database).
This type is located in the exe itself or one of the dll's in the directory
with the exe file.
When creating an instance directly it works, when using reflection none of
the dll's is "scanned".
I used following code to work around this problem.
Now I just want to know if this is the/a good way to do it, or should I use
another system.

Private Shared _assemblies As System.Reflecti on.Assembly() = Nothing

Private Shared Function MyGetType(ByVal typeName As String) As Type
Dim result As Type = Type.GetType(ty peName)
If (Not result Is Nothing) Then Return result

If (_assemblies Is Nothing) Then
Dim dir As String =
System.IO.Path. GetDirectoryNam e(System.Window s.Forms.Applica tion.Executable P
ath)
Dim dlls() As String = System.IO.Direc tory.GetFiles(d ir, "*.dll")
ReDim _assemblies(dll s.Length - 1)
For i As Integer = 0 To dlls.Length - 1
_assemblies(i) = System.Reflecti on.Assembly.Loa dFile(dlls(i))
Next i
End If

For Each asm As System.Reflecti on.Assembly In _assemblies
result = asm.GetType(typ eName)
If (Not result Is Nothing) Then Return result
Next asm

Return Nothing
End Function

Nov 20 '05 #1
1 1299
Dominique,

Yes, that will work, but its awfully expensive to load all assemblies when
you need a specific type. Why not use a type name which also qualifies the
assembly name instead? Then, the type loader will dynamically load the
assembly it needs.

For example, the following code will load a type named
"MyNamespace.My CustomType" defined in MyAssembly.dll. As long as
MyAssembly.dll is located in the same folder as the calling assembly (or if
you have a config file) then the CLR loads MyAssembly.dll for you and loads
the type.

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
Dim typeName As String = "MyNamespace.My CustomType, MyAssembly"
Dim t As Type = MyGetType(typeN ame)
Dim o As Object = Activator.Creat eInstance(t)
End Sub

Function MyGetType(ByVal typeName As String) As Type
Dim t As Type
t = [Type].GetType(typeNa me)
Return t
End Function

I hope that helps!

Keith Fink
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights

Nov 20 '05 #2

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

Similar topics

0
2048
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
5
1652
by: Michael Olea | last post by:
Here is a design problem I ran into this am - and I have cleaned the bathroom, scrubbed toilet sink and tub, windexed all glass, mopped the floor, and vacuumed the house - no dice, the problem is still there. Maybe y'all have some ideas? Background ========== The basic idea behind templates, of course, is that much code is independent
1
3300
by: CES | last post by:
All, Could someone please look at this and tell me what's wrong... the function works properly in FireFox but in IE onmouseover it replaces the innerHTML. So the original html: at info@test.com Thank you for your consideration. becomes at mailto:info@test.com:?SUBJECT=Request for additional information - Package id : /*JS var*/ Thank you for your consideration.
3
2903
by: Miquel Labòria | last post by:
When I load a document, it have text "ñ" I recibe an error: Reference to undeclared entity, 'Ntilde' "Ntilde" is an entity of iso-8859-1, why load method can't load it. How can I refer entities ISO-8859-1? Please help me...
0
2988
by: maxim mat | last post by:
Hi I need to build client for web service. But when I'm using Visual Studio .NET to add Web Reference, I get error: "Custom tool error: Unable to import WebService/Schema. Unable to import binding 'controllerSoapBinding' from namespace 'http://Bla-Bla-Bla'. Unable to import operation 'getClient'. The datatype 'Array' is missing. " Some problem when I try to use wsdl.exe tool in .NET. I get: Schema validation warning: Type 'cww:Class' is...
0
1641
by: Norman Yuan | last post by:
I posted this issue a while ago. After moveing the ASP.NET 1.1 app to a brand new server, the same problem still there. System: Winows2003 server std. .NET 1.1 App: Invoicing system Problem: Since the app is dealing with money amount a lot, in many place I used Double.ToString("C") to show a number as money amount on the web page. There
1
4023
by: spolsky | last post by:
try the the following code with Opera 9.01 (Windows). when clicked slightly faster than normal clicking, the toggler checkbox and other checkboxes displays differently although event method works fine to update the checkboxes. there is not any problem with IE 6 or FireFox 1.5. also, i used the double click event method to see if its the source but that does not help even. Opera 9.01 seems to be slow at updating checkboxes visually. am i...
2
4730
by: CWogksch | last post by:
Hello, Everyone... My name is Chris Wogksch. I have a point of sale application developed in VB6 using MS Access 2003 as the database. I've been running versions of this app for over eight years, using vb5/Access 97 and later vb6/Access 2000 and 2003, without issue. Recently, I've started selling my latest version to liquor stores in my local area. The typical installation setup is a peer-to-peer network with three workstations; a...
7
38027
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement using innerHTML. Now, I've researched this problem on the web, and found many references to it, but none of them quite addressed my specific situation, and since my experience with JavaScript is limited, I was not able to adapt the solutions I...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10156
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2916
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.