Type.GetType and ASP.NET 2.0 /App_Code directory

J

johnrlewis

I need to dynamically create an instance of a class that is in the
/App_Code directory. I do not know the name of the class at
design-time. It is provided to me in the form of a string.

If I use Type.GetType("MyClassName"), I get a null reference because I
did not specify an assembly name. I cannot specify an assembly name
because it is dynamically generated at run-time.

This used to be easy in ASP.NET 1.x because everything was compiled
into a single assembly with a known name. In ASP.NET 2.0 each source
file ends up as a seperate assembly with a random name.

I have seen this question asked many times, but have not seen it
answered. Please help.
 
S

Scott Allen

If I use Type.GetType("MyClassName"), I get a null reference because I
did not specify an assembly name. I cannot specify an assembly name
because it is dynamically generated at run-time.

John:

Use BuildManager.GetType() (System.Web.Compilation). The ASP.NET
BuildManager knows how to look through the dynamic assemblies.
 
J

John R. Lewis

Thank you everybody for your replies.

A seperate assembly may be a good idea, but I did not want to stipulate
the users of my library be required to do so.

BuildManager.GetType() worked like a charm. I can specify just the name
of the class with its namespace, leaving out the assembly name, and
BuildManager was able to find the type.

Thanks!!!
 

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

Top