[英]How to catch InvalidReference error in NVelocity
我在.net上使用codeplex NVelocity庫,我想在VelocityEngine實例上執行Evalute方法時發現錯誤,並且找不到模板文本中的一個參數。
我怎么能得到這個?
我在NVelocity.App.Event命名空間中找到了IInvalidReferenceEventHandler接口,但我找不到任何有關如何使用它的信息。 任何幫助將不勝感激。
我找到了解決方案。
我做了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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.