Click here to Skip to main content
15,891,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900