有没有办法做到动态隐含类型转换在C#中? [英] Is there a way to do dynamic implicit type casting in C#?

查看:252
本文介绍了有没有办法做到动态隐含类型转换在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这一类隐式转换操作符:

 公共类MyDateTime
{
    公共静态隐运营商MyDateTime(System.Int64 EN codeD)
    {
        返回新MyDateTime(EN codeD);
    }    公共MyDateTime(System.Int64 EN codeD)
    {
        _en codeD = EN codeD;
    }
    System.Int64 _en codeD;
}

我现在可以做到以下几点:

 长= 5;
MyDateTime B = A;

但不是以下内容:

 长F = 5;
对象G = F;
MyDateTime H =克;

这给出了一个编译时间:


  

无法隐式转换类型'对象'到'MyDateTime。


对我来说很有意义。

现在我修改previous举例如下:

 长F = 5;
对象G = F;
MyDateTime H =(MyDateTime)克;

这编译罚款。现在,我得到一个运行 InvalidCastException的


  

无法投类型'System.Int64'键入MyDateTime的对象。


这告诉我,C#隐式转换运营商仅在编译时应用,当.NET运行时尝试将不会应用于动态对象转换为另一种类型。

我的问题:


  1. 我是正确的?

  2. 有一些其他的方式来做到这一点?

顺便说一句,充分应用是我使用 Delegate.DynamicInvoke()来调用一个函数,它接受一个 MyDateTime 参数,我传递给 DynamicInvoke 是一个漫长的。


解决方案

  

我是正确的?


是的,是你。被挑剔的,你应该说:用户定义的隐式转换而不是隐式转换 - 铸造(几乎)总是明确的。但是,你扣了重载解析选择用户自定义转换调用的在编译时间的而不是在运行时,它的是正确的。


  

有一些其他的方式来做到这一点?


是的。在C#4,如果你键入对象,动态,然后我们再次启动编译器的在运行时的并重新执行对操作数所有的分析的就好像他们的编译时类型为当前运行时类型的。正如你可能想象,这是不便宜,但我们对缓存和重新使用的结果,你应该这样做在紧密循环。

很聪明

Given this class with an implicit cast operator:

public class MyDateTime
{
    public static implicit operator MyDateTime(System.Int64 encoded)
    {
        return new MyDateTime(encoded);
    }

    public MyDateTime(System.Int64 encoded)
    {
        _encoded = encoded;
    }
    System.Int64 _encoded;
}

I can now do the following:

long a = 5;
MyDateTime b = a;

But NOT the following:

long f = 5;
object g = f;
MyDateTime h = g;

This gives a compile time:

Cannot implicitly convert type 'object' to 'MyDateTime'.

Makes sense to me.

Now I modify the previous example as follows:

long f = 5;
object g = f;
MyDateTime h = (MyDateTime)g;

This compiles fine. Now I get a runtime InvalidCastException:

Unable to cast object of type 'System.Int64' to type MyDateTime'.

This tells me that C# implicit cast operators are applied at compile time only, and are not applied when the .NET runtime is attempting to dynamically cast an object to another type.

My questions:

  1. Am I correct?
  2. Is there some other way to do this?

By the way, the full application is that I'm using Delegate.DynamicInvoke() to call a function that takes a MyDateTime parameter, and the type of the argument I'm passing to DynamicInvoke is a long.

解决方案

Am I correct?

Yes, yes you are. To be nit-picky, you should be saying "user-defined implicit conversion" rather than "implicit cast" -- a cast is (almost) always explicit. But your deduction that overload resolution chooses which user-defined conversion to call at compile time and not at run time is correct.

Is there some other way to do this?

Yes. In C# 4 if you type your "object" as "dynamic" then we start up the compiler again at runtime and re-perform all the analysis on the operands as though their compile-time types were the current run-time types. As you might imagine, this is not cheap, though we are very smart about caching and re-using the results should you do this in a tight loop.

这篇关于有没有办法做到动态隐含类型转换在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆