NVelocity 扩展方法 ASP.NET webform
我想知道是否可以使用 asp.net webforms 和 nvelocity 的扩展方法。如果字符串值为 null 或空,我想设置一些默认值。
.vm 文件示例:
Example of my email body...
Billable Status: $billableStatus.Evaluate()
rest of my email body...
尝试的扩展方法:
public static class Helper
{
public static string Evaluate(this string value)
{
if (String.IsNullOrEmpty(value))
return "Not Provided";
else
return value;
}
}
或者是否有替代方法来完成我想要完成的任务?
I was wondering if it's possible to use an extension method with asp.net webforms and nvelocity. I would like to set some defaults if the string value is null or empty.
Example of .vm file:
Example of my email body...
Billable Status: $billableStatus.Evaluate()
rest of my email body...
Attempted extension method:
public static class Helper
{
public static string Evaluate(this string value)
{
if (String.IsNullOrEmpty(value))
return "Not Provided";
else
return value;
}
}
Or is there an alternative to what I'm tryting to accomplish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为 NVelocity 可以使用 C#/VB.NET 语法糖解析扩展方法。我所做的是在速度上下文中注册一个助手的实例:
然后在您的模板中:
当然,您必须使您的助手成为非静态才能工作。
I don't think NVelocity can resolve extension methods with C#/VB.NET syntax sugar. What I do is register an instance of a helper in the velocity context:
and then in your template:
You have to make your helper non-static for this to work, of course.
我过去遇到过类似的东西,我一直在寻找更复杂、更具控制力的东西。我发现 NVelocity 确实提供了一种拦截方法和属性调用的方法,但为此您必须实现某些功能。为了制作自定义拦截器,您需要实现 NVelocity.IDuck。例如
,现在 MyClass 的任何实例都会拦截方法和属性调用并将其传递给我们的这三个函数实现,并让我们有机会解析并返回输出。您可能从这三个函数签名中注意到,为了实现它们,我们可能需要一些反射,我们可以在可用的扩展类型上找到相应的方法并执行它们。如果需要,您可以阅读以下博客文章,了解有关这种方式的更多详细信息。 NVelocity 和扩展方法
I came across something similar in past and I was looking for something more sophisticated and with more control. I found that NVelocity does provide a way to intercept the method and property calls but for that you will have to implement certain things. In order to make your custom interceptor you will need to implement NVelocity.IDuck. For example
Now any instance of MyClass will intercept and pass the method and property calls to our these three function implementation and give us a chance to resolve and return the output. You may notice from these three function signatures that in order to implement them we may need some reflection where we can locate respective methods on available extension types and execute them. If needed you can read following blog post for more details about going this way. NVelocity and extension methods