ASP.NET 2.0 (beta) and Reflection

D

deja.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.
 
R

Rick Strahl [MVP]

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/
 
D

deja.com

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top