使用类型变量转换变量 [英] Casting a variable using a Type variable

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

问题描述

在 C# 中,我可以将 object 类型的变量转换为 T 类型的变量,其中 TType 中定义 变量?

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

推荐答案

以下是强制转换和转换的示例:

Here is an example of a cast and a convert:

using System;

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

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

评论里有人说这个答案没有回答问题.但是行 (T) Convert.ChangeType(input, typeof(T)) 提供了解决方案.Convert.ChangeType 方法尝试将任何对象转换为作为第二个参数提供的类型.

Some people in the comments say that this answer doesn't answer the question. But the line (T) Convert.ChangeType(input, typeof(T)) provides the solution. The Convert.ChangeType method tries to convert any Object to the Type provided as the second argument.

例如:

Type intType = typeof(Int32);
object value1 = 1000.1;

// Variable value2 is now an int with a value of 1000, the compiler 
// knows the exact type, it is safe to use and you will have autocomplete
int value2 = Convert.ChangeType(value1, intType);

// Variable value3 is now an int with a value of 1000, the compiler
// doesn't know the exact type so it will allow you to call any
// property or method on it, but will crash if it doesn't exist
dynamic value3 = Convert.ChangeType(value1, intType);

我已经用泛型写了答案,因为我认为当您想将 a something 转换为 a something else 而不使用时,这很可能是代码异味的迹象处理实际类型.99.9% 的情况下都不需要适当的接口.当涉及到反射时,可能有一些边缘情况可能有意义,但我建议避免这些情况.

I've written the answer with generics, because I think it is a very likely sign of code smell when you want to cast a something to a something else without handling an actual type. With proper interfaces that shouldn't be necessary 99.9% of the times. There are perhaps a few edge cases when it comes to reflection that it might make sense, but I would recommend to avoid those cases.

编辑 2:

一些额外的提示:

  • 尽量使您的代码类型安全.如果编译器不知道类型,则它无法检查您的代码是否正确,并且诸如自动完成之类的功能将不起作用.简单地说:如果你不能在编译时预测类型,那么编译器怎么能?
  • 如果您使用的类实现了一个通用的接口,您可以将值转换为该接口.否则,请考虑创建自己的接口并让类实现该接口.
  • 如果您正在使用动态导入的外部库,那么还要检查公共接口.否则,请考虑创建实现该接口的小型包装类.
  • 如果您想对对象进行调用,但不关心类型,请将值存储在 objectdynamic 变量.
  • 泛型创建适用于许多不同类型的可重用代码,而无需知道所涉及的确切类型.
  • 如果您遇到困难,请考虑采用不同的方法或代码重构.你的代码真的必须是动态的吗?它是否必须考虑任何类型?
  • Try to keep your code as type-safe as possible. If the compiler doesn't know the type, then it can't check if your code is correct and things like autocomplete won't work. Simply said: if you can't predict the type(s) at compile time, then how would the compiler be able to?
  • If the classes that you are working with implement a common interface, you can cast the value to that interface. Otherwise consider creating your own interface and have the classes implement that interface.
  • If you are working with external libraries that you are dynamically importing, then also check for a common interface. Otherwise consider creating small wrapper classes that implement the interface.
  • If you want to make calls on the object, but don't care about the type, then store the value in an object or dynamic variable.
  • Generics can be a great way to create reusable code that applies to a lot of different types, without having to know the exact types involved.
  • If you are stuck then consider a different approach or code refactor. Does your code really have to be that dynamic? Does it have to account for any type there is?

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

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