2

I can't get a dynamic method on an ExpandoObject to be directly callable in VB.NET. In C# the following works:

dynamic obj = new System.Dynamic.ExpandoObject();
var called = false;

obj.ForceRefresh = new Action(() => called = true);

obj.ForceRefresh();

I'd have thought the same thing in vb.net would be:

 Dim called = False
 Dim obj As Object = New Dynamic.ExpandoObject

 obj.ForceRefresh = New Action(Sub() called = True)

 obj.ForceRefresh() 'No default member found for type 'Action'.
 obj.ForceRefresh.Invoke() 'this works

the obj.ForceRefresh throws 'No default member found for type 'Action'

It works if I put an Invoke, but that's not an option (this is a very simplified example of a moq unit test, the objects being tested will call the functions so I can't change them)

Is there a way of setting up the dynamic method in vb.net so I can call it without the invoke?

NDJ
  • 5,189
  • 1
  • 18
  • 27

1 Answers1

1

There doesn't seem to be a way around this - you need to use "Invoke" to call dynamically-added methods in VB.

The following Microsoft link showing C# vs VB examples also confirms this: http://msdn.microsoft.com/en-ca/library/system.dynamic.expandoobject.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28