C# 无法加载类型';HtmlHelper`1';从组装
我正在尝试编译Razor模板,但尽管它编译,但在运行时失败,原因是:C# 无法加载类型';HtmlHelper`1';从组装,c#,.net,asp.net-mvc,razor,mono,C#,.net,Asp.net Mvc,Razor,Mono,我正在尝试编译Razor模板,但尽管它编译,但在运行时失败,原因是: System.TypeLoadException: Could not load type 'HtmlHelper`1' from assembly '/Users/oligofren/src/razor-cli/build/System.Web.Mvc.dll'. 我应该如何着手解决这个问题 我不太熟悉.NET的核心(这里是Mono版本),所以我不能说我在这里是否做错了什么。我已将所有程序集添加到build文件夹(exe
System.TypeLoadException: Could not load type 'HtmlHelper`1' from assembly '/Users/oligofren/src/razor-cli/build/System.Web.Mvc.dll'.
我应该如何着手解决这个问题
我不太熟悉.NET的核心(这里是Mono版本),所以我不能说我在这里是否做错了什么。我已将所有程序集添加到build文件夹(exe在其中结束),并且在RazorEngine尝试编译程序集之前,我还尝试手动加载所需的程序集
我如何解决这个问题
完整源代码
// See also tips on building cli apps with razorengine: https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Hosts.Console/RazorEngine.Hosts.Console.csproj
using System;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using Moq;
using System.IO;
using Newtonsoft.Json.Linq;
using RazorEngine;
using RazorEngine.Templating; // For extension methods.
using RazorEngine.Configuration;
using RazorEngine.Text;
public class RazorCli
{
static public void Main (string[] args)
{
CheckCommandLine(args);
string template = ReadFile(args[0]);
JObject model = ParseModel(args[1]);
// try to load the required assemblies
//http://stackoverflow.com/a/23496144/200987
System.Reflection.Assembly.Load("System.Web");
System.Reflection.Assembly.Load("System.Web.Mvc");
var result = CompileTemplate(template, model);
Console.WriteLine (result);
}
private static string CompileTemplate (string template, JObject model)
{
string res = "";
var config = new TemplateServiceConfiguration();
// You can use the @inherits directive instead (this is the fallback if no @inherits is found).
config.BaseTemplateType = typeof(MyClassImplementingTemplateBase<>);
try
{
using (var service = RazorEngineService.Create(config))
{
res = service.RunCompile(template, "templateKey", null, model);
}
}
catch( RazorEngine.Templating.TemplateCompilationException ex )
{
Console.WriteLine (ex);
System.Environment.Exit(1);
}
return res;
}
/* Cannot dispatch a dynamic object to extension methods */
private static JObject ParseModel(string fileName){
string json = ReadFile(fileName);
return JObject.Parse(json);
}
private static void CheckCommandLine(string[] args){
if(args.Length != 2){
Usage();
System.Environment.Exit(1);
}
}
private static void Usage(){
string usage = "Usage: razor-cli <partial.cshtml> <model.json>\n";
Console.WriteLine(usage);
}
private static String ReadFile(string filename)
{
string result;
using (StreamReader sr = new StreamReader(filename))
{
result = sr.ReadToEnd();
}
return result;
}
}
public class MyHtmlHelper
{
public IEncodedString Raw(string rawString)
{
return new RawString(rawString);
}
}
// https://antaris.github.io/RazorEngine/TemplateBasics.html
public abstract class MyClassImplementingTemplateBase<T> : TemplateBase<T>
{
public MyClassImplementingTemplateBase()
{
Html = MvcHelpers.CreateHtmlHelper<Object>();
}
public HtmlHelper Html { get; set; }
}
// Ripped straight from a SO Q/A
// http://stackoverflow.com/questions/17271688/mocking-viewcontext-to-test-validation-error-messages
public class MvcHelpers {
public static HtmlHelper<TModel> CreateHtmlHelper<TModel>(ViewDataDictionary dictionary = null)
{
if (dictionary == null)
dictionary = new ViewDataDictionary { TemplateInfo = new TemplateInfo() };
var mockViewContext = new Mock<ViewContext>(
new ControllerContext(
new Mock<HttpContextBase>().Object,
new RouteData(),
new Mock<ControllerBase>().Object),
new Mock<IView>().Object,
dictionary,
new TempDataDictionary(),
new Mock<TextWriter>().Object);
var mockViewDataContainer = new Mock<IViewDataContainer>();
mockViewDataContainer.Setup(v => v.ViewData).Returns(dictionary);
return new HtmlHelper<TModel>(mockViewContext.Object, mockViewDataContainer.Object);
}
}
//另请参见使用razorengine构建cli应用程序的提示:https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Hosts.Console/RazorEngine.Hosts.Console.csproj
使用制度;
使用System.Web;
使用System.Web.Routing;
使用System.Web.Mvc;
使用最小起订量;
使用System.IO;
使用Newtonsoft.Json.Linq;
使用RazorEngine;
使用RazorEngine.Templating;//对于扩展方法。
使用RazorEngine.配置;
使用RazorEngine.Text;
公共级RazorCli
{
静态公共void Main(字符串[]args)
{
检查命令行(args);
字符串模板=ReadFile(args[0]);
JObject model=ParseModel(args[1]);
//尝试加载所需的程序集
//http://stackoverflow.com/a/23496144/200987
System.Reflection.Assembly.Load(“System.Web”);
System.Reflection.Assembly.Load(“System.Web.Mvc”);
var结果=CompileTemplate(模板、模型);
Console.WriteLine(结果);
}
私有静态字符串编译器模板(字符串模板、作业对象模型)
{
字符串res=“”;
var config=new TemplateServiceConfiguration();
//您可以改为使用@inherits指令(如果未找到@inherits,则这是回退)。
config.BaseTemplateType=typeof(MyClassImplementingTemplateBase);
尝试
{
使用(var service=RazorEngineService.Create(config))
{
res=service.RunCompile(模板,“templateKey”,null,model);
}
}
catch(RazorEngine.Templating.TemplateCompilationException ex)
{
Console.WriteLine(ex);
系统、环境、出口(1);
}
返回res;
}
/*无法将动态对象分派给扩展方法*/
私有静态JObject ParseModel(字符串文件名){
字符串json=ReadFile(文件名);
返回JObject.Parse(json);
}
私有静态void检查命令行(字符串[]args){
如果(参数长度!=2){
用法();
系统、环境、出口(1);
}
}
私有静态void用法(){
string usage=“用法:razor cli\n”;
控制台写入线(用法);
}
私有静态字符串读取文件(字符串文件名)
{
字符串结果;
使用(StreamReader sr=新StreamReader(文件名))
{
结果=sr.ReadToEnd();
}
返回结果;
}
}
公共类MyHtmlHelper
{
公共IceNodeString原始(字符串原始字符串)
{
返回新的RawString(RawString);
}
}
// https://antaris.github.io/RazorEngine/TemplateBasics.html
公共抽象类MyClassImplementingTemplateBase:TemplateBase
{
public MyClassImplementingTemplateBase()
{
Html=MvcHelpers.CreateHtmlHelper();
}
公共HTMLHelperHTML{get;set;}
}
//直接从SO Q/a中删除
// http://stackoverflow.com/questions/17271688/mocking-viewcontext-to-test-validation-error-messages
公共级MvcHelpers{
公共静态HtmlHelper CreateHtmlHelper(ViewDataDictionary dictionary=null)
{
if(dictionary==null)
dictionary=new ViewDataDictionary{TemplateInfo=new TemplateInfo()};
var mockViewContext=new Mock(
新控制器上下文(
新建Mock().Object,
新路由数据(),
新建Mock().Object),
新建Mock().Object,
字典,
新建TempDataDictionary(),
新建Mock().Object);
var mockViewDataContainer=new Mock();
mockViewDataContainer.Setup(v=>v.ViewData).Returns(dictionary);
返回新的HtmlHelper(mockViewContext.Object、mockViewDataContainer.Object);
}
}
如果有帮助的话,可以在中查看有关如何运行此功能的详细信息
更多详细信息
在OS X 10.11.4上使用自制软件安装了Mono 4.2.2.0。这是什么版本的Mono,您是如何安装的?请在每次出现mono问题时提供此信息,thanks@knocte我已经在问题中添加了这方面的信息。它在OS X.btw上是4.2.2,问题可能是mono特有的,但最终我希望能够在正常的Windows.NET运行时和mono运行时中运行cli应用程序。这是什么版本的mono,您是如何安装的?请在每次出现mono问题时提供此信息,thanks@knocte我已经在问题中添加了这方面的信息。它是OS X.btw上的4.2.2版本,问题可能是特定于mono的,但最终我希望在普通Windows.NET运行时和mono运行时中都能运行cli应用程序。