NVelocity extension method ASP.NET webform

a 夏天 提交于 2019-12-01 01:45:45

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!