是否可以在模板中创建类?像...
@{
public class MyClass {
public MyClass() {
Three = new List<string>();
}
public string One { get; set; }
public int Two { get; set; }
public List<string> Three { get; set; }
}
}
目前我收到“无法编译模板。查看错误列表以获取详细信息。”当我尝试这样做时。我想获取 XML 内容并使用 XmlSerializer 在模板中创建 MyClass 的实例。我无法事先进行反序列化并将其插入模型,因为类可能因模板而异。
最佳答案
是的,这是完全可能的。使用 @functions
关键字:
@functions {
public class MyClass {
public MyClass() {
Three = new List<string>();
}
public string One { get; set; }
public int Two { get; set; }
public List<string> Three { get; set; }
}
}
关于c# - Razor 引擎模板中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5129663/