Answer the question
In order to leave comments, you need to log in
How to get list of types in open ClassLibrary at DesignTime in VisualStudio?
Perhaps the question is worded a little chaotically.
I use a slightly extended custom MvcWebPageRazorHost, the question arose of how to automatically import namespaces (NamespacesImport) into WebPageRazorHost (in order not to write a rather large list in web.config, and for a number of other reasons). This should work at runtime and designtime.
The structure is approximately the following: there is a Core project (let's call it that).
There are two classes inside:
namespace Core
{
public class CustomWebRazorHostFactory : MvcWebRazorHostFactory
{
public override WebPageRazorHost CreateHost(string virtualPath, string physicalPath)
{
return CustomWebPageRazorHost.CreateFromAnother(base.CreateHost(virtualPath, physicalPath));
}
}
class CustomWebPageRazorHost : MvcWebPageRazorHost
{
public static CustomWebPageRazorHost CreateFromAnother(WebPageRazorHost host)
{
var newHost = new CustomWebPageRazorHost(host.VirtualPath, host.PhysicalPath);
newHost.DefaultBaseClass = typeof(CustomWebViewPage<>).FullName.Replace("`1", "");
newHost.DefaultClassName = host.DefaultClassName;
newHost.DefaultDebugCompilation = host.DefaultDebugCompilation;
newHost.DefaultPageBaseClass = typeof(CustomWebViewPage<>).FullName.Replace("`1", "");
newHost.DesignTimeMode = host.DesignTimeMode;
newHost.EnableInstrumentation = host.EnableInstrumentation;
newHost.GeneratedClassContext = host.GeneratedClassContext;
newHost.InstrumentedSourceFilePath = host.InstrumentedSourceFilePath;
newHost.IsIndentingWithTabs = host.IsIndentingWithTabs;
newHost.TabSize = host.TabSize;
foreach (string current in host.NamespaceImports) newHost.NamespaceImports.Add(current);
return newHost;
}
protected CustomWebPageRazorHost(string s1, string s2) : base(s1, s2)
{
try
{
this.NamespaceImports.Add("System.Web.Helpers");
this.NamespaceImports.Add("System.Web.Mvc");
this.NamespaceImports.Add("System.Web.Mvc.Html");
this.NamespaceImports.Add("System.Web.Routing");
this.NamespaceImports.Add("System.Collections");
var namespaces = Utils.ReflectionHelper.GetEngineNamespaces();
foreach (var _namespace in namespaces)
{
this.NamespaceImports.Add(_namespace);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
}
<host factoryType="Core.CustomWebRazorHostFactory, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=..." />
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question