IDisposableAdaptor.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using ILRuntime.CLR.Method;
  3. using ILRuntime.Runtime.Enviorment;
  4. using ILRuntime.Runtime.Intepreter;
  5. namespace ETModel
  6. {
  7. [ILAdapter]
  8. public class IDisposableClassInheritanceAdaptor : CrossBindingAdaptor
  9. {
  10. public override Type BaseCLRType
  11. {
  12. get
  13. {
  14. return typeof (IDisposable);
  15. }
  16. }
  17. public override Type AdaptorType
  18. {
  19. get
  20. {
  21. return typeof (IDisposableAdaptor);
  22. }
  23. }
  24. public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
  25. {
  26. return new IDisposableAdaptor(appdomain, instance);
  27. }
  28. public class IDisposableAdaptor: IDisposable, CrossBindingAdaptorType
  29. {
  30. private ILTypeInstance instance;
  31. private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  32. private IMethod iDisposable;
  33. private readonly object[] param0 = new object[0];
  34. public IDisposableAdaptor()
  35. {
  36. }
  37. public IDisposableAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appDomain, ILTypeInstance instance)
  38. {
  39. this.appDomain = appDomain;
  40. this.instance = instance;
  41. }
  42. public ILTypeInstance ILInstance
  43. {
  44. get
  45. {
  46. return instance;
  47. }
  48. }
  49. public void Dispose()
  50. {
  51. if (this.iDisposable == null)
  52. {
  53. this.iDisposable = instance.Type.GetMethod("Dispose");
  54. }
  55. this.appDomain.Invoke(this.iDisposable, instance, this.param0);
  56. }
  57. public override string ToString()
  58. {
  59. IMethod m = this.appDomain.ObjectType.GetMethod("ToString", 0);
  60. m = instance.Type.GetVirtualMethod(m);
  61. if (m == null || m is ILMethod)
  62. {
  63. return instance.ToString();
  64. }
  65. return instance.Type.FullName;
  66. }
  67. }
  68. }
  69. }