铸造用一个类型变量的变量 [英] Casting a variable using a Type variable

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

问题描述

在C#我可以投Object类型的变量,其中T的类型变量定义类型T的变量?

In C# can I cast a variable of type object to a variable of type T where T is defined in a Type variable?

推荐答案

当然,你可以在这里既是一个简单的(假设这是一个T型投)投,如果方便,一个(假设我们可以将此转换为T)转换:

Sure you can here is both a simple (assume this is a T-type cast) cast and if convenient a (assume we can convert this to a T) convert:

public T CastExamp1<T>(object input) {   
    return (T) input;   
}

public T ConvertExamp1<T>(object input) {
    return (T) Convert.ChangeType(input, typeof(T));
}

这篇关于铸造用一个类型变量的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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