NVelocity 扩展方法 ASP.NET webform

发布于 2024-10-18 09:50:39 字数 519 浏览 1 评论 0原文

我想知道是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行雁书 2024-10-25 09:50:39

我不认为 N​​Velocity 可以使用 C#/VB.NET 语法糖解析扩展方法。我所做的是在速度上下文中注册一个助手的实例:

var context = VelocityContext();
context.Put("helper", new Helper());
context.Put("billableStatus", "something");
...

然后在您的模板中:

$helper.Evaluate($billableStatus)

当然,您必须使您的助手成为非静态才能工作。

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:

var context = VelocityContext();
context.Put("helper", new Helper());
context.Put("billableStatus", "something");
...

and then in your template:

$helper.Evaluate($billableStatus)

You have to make your helper non-static for this to work, of course.

无人问我粥可暖 2024-10-25 09:50:39

我过去遇到过类似的东西,我一直在寻找更复杂、更具控制力的东西。我发现 NVelocity 确实提供了一种拦截方法和属性调用的方法,但为此您必须实现某些功能。为了制作自定义拦截器,您需要实现 NVelocity.IDuck。例如

    public class MyClass : NVelocity.IDuck
    {
        public object GetInvoke(string propName)
        {
            ....
        }

        public object Invoke(string method, params object[] args)
        {
            ....
        }

        public void SetInvoke(string propName, object value)
        {
            ....
        }
    }

,现在 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

    public class MyClass : NVelocity.IDuck
    {
        public object GetInvoke(string propName)
        {
            ....
        }

        public object Invoke(string method, params object[] args)
        {
            ....
        }

        public void SetInvoke(string propName, object value)
        {
            ....
        }
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文