C#:动态运行时强制转换 [英] C#: Dynamic runtime cast

查看:41
本文介绍了C#:动态运行时强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个具有以下签名的方法

I would like to implement a method with the following signature

dynamic Cast(object obj, Type castTo);

有人知道怎么做吗?obj 确实实现了 castTo 但需要正确转换才能使我的应用程序的一些运行时绑定工作正常运行.

Anyone know how to do that? obj definitely implements castTo but needs to be cast properly in order to have some of my app's runtime binding stuff work out.

如果某些答案没有意义,那是因为我最初不小心输入了 dynamic Cast(dynamic obj, Type castTo); - 我的意思是输入应该是 object 或其他一些有保证的基类

If some of the answers don't make sense it's because I initially accidentally typed dynamic Cast(dynamic obj, Type castTo); - I mean the input should be object or some other guaranteed base class

推荐答案

我认为您在这里混淆了转换和转换的问题.

I think you're confusing the issues of casting and converting here.

  • 强制转换:改变指向对象的引用类型的行为.向上或向下移动对象层次结构或到已实现的接口
  • 转换:从不同类型的原始源对象创建一个新对象,并通过对该类型的引用访问它.

通常很难知道 C# 中的 2 之间的区别,因为它们都使用相同的 C# 运算符:强制转换.

It's often hard to know the difference between the 2 in C# because both of them use the same C# operator: the cast.

在这种情况下,您几乎肯定不是在寻找强制转换操作.将一个 dynamic 转换为另一个 dynamic 本质上是一种身份转换.它没有提供任何价值,因为您只是将 dynamic 引用返回到同一个底层对象.结果查找不会有什么不同.

In this situation you are almost certainly not looking for a cast operation. Casting a dynamic to another dynamic is essentially an identity conversion. It provides no value because you're just getting a dynamic reference back to the same underlying object. The resulting lookup would be no different.

在这种情况下,您似乎想要的是转换.这就是将底层对象变形为不同的类型,并以 dynamic 方式访问生成的对象.最好的 API 是 Convert.ChangeType.

Instead what you appear to want in this scenario is a conversion. That is morphing the underlying object to a different type and accessing the resulting object in a dynamic fashion. The best API for this is Convert.ChangeType.

public static dynamic Convert(dynamic source, Type dest) {
  return Convert.ChangeType(source, dest);
}

编辑

更新的问题有以下几行:

The updated question has the following line:

obj 肯定实现了 castTo

obj definitely implements castTo

如果是这种情况,则 Cast 方法不需要存在.源 object 可以简单地分配给 dynamic 引用.

If this is the case then the Cast method doesn't need to exist. The source object can simply be assigned to a dynamic reference.

dynamic d = source;

听起来您想要完成的是通过 dynamic 引用查看 source 层次结构中的特定接口或类型.那根本不可能.生成的 dynamic 引用将直接看到实现对象.它不会查看源层次结构中的任何特定类型.因此,转换到层次结构中的不同类型然后返回到 dynamic 的想法与首先分配给 dynamic 完全相同.它仍然指向同一个底层对象.

It sounds like what you're trying to accomplish is to see a particular interface or type in the hierarchy of source through a dynamic reference. That is simply not possible. The resulting dynamic reference will see the implementation object directly. It doesn't look through any particular type in the hierarchy of source. So the idea of casting to a different type in the hierarchy and then back to dynamic is exactly identical to just assigning to dynamic in the first place. It will still point to the same underlying object.

这篇关于C#:动态运行时强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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