gpt4 book ai didi

c# - 如何捕获 Velocity 中的无效引用错误

转载 作者:太空狗 更新时间:2023-10-29 23:35:34 25 4
gpt4 key购买 nike

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

我怎样才能得到这个?

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

最佳答案

我找到了解决方案。

我制作了 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;
}

关于c# - 如何捕获 Velocity 中的无效引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1577662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com