{"id":252577,"date":"2015-03-06T17:29:03","date_gmt":"2015-03-06T13:29:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=252577"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=252577","title":{"rendered":"\u0413\u0440\u0430\u0431\u043b\u0438, .NET, COM \u0438 dynamic"},"content":{"rendered":"<p>     \t<img decoding=\"async\" src=\"\/\/habrastorage.org\/files\/ae2\/34f\/a38\/ae234fa38d8d4109a43f6f3e4b3c6832.png\" align=\"left\" width=\"250\"\/><\/p>\n<h4>\u0416\u0438\u043b \u2014 \u0431\u044b\u043b \u0434\u0440\u0435\u0432\u043d\u0438\u0439 \u043a\u043e\u0434 \u044d\u043f\u043e\u0445\u0438 \u0434\u0438\u043d\u043e\u0437\u0430\u0432\u0440\u043e\u0432<\/h4>\n<p>  \u0414\u0430\u043d\u043e: \u0430\u0434\u043e\u0432 <i>\u043a\u043e\u0434\u044f\u0440\u043d\u0438\u043a<\/i> \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0441 16\u044e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u00ab\u0430\u0445 \u043a\u0430\u043a\u043e\u0433\u043e\u00bb \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u0430. COM, Interop, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0441\u0438\u0433\u043d\u043b\u0442\u043e\u043d\u044b \u0441 \u0444\u0430\u043a\u0442\u043e\u0440\u044f\u043c\u0438, \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0441 \u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430\u043c\u0438, \u043c\u043e\u0434\u0443\u043b\u0438 \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u043e\u0448\u043c\u0435\u0442\u043a\u0438 <i>\u043a\u0440\u044b\u0432\u0430\u0432\u043e\u0433\u043e \u044b\u043d\u0442\u044b\u0440\u043f\u0440\u0430\u0439\u0437\u0443<\/i>. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440. \u0420\u043e\u0441, \u043c\u0443\u0436\u0430\u043b \u0438 \u043c\u0430\u0442\u0435\u0440\u0435\u043b \u0442\u043e\u0442 \u043a\u043e\u0434\u044f\u0440\u043d\u0438\u043a \u043b\u0435\u0442 \u0441\u0435\u043c\u044c. \u041f\u043e\u043a\u0430 \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0444\u0438\u043a\u0441 \u043d\u0435 \u043f\u0440\u0438\u0432\u0435\u043b \u043a \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0430 \u0432 16 \u043c\u043e\u0434\u0443\u043b\u044f\u0445. \u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u2014 <a href=\"http:\/\/habrahabr.ru\/post\/252103\/\">foreach \u043d\u0430 for<\/a> \u043c\u0435\u043d\u044f\u043b\u0438.<\/p>\n<p>  \u041f\u043e\u043c\u0443\u0447\u0438\u0432\u0448\u0438\u0441\u044c, \u043f\u0440\u043e\u0432\u0435\u043b\u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435. \u041a\u043e\u043f\u0438\u043f\u0430\u0441\u0442 \u043d\u0430 95% \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d, \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043c\u0435\u043d\u0430 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u043e\u043f\u043e\u0432.<\/p>\n<p>  \u0410 \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043a\u0430\u043a-\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u0438 \u0441\u043e\u0442\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0441\u0432\u043e\u0438 \u0432\u0440\u0430\u043f\u043f\u0435\u0440\u044b, \u043f\u043b\u044e\u0441 \u0440\u0443\u0447\u043a\u0430\u043c\u0438 \u0431\u043e\u043a\u0441\u0438\u043d\u0433 \/ \u0430\u043d\u0431\u043e\u043a\u0441\u0438\u043d\u0433 \u044d\u0442\u0438\u0445 \u0432\u0440\u0430\u043f\u043f\u0435\u0440\u043e\u0432?<\/p>\n<p>  \u0415\u0441\u0442\u044c \u0436\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e dynamic!<br \/>  <a name=\"habracut\"><\/a><\/p>\n<p>  \u0418 \u0442\u043e\u0433\u0434\u0430 \u0430\u0434\u0441\u043a\u0438\u0435 \u043c\u0430\u043a\u0430\u0440\u043e\u043d\u044b \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u0447\u0443\u0434\u0435\u0441\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0430  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0443\u0436\u0430\u0441\u0442\u0438\u043a<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>    public abstract class Application : IDisposable     {         public abstract void Close();         public abstract Document CreateDocument();         public abstract Document OpenDocument(string doc_path);  \/\/ \u0435\u0449\u0435 200 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \/\/ \u043a\u0443\u0447\u0430 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0435\u0439 \u0442\u0438\u043f\u0430 \u0432\u0435\u0440\u0441\u0438\u0439, \u043f\u0443\u0442\u0435\u0439 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435                  void IDisposable.Dispose() {             Close();         }     }      public class ClientApplication : Application     {         protected ClientApplication(){             string recovery_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);             recovery_path = Path.Combine(                 recovery_path,                 String.Format(                     @&quot;...\\Version {0}\\en_GB\\Caches\\Recovery&quot;, Version));              try {                 foreach (string file in Directory.GetFiles(recovery_path)){                     try { File.Delete(file); }                     catch { }                 }             }             catch {}  \/\/ \u0435\u0449\u0435 \u043f\u043e\u0434\u043f\u043e\u0440\u043e\u043a \u0438\u0437 \u043f\u0430\u043b\u043e\u043a \u0438 \u0432\u0435\u0440\u0435\u0432\u043e\u043a          }          public override void Close() {             if (Host != null) {                 Marshal.ReleaseComObject(Host);                 Host = null;             }         }     }      public class ClientApplication7_5 : ClientApplication     {         protected ClientApplication7_5() {             Type type = Type.GetTypeFromProgID(&quot;....Application.&quot; + Version, true);             _app = Activator.CreateInstance(type) as Interop75.Application;             Host = app; \/\/ ...         }          public override Document CreateDocument() {             return new ClientDocument7_5(this, _app.Documents.Add());         }          public override Document OpenDocument(string doc_path) {             return new ClientDocument7_5(this, _app.Open(doc_path, true, ...) as Interop75.Document);         }  \/\/ \u0438 \u0435\u0449\u0435 200 \u0432\u0440\u0430\u043f\u043f\u0435\u0440\u043e\u0432          public override ComObject Host { get { return _app; } set { _app = value as Interop75.Application; }  }         private Interop75.Application _app; \/\/ \u0438 \u0435\u0449\u0435 \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0435\u0439 \u0441 \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438 \u043f\u0440\u043e\u0433-\u0430\u0439\u0434\u0438 \u0438 \u043f\u0440\u043e\u0447\u0438\u043c     }      public class ServerApplication : Application     {         public ServerApplication() {} ...     }  \/\/ \u0442\u0430 \u0436\u0435 \u0442\u0440\u0430\u0432\u0430 \u0447\u0442\u043e \u0438 \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442 \u0430\u043f\u043f\u043b\u0438\u043a\u0435\u0439\u0448\u0435\u043d, \u0435\u0449\u0435 8 \u0440\u0430\u0437 <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u043c\u0438, \u0430 \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u0442\u043e \u0431\u0435\u0437\u043e\u0431\u0440\u0430\u0437\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b<\/p>\n<pre><code>var app = Factory.GetApplication(); var doc = app.Documents.Add();  doc.DocumentPreferences.PreserveLayoutWhenShuffling = false; doc.DocumentPreferences.AllowPageShuffle = true; doc.DocumentPreferences.StartPageNumber = 1; <\/code><\/pre>\n<p>  \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u041f\u0440\u043e\u0444\u0438\u0442? \u0423\u0440\u0430, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u0414\u0432\u0430 \u0434\u0435\u0441\u044f\u0442\u043a\u0430 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442 \u043f\u043e\u043b\u0443\u043d\u0430\u0433\u0435\u043d\u0435\u0440\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0436\u0430\u0441\u0442\u0438\u043a\u0430 \u0443\u0434\u0430\u0447\u043d\u043e \u0432\u044b\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u0432 \u043c\u0443\u0441\u043e\u0440\u043a\u0443. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043d\u043e\u0432\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u0439 \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>  <img decoding=\"async\" src=\"\/\/habrastorage.org\/files\/410\/bf4\/463\/410bf4463fed4271825ba5c062cd0ba4.png\" width=\"250\" align=\"left\"\/><\/p>\n<h4>\u041b\u0438\u0442\u043e\u0432\u0441\u043a\u0438\u0439 \u043f\u0440\u0430\u0437\u0434\u043d\u0438\u043a \u00ab\u043e\u0431\u043b\u043e\u043c\u0430\u0439\u0442\u0438\u0441\u00bb<\/h4>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b. \u0411\u0410\u0426! <\/p>\n<p>  \u041d\u0435, \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0442\u043e\u0433\u043e \u043a\u043e\u043c\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 OK \u2014 \u0442\u043e \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u0436\u0435 \u0441\u0443\u043f\u0435\u0440. \u041d\u043e \u0441\u0442\u043e\u0438\u043b\u043e \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0442\u0435\u0441\u0442\u0430<\/p>\n<pre><code>try {     var app = Factory.GetApplication();     var doc = app.Documents.Add();      doc.DocumentPreferences.PreserveLayoutWhenShuffling = false;     doc.DocumentPreferences.AllowPageShuffle = true;     doc.DocumentPreferences.StartPageNumber = -1; } catch (COMException ok) {     .... \/\/ \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0442\u0443\u0442 \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0432 \u043b\u043e\u0433 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c &quot;\u043d\u0438\u0448\u043c\u0430\u0433\u043b\u0430&quot; } catch(Exception bad) {     ... \/\/ \u043c\u044b \u0432\u043e\u0442 \u0442\u0443\u0442, \u0430 bad - \u044d\u0442\u043e NullReferenceException \u0411\u0415\u0417 StackTrace!!! } <\/code><\/pre>\n<p>  \u0428\u043e\u043a, \u0441\u043a\u0430\u043d\u0434\u0430\u043b\u044b, \u0438\u043d\u0442\u0440\u0438\u0433\u0438, \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u2014 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u043d\u044b\u0439 \u0431\u0430\u0433 \u0432 \u043c\u0438\u043a\u0440\u043e\u0441\u043e\u0444\u0442\u0435, \u043f\u043e\u0444\u0438\u043a\u0448\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u0440\u0430\u043d\u0435\u0435 5.0. \u0413\u0440\u0443\u0441\u0442\u043d\u043e \u0438 \u0441\u043a\u0443\u0447\u043d\u043e. <\/p>\n<p>  \u041f\u044b\u0442\u043b\u0438\u0432\u044b\u0439 \u0443\u043c \u043d\u0435 \u0434\u0430\u0435\u0442 \u043f\u043e\u043a\u043e\u044f \u2014 \u0432\u0435\u0434\u044c \u0435\u0441\u043b\u0438 \u0445\u043e\u0434\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u043e\u043f\u044b \u0442\u043e \u0442\u0430\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u043d\u0430\u0434\u043e? \u041e\u0442\u043b\u0430\u0434\u0447\u0438\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u043d\u0430\u0448\u0435\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u043a\u0430\u043a System.__ComObject. \u0410 \u043a\u0430\u043a \u0436\u0435 RCW? \u041f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u043b\u043e?<\/p>\n<p>  \u041c\u0435\u043d\u044f\u0435\u043c \u0442\u0435\u0441\u0442 \u043d\u0430<\/p>\n<pre><code>try {     var app = Factory.GetApplication();     var doc = app.Documents.Add() as Interop75.Document;      doc.DocumentPreferences.PreserveLayoutWhenShuffling = false;     doc.DocumentPreferences.AllowPageShuffle = true;     doc.DocumentPreferences.StartPageNumber = -1; } catch (COMException ok) {     .... \/\/ \u0438 \u043c\u044b \u043e\u043f\u044f\u0442\u044c \u043d\u0430 \u0441\u0432\u043e\u0435\u043c \u043c\u0435\u0441\u0442\u0435 } catch(Exception bad) {     ...  } <\/code><\/pre>\n<p>  \u0438\u2026 \u0442\u0435\u0441\u0442 \u043f\u0440\u043e\u0439\u0434\u0435\u043d.<\/p>\n<p>  \u0413\u0438\u043f\u043e\u0442\u0435\u0437\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430. \u0422\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u043e\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0442\u0438\u043f? \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c<\/p>\n<pre><code>    var app = Factory.GetApplication();     var doc = app.Documents.Add();      var typeName = Microsoft.VisualBasic.Information.TypeName(doc); <\/code><\/pre>\n<p>  \u0425\u043c \u0445\u043c. \u0412\u043f\u043e\u043b\u043d\u0435 \u0441\u0435\u0431\u0435.<\/p>\n<p>  \u0418\u0434\u0435\u0438 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438\u0441\u044c. <\/p>\n<p>  \u041d\u043e \u043f\u043e\u0441\u0442\u043e\u0439\u0442\u0435 \u2014 \u0435\u0441\u0442\u044c \u0436\u0435 \u0441\u044b\u0440\u0446\u044b? \u0421\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0443\u0440\u0438\u043c, \u0432\u043e\u0441\u0445\u0438\u0449\u0430\u0435\u043c\u0441\u044f \u043c\u0430\u0441\u0442\u0435\u0440\u0441\u0442\u0432\u0443 \u0437\u0430\u043f\u0443\u0442\u044b\u0432\u0430\u043d\u0438\u044f. \u041d\u0430\u0447\u0430\u043b\u0438 \u043e\u0442\u0441\u044e\u0434\u0430: <a href=\"http:\/\/www.dotnetframework.org\/default.aspx\/4@0\/4@0\/DEVDIV_TFS\/Dev10\/Releases\/RTMRel\/ndp\/clr\/src\/BCL\/System\/__ComObject@cs\/1305376\/__ComObject@cs\">__ComObject<\/a>. \u041f\u043b\u0430\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0442\u0435\u043a\u043b\u0438 \u0441\u044e\u0434\u0430: <a href=\"http:\/\/referencesource.microsoft.com\/#mscorlib\/system\/type.cs\">Type.cs<\/a>. \u0417\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 ildasm. \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043a\u0443\u0440\u0435\u0432\u0430 \u043f\u0440\u0438\u0448\u043b\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u2014 \u0442\u0430\u043a \u0442\u0430\u043c \u044f\u0432\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0441\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u044d\u0442\u0438 \u043a\u043e\u043c\u044b \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0410 \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0435\u0441\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c<\/p>\n<pre><code>doc.DocumentPreferences.StartPageNumber = -1; <\/code><\/pre>\n<p>  \u043d\u0430<\/p>\n<pre><code>Type type = doc.DocumentPreferences.GetType();     type.InvokeMember(&quot;StartPageNumber&quot;, BindingFlags.SetProperty, null, doc.DocumentPreferences, new object[] { -1 }); <\/code><\/pre>\n<p>  \u041f\u043e \u0438\u0434\u0435\u0435 \u2014 \u043d\u0438\u0447\u0435\u0433\u043e?<\/p>\n<p>  <img decoding=\"async\" src=\"\/\/habrastorage.org\/files\/a27\/a54\/5d0\/a27a545d0af844cb8f138312bb9f7fbf.png\" align=\"left\" width=\"250\"\/><\/p>\n<h4>\u0413\u0430\u043b\u0430\u043d\u0442\u0435\u0440\u0435\u0439\u0449\u0438\u043a \u0438 \u043a\u0430\u0440\u0434\u0438\u043d\u0430\u043b \u2014 \u044d\u0442\u043e \u0441\u0438\u043b\u0430<\/h4>\n<p>  \u0410 \u0432\u043e\u0442 \u0438 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f. \u0422\u0435\u0441\u0442 \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u043e\u0439\u0434\u0435\u043d. \u0418 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c? \u041f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439 \u043a\u043e\u0434 \u0432 \u043c\u0430\u043a\u0430\u0440\u043e\u043d\u044b \u2014 \u043d\u0435 \u0443\u043b\u044b\u0431\u0430\u0435\u0442\u0441\u044f, \u0434\u0430 \u0438 \u043c\u043d\u043e\u0433\u043e \u0435\u0433\u043e.<\/p>\n<p>  \u041f\u043e\u0437\u0434\u043d\u043e, \u0432\u0435\u0447\u0435\u0440, \u043f\u044b\u0442\u0430\u044e\u0441\u044c \u0442\u043e\u043b\u0441\u0442\u043e <i>\u043f\u043e\u0442\u0440\u043e\u043b\u043b\u0438\u0442\u044c<\/i> \u0438 \u0440\u0430\u0437\u0440\u044f\u0434\u0438\u0442\u044c \u043e\u0431\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u2014 \u0442\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u043a\u043e\u0432 \u043f\u043e\u0434\u0441\u0443\u043d\u0435\u043c \u2014 \u043d\u0430 \u0440\u0435\u0444\u043b\u0435\u043a\u0442\u0430\u0445? \u0415\u0449\u0435 \u043d\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0432 \u043c\u044b\u0441\u043b\u044c \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u2014 \u0430 \u044d\u0442\u043e \u043c\u044b\u0441\u043b\u044c!<\/p>\n<p>  \u041f\u0440\u043e\u0431\u0443\u0435\u043c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ComWrapper extends DynamicObject<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>public class ComWrapper : DynamicObject { \tpublic ComWrapper(object comObject) { \t\t_comObject = comObject; \t\t_type = _comObject.GetType(); \t}  \tpublic object WrappedObject { get { return _comObject; } } \/\/ \u0432\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0434\u043e  \/\/ \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u044f \u0433\u0435\u0442 + \u0441\u0435\u0442 \tpublic override bool TryGetMember(GetMemberBinder binder, out object result) { \t\tresult = Wrap(_type.InvokeMember(binder.Name, BindingFlags.GetProperty, null, _comObject, null)); \t\treturn true; \t}  \tpublic override bool TrySetMember(SetMemberBinder binder, object value) { \t\t_type.InvokeMember( \t\t\tbinder.Name, BindingFlags.SetProperty, null, _comObject, \t\t\tnew object[] { Unwrap(value) }\t); \t\treturn true; \t}  \/\/ \u0442\u0430 \u0436\u0435 \u0442\u0440\u0430\u0432\u0430 \u043f\u0440\u043e \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 \t public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { \t\tresult = Wrap(_type.InvokeMember( \t\t\tbinder.Name, BindingFlags.InvokeMethod, null, _comObject, \t\t\targs.Select(arg =&gt; Unwrap(arg)).ToArray() \t\t)); \t\treturn true; \t}  \/\/ \u043d\u0430\u0448 \u0440\u0443\u0447\u043d\u043e\u0439 \u0431\u043e\u043a\u0441\u0438\u043d\u0433 - \u0430\u043d\u0431\u043e\u043a\u0441\u0438\u043d\u0433 \tprivate object Wrap(object obj) { \t\treturn obj != null && obj.GetType().IsCOMObject ? new ComWrapper(obj) : obj; \t}  \tprivate object Unwrap(object obj) { \t\tComWrapper wrapper = obj as ComWrapper; \t\treturn wrapper != null ? wrapper._comObject : obj; \t}  \/\/ \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0442\u043e \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 + \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0441\u0442\u043e \u0440\u0430\u0437 \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \tprivate object _comObject; \tprivate Type _type; } <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u2014 \u0432\u0441\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0441\u0430\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u043d\u0430\u0434\u043e, \u0432\u0441\u0435 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u2014 \u044d\u0442\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 Factory.GetApplication(). \u041f\u0440\u044f\u043c\u043e \u0442\u0430\u043c \u0438 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c. \u0415\u0441\u0442\u044c \u043f\u0440\u0430\u0432\u0434\u0430 \u043d\u044e\u0430\u043d\u0441 \u2014 \u0437\u0430\u0431\u044b\u043b\u0438 \u043f\u0440\u043e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0422\u0430\u043a \u0447\u0442\u043e \u0447\u0443\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0435\u0449\u0435 \u0438 \u0442\u0430\u043a\u043e\u0435:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u043f\u043e\u0440\u043e\u043a<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>\/\/ \u043d\u0430\u0448 \u0435\u043d\u0443\u043c\u0435\u0440\u0430\u0442\u043e\u0440 \u043d\u0430 \u043a\u043e\u043b\u0435\u043d\u043a\u0435 \tprivate IEnumerable Enumerate() { \t\tforeach (var item in (IEnumerable)_comObject) \t\t\tyield return Wrap(item); \t}  \/\/ \u0430\u0432\u0442\u043e\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u043a enumerable \tpublic override bool TryConvert(ConvertBinder binder, out object result) { \t\tif (binder.Type.Equals(typeof(IEnumerable)) && _comObject is IEnumerable) { \t\t\tresult = Enumerate(); \t\t\treturn true; \t\t} \t\tresult = null; \t\treturn false; \t}  \/\/ \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u0430\u043a \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443. \u041d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \tpublic override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)\t{ \t\tif (indexes.Length == 1) { \t\t\tdynamic indexer = _comObject; \t\t\tresult = Wrap(indexer[indexes[0]]); \t\t\treturn true; \t\t}  \t\tresult = null; \t\treturn false; \t}  \tpublic override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)\t{ \t\tif (indexes.Length == 1) { \t\t\tdynamic indexer = _comObject; \t\t\tindexer[indexes[0]] = Unwrap(value); \t\t\treturn true; \t\t} \t\treturn false; \t} <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0412\u043e\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u2014 \u043f\u043e\u0431\u0435\u0434\u0430.<\/p>\n<p>  \u0412\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f.      \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/252371\/\"> http:\/\/habrahabr.ru\/post\/252371\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t<img decoding=\"async\" src=\"\/\/habrastorage.org\/files\/ae2\/34f\/a38\/ae234fa38d8d4109a43f6f3e4b3c6832.png\" align=\"left\" width=\"250\"\/><\/p>\n<h4>\u0416\u0438\u043b \u2014 \u0431\u044b\u043b \u0434\u0440\u0435\u0432\u043d\u0438\u0439 \u043a\u043e\u0434 \u044d\u043f\u043e\u0445\u0438 \u0434\u0438\u043d\u043e\u0437\u0430\u0432\u0440\u043e\u0432<\/h4>\n<p>  \u0414\u0430\u043d\u043e: \u0430\u0434\u043e\u0432 <i>\u043a\u043e\u0434\u044f\u0440\u043d\u0438\u043a<\/i> \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0441 16\u044e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u00ab\u0430\u0445 \u043a\u0430\u043a\u043e\u0433\u043e\u00bb \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u0430. COM, Interop, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0441\u0438\u0433\u043d\u043b\u0442\u043e\u043d\u044b \u0441 \u0444\u0430\u043a\u0442\u043e\u0440\u044f\u043c\u0438, \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0441 \u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430\u043c\u0438, \u043c\u043e\u0434\u0443\u043b\u0438 \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u043e\u0448\u043c\u0435\u0442\u043a\u0438 <i>\u043a\u0440\u044b\u0432\u0430\u0432\u043e\u0433\u043e \u044b\u043d\u0442\u044b\u0440\u043f\u0440\u0430\u0439\u0437\u0443<\/i>. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440. \u0420\u043e\u0441, \u043c\u0443\u0436\u0430\u043b \u0438 \u043c\u0430\u0442\u0435\u0440\u0435\u043b \u0442\u043e\u0442 \u043a\u043e\u0434\u044f\u0440\u043d\u0438\u043a \u043b\u0435\u0442 \u0441\u0435\u043c\u044c. \u041f\u043e\u043a\u0430 \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0444\u0438\u043a\u0441 \u043d\u0435 \u043f\u0440\u0438\u0432\u0435\u043b \u043a \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0430 \u0432 16 \u043c\u043e\u0434\u0443\u043b\u044f\u0445. \u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u2014 <a href=\"http:\/\/habrahabr.ru\/post\/252103\/\">foreach \u043d\u0430 for<\/a> \u043c\u0435\u043d\u044f\u043b\u0438.<\/p>\n<p>  \u041f\u043e\u043c\u0443\u0447\u0438\u0432\u0448\u0438\u0441\u044c, \u043f\u0440\u043e\u0432\u0435\u043b\u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435. \u041a\u043e\u043f\u0438\u043f\u0430\u0441\u0442 \u043d\u0430 95% \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d, \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043c\u0435\u043d\u0430 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u043e\u043f\u043e\u0432.<\/p>\n<p>  \u0410 \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043a\u0430\u043a-\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u0438 \u0441\u043e\u0442\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0441\u0432\u043e\u0438 \u0432\u0440\u0430\u043f\u043f\u0435\u0440\u044b, \u043f\u043b\u044e\u0441 \u0440\u0443\u0447\u043a\u0430\u043c\u0438 \u0431\u043e\u043a\u0441\u0438\u043d\u0433 \/ \u0430\u043d\u0431\u043e\u043a\u0441\u0438\u043d\u0433 \u044d\u0442\u0438\u0445 \u0432\u0440\u0430\u043f\u043f\u0435\u0440\u043e\u0432?<\/p>\n<p>  \u0415\u0441\u0442\u044c \u0436\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e dynamic!  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-252577","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/252577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=252577"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/252577\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}