Advertisement
Serginio

Untitled

Feb 15th, 2016
6,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. class ДинамикГК : DynamicObject, IDisposable
  2.  
  3. {
  4.         dynamic ГК1С;
  5.         Type ТипГК;
  6.         object App1C;
  7.  
  8.         void УничтожитьОбъект(object Объект)
  9.         {
  10.             Marshal.Release(Marshal.GetIDispatchForObject(Объект));
  11.             Marshal.ReleaseComObject(Объект);
  12.  
  13.  
  14.         }
  15.   public ДинамикГК(dynamic ГК1С)
  16.         {
  17.             ТипГК = ГК1С.GetType();
  18.             App1C = ГК1С.AppDispatch;
  19.             this.ГК1С = ГК1С;
  20.  
  21.  
  22.         }
  23.     // установка свойства
  24.     public override bool TrySetMember(SetMemberBinder binder, object value)
  25.     {
  26.          try
  27.         {
  28.         ТипГК.InvokeMember(binder.Name, BindingFlags.SetProperty, null, App1C, new object[]{value});
  29.         return true;
  30.         }
  31.          catch (Exception)
  32.          {
  33.          }
  34.          return false;
  35.     }
  36.     // получение свойства
  37.     public override bool TryGetMember(GetMemberBinder binder, out object result)
  38.     {
  39.         try
  40.         {
  41.             result = ТипГК.InvokeMember(binder.Name, BindingFlags.GetProperty, null, App1C, null);
  42.             return true;
  43.         }
  44.         catch (Exception)
  45.         {  
  46.         }
  47.         result = null;
  48.         return false;
  49.     }
  50.     // вызов метода
  51.     public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
  52.     {
  53.        // dynamic method = members[binder.Name];
  54.        // result = method((int)args[0]);
  55.        // return result != null;
  56.         if (binder.Name=="ЗакрытьОбъект")
  57.         {
  58.  
  59.             Dispose();
  60.             result = null;
  61.             return true;
  62.  
  63.         }
  64.  
  65.  
  66.         try
  67.         {
  68.             if (args.Length == 1 && args[0].GetType() == typeof(System.Object[]))
  69.                 result = ТипГК.InvokeMember(binder.Name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, App1C, (System.Object[])args[0]);
  70.             else
  71.                 result = ТипГК.InvokeMember(binder.Name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, App1C, args);
  72.  
  73.             return true;
  74.         }
  75.         catch (Exception)
  76.         {
  77.         }
  78.         result = null;
  79.         return false;
  80.     }
  81.  
  82.     bool disposed = false;
  83.       public void Dispose()
  84.     {
  85.         Dispose(true);
  86.         GC.SuppressFinalize(this);
  87.     }
  88.  
  89.     // Protected implementation of Dispose pattern.
  90.     protected virtual void Dispose(bool disposing)
  91.     {
  92.         if (disposed)
  93.             return;
  94.  
  95.         if (disposing)
  96.         {
  97.             if (ГК1С != null)
  98.             {
  99.                 УничтожитьОбъект(ГК1С);
  100.                 УничтожитьОбъект(App1C);
  101.                 ГК1С = null;
  102.                 App1C = null;
  103.                 ТипГК = null;
  104.                 // Free any other managed objects here.
  105.                 //
  106.                 }
  107.         }
  108.  
  109.         // Free any unmanaged objects here.
  110.         //
  111.         disposed = true;
  112.     }
  113.  
  114.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement