views:

203

answers:

3

C# 4.0 introduces dynamic keyword, which will look up at run-time.

Does this mean we'll need awkward reflection no more? If does, Can you show up an example of it?

+3  A: 

Dynamic dispatch is only one possible use of Reflection. There are many good reasons to interrogate a class for its structure, get information about that structure and visualize in some form or act on it in some way without ever dynamically accessing members. Reflection is here to stay. :)

If you want examples of the dynamic keyword, here is a video from PDC of the man himself talking about it (and other stuff C# 4.0 related).

JP Alioto
Great talk - especially the C#5 stuff - has me drooling...
Erik Forbes
Yeah, the REPL with C# was so great.
JP Alioto
+5  A: 

We'll still have Reflection - using 'dynamic' against regular CLR objects will invoke a Reflection-based dispatcher.

So - we'll still have Reflection, but it'll be easier to do.

Here's an example:

// Via 'dynamic'    
dynamic dx = GetSomeCLRObject();
dx.DoSomething();
dx.SomeMember = 2;

// Via Reflection
object x = GetSomeCLRObject();
Type xt = x.GetType();
MemberInfo DoSomethingMethod = xt.GetMethod("DoSomething");
DoSomethingMethod.Invoke(x, null);
PropertyInfo SomeMemberProperty = xt.GetProperty("SomeMember");
SomeMemberProperty.SetValue(x, 2);

I don't know about you, but I like the former. =)

In both these cases, I get no compile-time checking, no Intellisense, no IDE support - but the former case is much more expressive than the latter.

Erik Forbes
wow awesome, I've heard so much about the dynamic language features but totally didn't get how it would work. I use reflection often for class library stuff, so this is awesome. I've actually written a few extension methods for Object that encapsulate reflection....they'll be redundant now :-(
andy
What's better is when you see that the same syntax could target something running on the DLR - like Python or Javascript methods - or even COM objects... It's going to be really great I think. =)
Erik Forbes
+1  A: 

Dynamic will go a long way to solving problems with methods known only by name, where that name is known and fixed at compile time - but of course, such methods could also be expressed as interfaces if you control the types.

There are cases where dynamic would not help at all:

  • where the method name isn't known at compile time (i.e. it is loaded from config / user input)
  • object creation
  • maybe some generics scenarios

The biggest uses I see for dynamic are:

  • COM interop (obviously)
  • generic operator support
  • duck typing where there is no common interface
  • DLR interop (see comments)

But it definitely doesn't solve every reflection woe.

Marc Gravell
good point about the generics scenario. for me at least generics is where most of my reflection happens. in this case dynamics won't help.but will generics solve the issu of returning anonymos types from a method?
andy
*dynamic* will make it easier to work with anonymous types - but to be honest, a more answer would be "make a named type". Re generics - *some* generics may work (it is hard to tell before the beta) - but I wouldn't assume all generics to work. Mainly the problem will be generic methods (not generic types, which should work OK, since thee type is closed once it is materialized).
Marc Gravell
You should add DLR interop to your positive use cases list, as this is going to be more and more important in the framework to come.
Erik Forbes
@Erik - indeed; added, thanks.
Marc Gravell