在运行时动态投射 [英] Dynamic cast at runtime

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

问题描述

是否有一种可以在运行时动态转换的方法,例如以下伪代码:

Is there a way to dynamically cast at runtime like the following pseudo code:

foreach (DataRow row in table.Rows)
{
    foreach (DataColumn col in table.Columns)
    {
        if (row[col] != DBNull.Value)
        {
            Type type = col.DataType;

            type cellContent = (type)row[col]; //Pseudo-Code
        }
    }
}

我一直在网上搜索,却没有找到任何东西.有一个 object obj = Activator.CreateInstance(type); ,但是那时我仍然迷恋于一个对象,不能与它一起使用特定的类型方法.另外,我需要现有对象的强制转换,而不是新实例的强制转换.我需要从CellContent中删除所有EventHandler,因为在某些情况下它们会导致内存泄漏,例如:对象类型为IList [SerialNumberGridViewModel],而SerialNumberGridViewModel实现了PropertyChanged-Handler,这导致了内存泄漏.任何想法?有办法解决这个问题吗?

I´ve been searching the web and not found anything. There´s object obj = Activator.CreateInstance(type);, but then I´m still stuck with an object and can´t use specific type methods with it. Also I need a cast of an existing object and not a new instance. I need to remove all EventHandlers from CellContent as in certain cases they cause a memory leak, example: Object type is IList[SerialNumberGridViewModel] and SerialNumberGridViewModel implements PropertyChanged-Handler which is causing a memory leak. Any idea? Is there a way to solve this issue?

我已经在上述特定情况下解决了该问题,但是通用方法会更好,因为我正在使用的程序很大,并且有很多内存泄漏需要清除.

I´ve already solved it in the above specific case, but a general method would be a lot better, as the program I´m working with is big and has a lot of memory leaks to be removed.

推荐答案

否.您不能将其转换为在编译时未知的任何类型.但是,c#确实有一个特殊的关键字来声明未知类型的变量-它是 动态 .
您可以将其视为 late绑定的一种形式-变量的实际类型仅在运行时确定.
声明动态变量时,c#编译器实际上会创建类型为object的变量,但不执行任何类型检查.

No. You can't cast to any type that is unknown at compile time. However, c# does have a special keyword for declaring variables of a type that is unknown - it's dynamic.
You can think of it like a form of late binding - The actual type of the variable is determind at run time only.
When you declare a dynamic variable, c# compiler actually creates a variable of type object, but does not perform any type checking.

动态类型使发生的操作可以绕过编译时类型检查.而是在运行时解决这些操作.
...
在大多数情况下,动态类型的行为类似于类型对象.但是,包含动态类型的表达式的操作不会被编译器解析或类型检查.编译器将有关该操作的信息打包在一起,该信息随后将用于在运行时评估该操作.作为该过程的一部分,动态类型的变量将被编译为类型对象的变量.因此,动态类型仅在编译时存在,而在运行时不存在.

The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time.
...
Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.

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

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