题
我正在使用Codeplex nvelocity 当我在VelocityEngine实例上执行评估方法时,我想在.NET上的库和我想捕获错误,并且找不到模板文本中的参数之一。
我该如何获得这个?
我发现 iinvalidReferenceEventHandler nvelocity.app.event名称空间中的接口,但我找不到任何使用它的信息。任何帮助将不胜感激。
解决方案
我找到了解决方案。
我已经完成了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;
}
不隶属于 StackOverflow