Do C # 4.0 dynamic objects have a duck printing tool?

In C # 4.0, we are going to get dynamic types or objects whose "static type is dynamic", according to Anders. This will allow any call to the method call at runtime, rather than compilation time. But will it be possible to associate a dynamic object with some kind of contract (and thereby also get the full intellisense for it back), and not allow any call to it, even if you know that this is unlikely to be valid.

those. instead of just

dynamic foo = GetSomeDynamicObject();

have the ability to create or transform it in order to limit it to a known contract, for example

IFoo foo2 = foo.To<IFoo>;

or even just

IFoo foo2 = foo as IFoo;

It is impossible to find anything like this in existing materials for C # 4.0, but it seems like a logical extension of the dynamic paradigm. Anyone with more info?

+5
source share
3 answers

I do not know anything like a duck seal, I'm afraid. I wrote about this idea , but I do not expect any support. It would probably not be too difficult to use Reflection.Emit to create a class that will generate an implementation of any given interface, accepting a dynamic object in the constructor and simply proxying every call to it. Not perfect, but it could be a stop.

+2
source

. , / CLR, , / , , , . CLR " IFoo" , IFoo. , , .

+1

blogpost, Convert() MetaObject . .

+1

All Articles