使用类型变量进行转换 [英] cast with a Type variable

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

问题描述

下面的代码不起作用,我想知道如何将实例动态转换为在运行时确定的类型?

The below code won't work, I wanted to know how I can dynamically cast an instance to a type determined at runtime?

Convert.ChangeType()返回仍然需要转换的对象。调用Invoke(),GetConstructor()或Activator.CreateInstance()的所有尝试也是如此,请参见下文。在某些时候,我需要显式地植入代码,希望避免它或将其尽可能地推出。

Convert.ChangeType() returns an Object that still needs to be cast. So does all attempts to Invoke() a GetConstructor() or Activator.CreateInstance(), see below. At some point I need to explicitly cast in code, I'm hoping to avoid it or push it out as far as possible.

Type type = Type.GetType ("RandomChildClass");
Object obj = Activator.CreateInstance (type, new Object[]{ "argument" });
var instance = (type)obj;

我知道我可以创建一种接受< T>,但仍然会有一个相同的问题,就是不知道如何使用动态随机类型
使用类型变量投射变量

I know I can create a method to accept < T >, but I'd still have the same problem of not knowing how to call it with a dynamic random type Casting a variable using a Type variable

推荐答案

使用 Type value 来确定表达式的类型。 (泛型类型参数与值不同,因为它们被编入类型系统。)

It is not possible to use a Type value to determine the type of an expression. (Generics type parameters are different than values as they are codified into the type-system.)

变量的值来自运行时代码执行,而表达式类型是 compile-time 构造。不用说,编译是在代码运行之前发生的,因此不可能使用变量进行强制转换。

The value of the variable is from the run-time code execution, while the expression type is a compile-time construct. Needless to say, the compilation occurs before the code ever runs so using a variable for a cast is impossible.

反射(不便)或 dynamic (基本上易于使用),允许任意调用方法或针对通用对象类型的表达式访问属性/字段-有时称为后期绑定。但是,在其上调用操作的表达式的类型仍然是对象。

Reflection (albiet unwieldy) or dynamic (which is basically easier-to-use-reflection) allow invoking arbitrary methods or accessing properties/fields against a generic object-typed expression - this is occasionally refered to as "late binding". However, the type of the expression upon which operations is invoked is still object.

接口可用于统一不同的类实现,以进行适当的静态类型化。然后,新创建的对象可以强制转换为适用的接口。就像其他表达式一样,类型是一个 compile-time 构造(因此必须直接指定该接口),但是代码现在不受特定类的限制。

Interfaces can be used to unify disparate class implementations for proper static typing. The newly created object can then cast to the applicable interface(s) are required. Just like with other expressions, the type is a compile-time construct (and as such the interface must be directly specified), but the code is now free from a particular class.

如果创建系统以使这些动态类直接在静态类型(C#)代码中使用,并且可以保证接口或将接口限制为一小组,则可以使用接口可能是最干净的方法:例如 var myAction =(IMyAction)obj 。否则,请退回动态访问-直接或直接在立面后面。

If creating a system such that these "dynamic classes" are to be used directly in statically typed (C#) code, and the interfaces can be guaranteed or are constrained to a small set, then using interfaces is likely the cleanest approach: e.g. var myAction = (IMyAction)obj. Otherwise, fall back to dynamic access - direct or behind a facade.

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

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