LazyInitializer.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Reflection;
  3. using LinFu.DynamicProxy;
  4. using NHibernate.Engine;
  5. using NHibernate.Proxy.Poco;
  6. using NHibernate.Type;
  7. namespace NHibernate.ByteCode.LinFu
  8. {
  9. [Serializable]
  10. public class LazyInitializer : BasicLazyInitializer, global::LinFu.DynamicProxy.IInterceptor
  11. {
  12. private static readonly MethodInfo exceptionInternalPreserveStackTrace =
  13. typeof (Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic);
  14. public LazyInitializer(string entityName, System.Type persistentClass, object id, MethodInfo getIdentifierMethod,
  15. MethodInfo setIdentifierMethod, IAbstractComponentType componentIdType,
  16. ISessionImplementor session)
  17. : base(entityName, persistentClass, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session) {}
  18. #region Implementation of IInterceptor
  19. public object Intercept(InvocationInfo info)
  20. {
  21. object returnValue;
  22. try
  23. {
  24. returnValue = base.Invoke(info.TargetMethod, info.Arguments, info.Target);
  25. // Avoid invoking the actual implementation, if possible
  26. if (returnValue != InvokeImplementation)
  27. {
  28. return returnValue;
  29. }
  30. returnValue = info.TargetMethod.Invoke(GetImplementation(), info.Arguments);
  31. }
  32. catch (TargetInvocationException ex)
  33. {
  34. exceptionInternalPreserveStackTrace.Invoke(ex.InnerException, new Object[] {});
  35. throw ex.InnerException;
  36. }
  37. return returnValue;
  38. }
  39. #endregion
  40. }
  41. }