KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to give a short example of IDynamicMetaObjectProvider for the second edition of C# in Depth, and I'm running into issues. I want to be able to express a void call, and I'm failing. I'm sure it's possible, because if I dynamically call a void method using the reflection binder, all is fine. Here's a short but complete example: using System; using System.Dynamic; using System.Linq.Expressions; class DynamicDemo : IDynamicMetaObjectProvider { public DynamicMetaObject GetMetaObject(Expression expression) { return new MetaDemo(expression, this); } public void TestMethod(string name) { Console.WriteLine(name); } } class MetaDemo : DynamicMetaObject { internal MetaDemo(Expression expression, DynamicDemo demo) : base(expression, BindingRestrictions.Empty, demo) { } public override DynamicMetaObject BindInvokeMember (InvokeMemberBinder binder, DynamicMetaObject[] args) { Expression self = this.Expression; Expression target = Expression.Call (Expression.Convert(self, typeof(DynamicDemo)), typeof(DynamicDemo).GetMethod("TestMethod"), Expression.Constant(binder.Name)); var restrictions = BindingRestrictions.GetTypeRestriction (self, typeof(DynamicDemo)); return new DynamicMetaObject(target, restrictions); } } class Test { public void Foo() { } static void Main() { dynamic x = new Test(); x.Foo(); // Works fine! x = new DynamicDemo(); x.Foo(); // Throws } } This throws an exception: Unhandled Exception: System.InvalidCastException: The result type 'System.Void' of the dynamic binding produced by the object with type 'DynamicDemo' fo
Tags (comma-separated)
Save Edits
Cancel