首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在InvalidReference中捕获NVelocity错误

如何在InvalidReference中捕获NVelocity错误
EN

Stack Overflow用户
提问于 2009-10-16 12:04:03
回答 1查看 244关注 0票数 5

我正在使用codeplex NVelocity库在.net上,当我在VelocityEngine实例上执行Evalute方法时,如果没有找到模板文本中的参数之一,我希望捕获一个错误。

我怎样才能得到这个?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-19 06:04:09

我找到了解决办法。

我上了EventHandler课程:

代码语言:javascript
复制
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 
}

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

代码语言:javascript
复制
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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1577662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档