简体   繁体   English

如何从该网站引用的DLL加载网站中包含的类?

[英]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. 基本上,我的网站上有一个Presentation Layer Dll,用于处理Model View Presenter类。 The presentation layer also handles login for my website and then calls off to a web service. 表示层还处理我的网站的登录,然后退出Web服务。 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. 当前,无论何时表示层调用模型,它都会验证用户详细信息,如果无效,它将调用loginHandler将用户重定向到登录页面。 However I cannot dynamically load a new istance of the Login Page in my website from within my Presentation layer. 但是,我无法从我的Presentation层中动态加载网站中登录页面的新位置。

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. 理想的情况是,我不想将此实现特别绑定到网站中,因为WPF应用程序中也使用了表示层。

The class of your page is dynamically generated by ASP.NET When it does this, it gives each of the assemblies/types unique names. 页面的类由ASP.NET动态生成。执行此操作时,它将为每个程序集/类型赋予唯一的名称。 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. 如果有人知道如何获取由ASP.NET动态创建的特定程序集,我很想听听它。

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. 如果这只是一个类,那么将其移到表示层dll时应该没有问题。 If this is a user control - you can use LoadControl method on the Page object 如果这是一个用户控件,则可以在Page对象上使用LoadControl方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM