Morgeh
Morgeh

Reputation: 679

How can I load a Class contained in a website from a DLL referenced by that website?

Ok so this is alittle bit obscure and I'm not sure its the best way of doing what I'm trying to do but here goes.

Basically I have a Presentation Layer Dll in my web site which handles the Model View Presenter classes. The presentation layer also handles login for my website and then calls off to a web service. Currently whenever the presentation layer calls to a model it verifies the users details and if they are invalid it calls to a loginHandler which redirects the user to the login page. However I cannot dynamically load a new istance of the Login Page in my website from within my Presentation layer.

I've tried to use reflection to dynamically load the class but Since the method call is in the presentation assembly it is only looking within that assembly while the page I want to load is in the website.

heres the reflection code that loads the View:

public ILoginView LoadView()
{
    string viewName = ConfigurationManager.AppSettings["LoginView"].ToString();
    Type type = Type.GetType(viewName, true);
    object newInstance = Activator.CreateInstance(type);
    return newInstance as ILoginView;
} 

Anyone got any suggestions on how to search within the website assembly? Ideal I don't want to tie this implementation into the website specifically as the presentation layer is also used in a WPF application.

Upvotes: 1

Views: 121

Answers (2)

Seattle Leonard
Seattle Leonard

Reputation: 6776

The class of your page is dynamically generated by ASP.NET When it does this, it gives each of the assemblies/types unique names. This is why it is hard to find the type you are looking for.

I actually have a similar problem where I am lookig specifically for these assemblies that only exist in memory.

Here is what I've come up with

Type t = (from asm in AppDomain.CurrentDomain.GetAssemblies()
      from type in asm.GetTypes()
      where type.Name.StartsWith("MyType")
      select type).FirstOrDefault();

If any one knows how to grab a specific assembly that was dynamically created by ASP.NET, I would love to hear it.

Upvotes: 3

mfeingold
mfeingold

Reputation: 7152

What is this object you are trying to instantiate dynamically? If this is just a class than yous should have no problems moving it to the presentation layer dll. If this is a user control - you can use LoadControl method on the Page object

Upvotes: 0

Related Questions