473,383 Members | 1,822 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,383 software developers and data experts.

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.Reflection.Assembly() = Nothing

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

If (_assemblies Is Nothing) Then
Dim dir As String =
System.IO.Path.GetDirectoryName(System.Windows.For ms.Application.ExecutableP
ath)
Dim dlls() As String = System.IO.Directory.GetFiles(dir, "*.dll")
ReDim _assemblies(dlls.Length - 1)
For i As Integer = 0 To dlls.Length - 1
_assemblies(i) = System.Reflection.Assembly.LoadFile(dlls(i))
Next i
End If

For Each asm As System.Reflection.Assembly In _assemblies
result = asm.GetType(typeName)
If (Not result Is Nothing) Then Return result
Next asm

Return Nothing
End Function

Nov 20 '05 #1
1 1274
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.MyCustomType" 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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim typeName As String = "MyNamespace.MyCustomType, MyAssembly"
Dim t As Type = MyGetType(typeName)
Dim o As Object = Activator.CreateInstance(t)
End Sub

Function MyGetType(ByVal typeName As String) As Type
Dim t As Type
t = [Type].GetType(typeName)
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
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...
5
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...
1
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...
3
by: Miquel Laḅ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...
0
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...
0
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:...
1
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...
2
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...
7
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.