如何捕获 NVelocity 中的 InvalidReference 错误

发布于 2024-08-08 04:22:23 字数 251 浏览 6 评论 0原文

我在 .net 上使用 codeplex NVelocity 库,当我在 VelocityEngine 实例上执行 Evalute 方法并且未找到模板文本中的参数之一时,我想捕获错误。

我怎样才能获得这个?

我在 NVelocity.App.Event 命名空间中找到 IInvalidReferenceEventHandler 接口,但没有找到任何如何使用它的信息。任何帮助将不胜感激。

I am using codeplex NVelocity library on .net and i want to catch an error when I execute Evalute method on VelocityEngine instance and one of the parameter in template text was not found.

How can I obtain this?

I find IInvalidReferenceEventHandler interface in NVelocity.App.Event namespace but I dont't find any information how to use it. Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南城追梦 2024-08-15 04:22:23

我找到了解决方案。

我已经创建了 EventHandler 类:

public class NVelocityEventHandler : IInvalidReferenceEventHandler, IMethodExceptionEventHandler
{
        #region IInvalidReferenceEventHandler Members

        public object InvalidGetMethod(NVelocity.Context.IContext context, string reference, object object_Renamed, string property, NVelocity.Util.Introspection.Info info)
        {
            return "InvalidGetMethod:" + reference;
        }

        public object InvalidMethod(NVelocity.Context.IContext context, string reference, object object_Renamed, string method, NVelocity.Util.Introspection.Info info)
        {
            return "InvalidMethod:" + reference;
        }

        public bool InvalidSetMethod(NVelocity.Context.IContext context, string leftreference, string rightreference, NVelocity.Util.Introspection.Info info)
        {
            return true;
        }

        #endregion

        #region IMethodExceptionEventHandler Members

        object IMethodExceptionEventHandler.MethodException(Type claz, string method, Exception e)
        {
            return "MethodException:" + method;
        }

        #endregion 
}

然后我在下面的代码中使用它:

StringWriter writer = new StringWriter();
NVelocity.App.VelocityEngine eng = new NVelocity.App.VelocityEngine();
try
{
    NVelocityEventHandler te = new NVelocityEventHandler();
    EventCartridge ec = new EventCartridge();
    ec.AddEventHandler(te);
    VelocityContext vc = new VelocityContext((IDictionary)parameters);
    ec.AttachToContext(vc);
    eng.Evaluate(vc, writer, "templatestring", template);
}
catch (ParseErrorException pe)
{
    return pe.Message;
}
catch (MethodInvocationException mi)
{
    return mi.Message;
}

I've found the solution.

I've made EventHandler class:

public class NVelocityEventHandler : IInvalidReferenceEventHandler, IMethodExceptionEventHandler
{
        #region IInvalidReferenceEventHandler Members

        public object InvalidGetMethod(NVelocity.Context.IContext context, string reference, object object_Renamed, string property, NVelocity.Util.Introspection.Info info)
        {
            return "InvalidGetMethod:" + reference;
        }

        public object InvalidMethod(NVelocity.Context.IContext context, string reference, object object_Renamed, string method, NVelocity.Util.Introspection.Info info)
        {
            return "InvalidMethod:" + reference;
        }

        public bool InvalidSetMethod(NVelocity.Context.IContext context, string leftreference, string rightreference, NVelocity.Util.Introspection.Info info)
        {
            return true;
        }

        #endregion

        #region IMethodExceptionEventHandler Members

        object IMethodExceptionEventHandler.MethodException(Type claz, string method, Exception e)
        {
            return "MethodException:" + method;
        }

        #endregion 
}

Then I use it in the code below:

StringWriter writer = new StringWriter();
NVelocity.App.VelocityEngine eng = new NVelocity.App.VelocityEngine();
try
{
    NVelocityEventHandler te = new NVelocityEventHandler();
    EventCartridge ec = new EventCartridge();
    ec.AddEventHandler(te);
    VelocityContext vc = new VelocityContext((IDictionary)parameters);
    ec.AttachToContext(vc);
    eng.Evaluate(vc, writer, "templatestring", template);
}
catch (ParseErrorException pe)
{
    return pe.Message;
}
catch (MethodInvocationException mi)
{
    return mi.Message;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文