DynamicAccess is a library to aid connecting DLR languages such as ironpython and ironruby to non-dynamic languages like managed C++. It also fills in some gaps in the current C# support of dynamic objects, such as member access by string and deletion of members or indexes.

Only the Dynamic assembly is required, however there is additional code to test and demonstrate how to use the library. The samples below demonstrate some of the potential uses.

The source code, though not extensive, is also a useful resource for those who need to learn how to use some of the powerful features of the DLR.

  

Previously Executed Python Code:

import sys

class PyTest(object):
   def __init__(self, p1=None):
     self.p1 = p1
   @staticmethod
   def StaticTest1():
     return "Test"
   @staticmethod
   def StaticTest2(x):
     return 6*x
   def InstTest1(self):
     return self.p1

 

Short C# Sample:


var PyTestType = scope.GetVariable("PyTest");
var PyTestInstance = Accessors.CreateInstance(PyTestType, "xx");
var prop = Accessors.GetMember(PyTestInstance, "p1");
Accessors.SetMember(PyTestInstance, "p2", "xxx");
Accessors.DeleteMember(PyTestInstance, "p2");
var n = Accessors.CallMember(PyTestInstance, "InstTest1");

  

Managed C++ sample using the dynamic wrapper:


// Get the builtins dictionary
DynWrap^ builtins = gcnew DynWrap(scope->GetVariable("__builtins__"));
// index it
DynWrap^ hasattr = builtins->I("hasattr");
// get our test type
DynWrap^ PyTestType = gcnew DynWrap(scope->GetVariable("PyTest"));
// check if it has a particular attribute
bool bVal = hasattr->C(PyTestType, "InstTest1");
// call a static method
int rtn = PyTestType->CM("StaticTest2", 10);
// construct an instance
DynWrap^ PyTestInst = PyTestType->CC("xyzzy");
// get a member of an instance
String^ p1 = PyTestInst->M("p1");

Last edited Jan 5, 2011 at 3:48 AM by hevok, version 19