c# RuntimeBinderException when using dynamic varia

2019-07-29 18:24发布

问题:

I'm using a not-fully-exposed API in the add-on that I'm currently writing. Some classes there are still kept internal, although some public methods require parameters of these types.

To handle the problem I tried to use dynamic typing with a helper DymamicObject implementation. I was pretty successful doing so but at one point I get a RuntimeBinderException, which doesn't tell me the exact details: "The best overloaded method match for Autodesk.Civil.DatabaseServices.PressurePipeNetwork.AddLinePipe(Autodesk.AutoCAD.Geometry.LineSegment3d, Autodesk.Civil.DatabaseServices.Styles.PressurePartSize)' has some invalid arguments".

During debugging, I can see that my dynamic variable is of expected type and it is a valid object in general - contains expected information in its properties.

How could I fix the problem? How could I examine what is exactly wrong with this method call?

Below the current code (the AddLinePipe method is throwing an exception)

var network = (PressurePipeNetwork)tr.GetObject(networkId, OpenMode.ForWrite);

                var pressurePartList = doc.Styles.GetPressurePartListsExt() as StyleCollectionBase;
                var myListId = pressurePartList["My Parts"];
                var myList = tr.GetObject(myListId, OpenMode.ForRead);

                dynamic exposedPartsList = new ExposedPressureObject(myList);
                dynamic myPipes = exposedPartsList.GetParts(PressurePartDomainType.Pipe);
                dynamic myPipesExposed = new ExposedPressureObject(myPipes);
                dynamic pipe = myPipesExposed[0];

                LineSegment3d line = new LineSegment3d(new Point3d(30, 9, 0), new Point3d(33, 8, 0));
                //here is the public method, requirng a variable of internal type
                network.AddLinePipe(line, pipe);

Below implemenation of DymamicObject:

class ExposedPressureObject : DynamicObject
{
    private object _object;
    private IEnumerable<object> _listObject = new List<object>();

    public ExposedPressureObject(object obj)
    {
        System.Collections.IEnumerable enumerable = obj as System.Collections.IEnumerable;
        if (enumerable == null)
            _object = obj;
        else
            _listObject = enumerable.Cast<object>();
    }

    public override bool TryInvokeMember(
            InvokeMemberBinder binder, object[] args, out object result)
    {
        // Find the called method using reflection
        var methodInfo = _object.GetType().GetMethod(
            binder.Name,
            new[] { args[0].GetType() });

        // Call the method
        result = methodInfo.Invoke(_object, args);
        return true;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        PropertyInfo propInfo = _object.GetType().GetProperty(binder.Name,
            BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
        result = propInfo.GetValue(_object , null);
        return true;
    }

    public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
    {
        int index = (int)indexes[0];
        result = _listObject.ToList()[index];
        return true;
    }
}