« T-Mobile web'n'walk stick with Snow LeopardOdd behavior of C# 4.0 "dynamic" »

More odd behavior - DynamicObject this time

16/02/10

Permalink 05:52:21 pm
Categories: .NET

More odd behavior - DynamicObject this time

I posted some sample code recently, for some tests with C# 4.0 dynamic. Now I’ve found a new interesting thing – perhaps this is also related to using beta 2 instead of the RC, again I’ll try this when I get around to it.

The thing is, I have created my own dynamic object:

public class MyDynamicType : DynamicObject {
  public override bool TryInvokeMember(InvokeMemberBinder binder, 
    object[] args, out object result) {
    Console.WriteLine("TryInvokeMember {0}", binder.Name);

    result = null;
    return true;
  }

  public override bool TryGetMember(GetMemberBinder binder, out object result) {
    Console.WriteLine( "TryGetMember " + binder.Name);
    return base.TryGetMember(binder, out result);
  }
}

Very simple. Now, you already know my little DoFall helper function. I have this code in my main program:

dynamic myObject = new MyDynamicType();
myObject.Fall( );
DoFall(myObject);

Not surprisingly, this is the result:

TryInvokeMember Fall
TryInvokeMember Fall

Now I add my previous tests back into the main method, so I have this code altogether:

DoFall(new Leaf());
DoFall(new ExchangeRate());
DoFall(42);

dynamic myObject = new MyDynamicType();
myObject.Fall( );
DoFall(myObject);

What do you think the result is going to be??? Wrong! Here’s what I get:

Leaf is falling
Exchange rate is falling
Turns out 42 can't fall.
TryInvokeMember Fall
TryGetMember Fall
Turns out Dynamic.MyDynamicType can't fall.

Magic, eh?

For a while there I thought there was some logic in this – after all, Fall could conceivably be an element that has to be “accessed” first, before it can be “executed", like a property that returns an anonymous function or similar. But since the behavior seems to change with the surrounding code in the caller method, it looks much more like a bug… hopefully just in beta 2.

2 comments

Have you looked at the generated IL. I bet you find the answer there. I expect that the DoFall method wraps your MyDynamicType in a dynamic object.
02/16/10 @ 19:38
Comment from: Oliver Sturm Email
Hi Steven,

Yeah, I believe that - but I haven't checked it. I lost interest in the exact reason when I saw that the behavior depends on a few lines of entirely unconnected code. I don't expect this to be intended behavior anymore. Will keep this blog posted when I find out more.
02/16/10 @ 19:45

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Please complete the song title below. Hint: enter 'h', 'e', 'a', 'v', 'e', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
July 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31