2009-10-16 10 views
5

Używam biblioteki codeplex NVelocity na .NET i chcę złapać błąd, gdy wykonuję metodę Evalute na instancji VelocityEngine i nie znaleziono jednego z parametrów w tekście szablonu.Jak złapać błąd InvalidReference w NVelocity

Jak mogę to uzyskać?

Znajduję interfejs IInvalidReferenceEventHandler w przestrzeni nazw NVelocity.App.Event, ale nie znajduję żadnych informacji, jak go używać. Każda pomoc zostanie doceniona.

Odpowiedz

3

Znalazłem rozwiązanie.

Zrobiłem klasa Podprogram:

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 
} 

Potem go używać w kodzie poniżej:

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; 
}