title: Running IronPython object from C# with dynamic keyword
body: <p>I have the following IronPython code.</p>
  <pre><code>class Hello:
      def __init__(self):
          pass
      def add(self, x, y):
          return (x+y)
  </code></pre>
  <p>I could make the following C# code to use the IronPython code.</p>
  <pre><code>static void Main()
  {
      
      string source = GetSourceCode(&quot;ipyth.py&quot;);
      Engine engine = new Engine(source);
      ObjectOperations ops = engine._engine.Operations;
  
      bool result = engine.Execute();
      if (!result)
      {
          Console.WriteLine(&quot;Executing Python code failed!&quot;);
      }
      else
      {
          object klass = engine._scope.GetVariable(&quot;Hello&quot;);
          object instance = ops.Invoke(klass);
          object method = ops.GetMember(instance, &quot;add&quot;);
          int res = (int) ops.Invoke(method, 10, 20);
          Console.WriteLine(res);
      }
  
      Console.WriteLine(&quot;Press any key to exit.&quot;);
      Console.ReadLine();
  }
  </code></pre>
  <p>Can I make this code simpler with dynamic DLR?</p>
  <p>The <a href="http://www.ironpythoninaction.com/" rel="nofollow noreferrer">IronPython In Action</a> book has the simple explanation about it at &lt;15.4.4 The future of interacting with dynamic objects&gt;, but I couldn't find some examples.</p>
  <h2>ADDED</h2>
  <p>I attach the source/batch file for the program.
  <a href="http://dl.dropbox.com/u/10773282/2010/Program.cs" rel="nofollow noreferrer">Program.cs</a>
  <a href="http://dl.dropbox.com/u/10773282/2010/runme.bat" rel="nofollow noreferrer">runme.bat</a></p>