开发者

Is there a C# version of CTypeDynamic?

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-开发者_StackOverflow社区defined, static conversions.

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


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


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!


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.


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.


The C# equivalent function to VB's CTypeDynamic() is Convert.ChangeType().

The change in name helps distinguish the C# function as being about setting a type conversion at runtime according to a type variable, rather than being about the dynamic type mechanism.

Convert.ChangeType

Returns an object of a specified type whose value is equivalent to a specified object.

Conversion.CTypeDynamic

Converts an object to the specified type.

...

The CTypeDynamic method converts the object passed as the Expression parameter to the type specified by the TargetType parameter. If the object is a dynamic object, the CTypeDynamic method applies available dynamic conversions.

...

CTypeDynamic method first attempts to perform the conversion by using a user-defined, static conversion

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜