473,397 Members | 2,028 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,397 software developers and data experts.

ASP.NET 2.0 (beta) and Reflection

My Goal:
I want to be able to use reflection to load an object from an assembly.
note: the object name is unknown and passed in from a config file, but
will have a known base class.

My Problem:
the following command sets t as 'null'; the type is not found.
Type t = Type.GetType("Joel.Net.SecurityHttpModule");

Workaround:
I've created some code to locate the assembly that contains the type...

string[] ignoreAsem = { "mscorlib", "System.Web", "System",
"System.Xml", "System.Data", "System.Web.Services", "System.Drawing",
"System.EnterpriseServices", "System.Web.Mobile",
"System.Web.RegularExpressions", "WebDev.WebHost", "msmetabase" };

foreach (Assembly asem in AppDomain.CurrentDomain.GetAssemblies()) {
if (Array.IndexOf(ignoreAsem, asem.GetName().Name) == -1) {
Response.Write(String.Format("{0}<BR>", asem.ToString()));

foreach (Type t in asem.GetTypes()) {
Response.Write(String.Format("&nbsp;&nbsp;{0}<BR>" , t.ToString()));
}
}
}

Once I find the name of the object I want to load, I can call
asem.GetType("Joel.Net.SecurityHttpModule") and it works.

Question:
Is this the right way of doing things? Is there an easier way?

Additional Comments:
If I compile the object and include it in the /bin directory, I know
the assembly name and can load it without any issues. The code example
here is attempting to retrieve an object from the /code directory. I
would like to be able to load either with the same code, in the
simplest method possible.

Nov 18 '05 #1
2 1458

I'm not sure if there's an easier way to get a reference to the assembly,
but once you do you'll want to load the Constructor info and hang on to it
(or the object reference for that matter) so that you don't do this sort of
thing repeatedly.

By caching the ConstructorInfo you can load the assembly quickly.

In general, the pattern for this sort of thing is to include the name of the
assembly the file lives in. If you look at all the dynamically loaded stuff
in web.config, it's always the typename + the dll file so the code knows
where to load from which reduce the amount of searching that has to happen,
since there could be tons of system assemblies loaded.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
<de******@joel.net> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
My Goal:
I want to be able to use reflection to load an object from an assembly.
note: the object name is unknown and passed in from a config file, but
will have a known base class.

My Problem:
the following command sets t as 'null'; the type is not found.
Type t = Type.GetType("Joel.Net.SecurityHttpModule");

Workaround:
I've created some code to locate the assembly that contains the type...

string[] ignoreAsem = { "mscorlib", "System.Web", "System",
"System.Xml", "System.Data", "System.Web.Services", "System.Drawing",
"System.EnterpriseServices", "System.Web.Mobile",
"System.Web.RegularExpressions", "WebDev.WebHost", "msmetabase" };

foreach (Assembly asem in AppDomain.CurrentDomain.GetAssemblies()) {
if (Array.IndexOf(ignoreAsem, asem.GetName().Name) == -1) {
Response.Write(String.Format("{0}<BR>", asem.ToString()));

foreach (Type t in asem.GetTypes()) {
Response.Write(String.Format("&nbsp;&nbsp;{0}<BR>" , t.ToString()));
}
}
}

Once I find the name of the object I want to load, I can call
asem.GetType("Joel.Net.SecurityHttpModule") and it works.

Question:
Is this the right way of doing things? Is there an easier way?

Additional Comments:
If I compile the object and include it in the /bin directory, I know
the assembly name and can load it without any issues. The code example
here is attempting to retrieve an object from the /code directory. I
would like to be able to load either with the same code, in the
simplest method possible.

Nov 18 '05 #2
I was going to go with the method of including the assembly name, but I
also wanted the ability to load objects that are in the /code
directory. After browsing through all the assemblies I found this...

__codejj24v1sl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Joel.Net.SecurityHttpModule

.... this is the assembly I want to load, it has my object in it. But
it looks like code in the /code directory is compiled into an unknown
assembly (meaning I don't know the name). The '__codejj24v1sl' is not
a constant name unfortunately. I believe this happens for partial
compilation.

I only have to grab the Object once, so it won't be used in a loop, I
just created this code as a test.

Nov 18 '05 #3

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

Similar topics

0
by: Eric Raymond | last post by:
When installing the rpms (or the tar file) onto Red Hat Enteprise Linux AS Beta 1 (Taroon), we get the follwing error: Installing all prepared tables /usr/bin/mysql_install_db: line 1: 7690...
48
by: ik | last post by:
ERROR after uninstalling SQL Server 2005 Express I get this message, SQLDMO has not been registered. Please re-run your setupand contact your system administrator. GREAT!!! ReInstalled SQL...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Steen Tøttrup | last post by:
I don't know if there is another newsgroup where I should ask this question..?? I have this .NET web application that I've build using .NET 1.1. Now I've moved it to .NET 2.0 beta 2, and...
5
by: PaulW | last post by:
When I compile the following code with the C++ compiler version 14.00.50215.44 (Microsoft Visual Studio 2005 beta 2) using the command line 'cl -W4 -clr:oldSyntax test.cpp' at run time I get a Null...
1
by: mrkurt | last post by:
I've built a few webservices locally that work just fine. Deployed them to a server and I get the following exception. This exception happens on even the most basic webmethod call. Hitting the...
5
by: Franck | last post by:
Hello, I've just moved to visual developper 2005 to do so, I also had to use the convert assistant. what it did; moving my file resx files that i had in a diresctory called resx to a new...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...
0
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,...
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...
0
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
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...

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.