views:

64

answers:

4

From the documentation:

The CTypeDynamic method applies dynamic conversions in accordance with the conversion semantics defined by the object itself. If a dynamic object inherits from DynamicObject, the CTypeDynamic method first attempts to perform the conversion by using a user-defined, static conversion. If the user-defined, static conversion fails, the CTypeDynamic method attempts to perform the conversion by using dynamic conversions. If a dynamic object implements IDynamicMetaObjectProvider, the CTypeDynamic method gives precedence to dynamic conversions over user-defined, static conversions.

Is there something in C# that does this? Or do I just have to import the VB library that has it.

+2  A: 

If you use C# 4.0 then yes, called dynamic. Here is the link

Eugene Cheverda
Yes, that would work for the CTypeDyamic examples I found. Still wonder if it 'is' the same.
Henk Holterman
It implements IDynamicMetaObjectProvider so it suits your needs as well.
Eugene Cheverda
It's not exactly the same since C# uses the C# runtime binder, and VB uses the VB runtime binder.
codekaizen
The result is that all of them uses DLR conventions on working with dynamic objects.
Eugene Cheverda
+1  A: 

You don't need to import the VB library for this.

If you override TryConvert in your DynamicObject-derived class, then C# allows you to implicitly call this via an implicit or explicit cast.

//explicit conversion
String myObject = (String)myDynObject;

//implicit conversion
String myObject = myDynObject;

VB.NET only supports explicit conversions. C# supports both implicit and explicit.

Here's the link to MSDN about this.

Hope this helps!

David Hoerster
I guess that makes sense considering that VB developers would just use Option Strict Off for late-bound code.
Jonathan Allen
I think so. I'm not as much of a VB.NET dev as I am a C# dev...but it makes sense.
David Hoerster
A: 

I'd guess first casting to dynamic and then to the target type should do the trick.
Dynamic is available in C# 4.0 / VS 2010

In C# 3.0 / VS2008 you probably can work around using expressions. If I recall correctly the "MiscUtil" library contains functions to use the conversion operators in their generic operator class.

Chaos
A: 

Short answer

You don't need to import anything. Just use implicit and explicit C# conversion and the "dynamic" keyword. D Hoerster's answer is correct.

Long answer

C# has a separate type to treat dynamic objects - the type is "dynamic". But in VB there is no dedicated type for this, since the dynamic behavior is implemented through late binding. So, in VB the dynamic objects that need real dynamic binding are sometimes hard to distinguish from just "objects". CType handles conversions of "objects" to types. In most cases, it is fine. However, when you deal with implementations of IDynamicMetaObjectProvider interface, it may cause troubles. CType can't understand whether it deals with "object" or "dynamic object" - there is no syntax to distinguish one from another and CType doesn't want to call dynamic binders for all objects defined through the late binding. It assumes that all the objects it deals with are "objects", not "dynamic objects". So you need to somehow let the compiler know that you are dealing with a dynamic object. Instead of creating the new "dynamic" type, the VB team decided to just add one more conversion function - CTypeDynamic. It explicitly tells the compiler that you are converting dynamic type to some other type (again, in C# you don't need this, since you already defined the object by using the dynamic keyword and the compiler already knows what it deals with).

The good illustration of the problem is DynamicObject.TryConvert method. Try to replace CTypeDynamic with CType in the VB example and you'll see an exception. Again, this is because CType knows nothing about dynamic runtime binder. But in C# the runtime binder is called for all objects defined by the dynamic keyword, so it doesn't need this special function.

Alexandra Rusina