如何在运行时引用mvc中的程序集? [英] How to reference to assembly in mvc at runtime?

查看:129
本文介绍了如何在运行时引用mvc中的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Asp.Net MVC应用程序中,我有一些视图文件(.cshtml),它引用了一个外部库,它将在运行时加载。所以在应用程序启动后,我通过Assembly.Load加载程序集,我通过自己的自定义ControllerFactory注册控制器,每件事都没问题。



但是,在某些视图中它引用了动态加载的程序集,抛出:



编译器错误消息:CS0234:类型或命名空间名称'MyDynamicNamespace'不存在于命名空间'MyApp'(你是否缺少程序集引用?)

异常,告诉razor编译器无法解析相关的程序集。



我的问题是,有没有办法在运行时注册程序集,剃刀编译器可以访问它并解决它?



请注意我不能使用BuildManager.AddReferencedAssembly方法,因为我的程序集必须在应用程序启动后加载,而BuildManager不支持它。

解决方案

这不是有的没有方式;没有在运行时注册程序集这样的概念。程序集没有注册,它被引用,引用的结果转到编译。



我不知道为什么你决定汇编必须是应用程序启动后加载。最有可能的是,这只是你的幻想,但如果你解释一下,我已经准备好讨论了。同样,BuildManager不支持它是一个完全错误的想法。没有什么可以支持;整个概念不存在,或者更确切地说,这是一种误解。



在运行时,您可以加载任何兼容的程序集。要了解它是如何工作的,请阅读 System.Reflection.Assembly 上的文档:

https://msdn.microsoft.com/en-us/library/system.reflection.assembly%28v= vs.110%29.aspx [ ^ ]。



注意所有汇编。*加载* 方法。



但是,当您加载程序集时,您将使用它做什么?在所有情况下,您都需要进一步使用反射。如果我知道你的目的,我可以解释一下。要获得基本想法,您可以看到我过去的答案:

动态加载用户控件 [ ^ ],

C#现有实例上的Reflection InvokeMember [ ^ ],

通过它的字符串表示从程序集中收集类型 [ ^ ],

访问自定义对象插入dll [ ^ ]。



-SA

In my Asp.Net MVC application, i have some view file (.cshtml) which has reference to an external library which it will be loaded at runtime. so after app started, i load the assembly by Assembly.Load and i register the controllers by my own custom ControllerFactory and every thing is ok.

But, in some views which has references to the dynamically loaded assembly, throws the :

Compiler Error Message: CS0234: The type or namespace name 'MyDynamicNamespace' does not exist in the namespace 'MyApp' (are you missing an assembly reference?)
exception that tells the razor compiler cannot resolve the related assembly.

My question is that, is there a way to register the assembly at runtime, to able the razor compiler can access to it and resolve it?

Notice that i can't use BuildManager.AddReferencedAssembly method because my assembly have to be loaded after app start, and the BuildManager does not support it.

解决方案

It's not that there is no a "way"; there is no such concept as "register the assembly at runtime". The assembly is not "registered", it is referenced, and the result of referencing goes to compilation.

I have no idea why did you decide "assembly have to be loaded after app start". Most likely, this is just you fantasy, but if you explain it, I'm ready to discuss it. Again, "the BuildManager does not support it" is a totally wrong idea. There is nothing to "support"; the whole concept does not exist, or, more exactly, this is a misconception.

During runtime, you can load any compatible assembly. To understand how it works, please read the documentation on System.Reflection.Assembly:
https://msdn.microsoft.com/en-us/library/system.reflection.assembly%28v=vs.110%29.aspx[^].

Pay attention for all the Assembly.*Load* methods.

However, when you load the assembly, what you are going to do with it? In all cases, you would need to further use reflection. I could explain it if I knew your purpose. To get basic idea, you can see my past answers:
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Gathering types from assemblies by it's string representation[^],
Access a custom object that resides in plug in dll[^].

—SA


这篇关于如何在运行时引用mvc中的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆