动态铸造? [英] dynamic casting?

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

问题描述

我需要一种方法来对象转换一个类型,是不知道在编译时间

I need a way to cast an object to a type that is not know at compiler time.

是这样的:

object obj;

public (type of obj) Obj
{
    get
    {
        return obj
    }
    set
    {
        obj = (type of obj)value;
    }
}

这是唯一知道的是,obj为值类型。

The only thing that is know is that obj is a value type.

我怀疑,这样的事情是可能的。只是检查,看看是否有人有那么这是一种聪明的办法。

I doubt that something like this would be possible. Just checking to see if someone has a clever way of doing it.

推荐答案

这是不是在C#3.0中可能的,但如果你可以定义你所有的物体实现共同的接口,你可以转换到这一点。

This is not possible in C# 3.0, but if you could define a common interface that all of your objects implement, you could cast to that.

C#4.0,我们将获得动态关键字,可以让我们做鸭打字。

In C# 4.0 we will get the dynamic keyword that allows us to do Duck Typing.

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

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