简体   繁体   English

将Convert.ChangeType转换为System.Type

[英]Convert.ChangeType to System.Type

I have a System.Type stored in a variable. 我有一个存储在变量中的System.Type。 I wish to Change an object's type to this type. 我希望将对象的类型更改为此类型。

I have the following code, but cannot get the Type to change to the type. 我有以下代码,但无法将Type更改为type。

Ideally I'd like: var intTest3 =(MyType)Convert.ChangeType(test, MyType); 理想情况下,我想要: var intTest3 =(MyType)Convert.ChangeType(test, MyType); to return an int, when : MyType is a System.Int32 当: MyType is a System.Int32时,返回一个int

Here's my working so far - where am I going wrong? 到目前为止,这是我的工作-我哪里出错了?

        // object to cast to int
        object test = 1;

        // INT32 type
        Type MyType = typeof(System.Int32);

        // explicit type int WORKS
        var intTest = (int)Convert.ChangeType(test, typeof(Int32));


        // explicit type to int WORKS
        var intTest2 = (int)Convert.ChangeType(test, MyType);

        // explicit type to int WORKS - but as object
        object intTest3 = Convert.ChangeType(test, MyType);


        // cast to my type DOESNT WORK
        var intTest3 =(MyType)Convert.ChangeType(test, MyType);

Thank you! 谢谢!

It's not supposed to. 不应该这样 MyType isn't a "type" as far as C# is concerned, it's a variable of type "Type". 就C#而言,MyType不是“类型”,它是“ Type”类型的变量。 The "type" of "MyType" is "Type", but you can't cast 1 to "Type". “ MyType”的“ type”是“ Type”,但是不能将1强制转换为“ Type”。

In this situation, you've entered "reflection-land" but you're really trying to find a way out. 在这种情况下,您已经进入“反射区”,但实际上您正在尝试寻找出路。 I'm sorry, but there's no way to get back to strongly-typed-land from this sort of situation. 很抱歉,但是无法从这种情况返回强类型土地。

One workaround you could try to do however, is to move that last line into another generic method, and then invoke the method generically: 但是,您可以尝试执行的一种解决方法是将最后一行移到另一个通用方法中,然后以通用方式调用该方法:

public static void LeaveReflectionLand<T>(object value)
{
     T newItem = (T)value;
}

Then from outside, you'd have to do something like this: 然后从外面,您必须执行以下操作:

this.GetType().GetMethod("LeaveReflectionLand", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(MyType).Invoke(null, test);

But of course, that's a huge and scary workaround. 但是,当然,这是一个巨大而可怕的解决方法。 Check Jon Skeet's answer... I think it put things clearly. 检查乔恩·斯凯特(Jon Skeet)的答案...我认为这很清楚。

The long and the short of it is, as MyType is an instance of the class Type, it could represent any type at all. 它的长短是,因为MyType是Type类的实例,所以它可以表示任何类型。 It's a variable just like int, as far as the compiler is concerned. 就编译器而言,它是一个与int一样的变量。 It's not a proper "type" in and of itself, it's a variable that "describes" a type. 它本身不是一个适当的“类型”,而是一个“描述”类型的变量。 Because it's a variable that describes a type, there's no way, at compile time, for the compiler to know the actual type described by MyType, so this sort of thing isn't allowed. 因为它是一个描述类型的变量,所以在编译时,编译器无法知道MyType所描述的实际类型,因此这种事情是不允许的。

It's not unlike doing something like this: 这与执行以下操作没有什么不同:

int test = 1;
int MyType = 2;
int anotherTest = (MyType)test;

Obviously, you can't do this. 显然,您不能这样做。 MyType is a variable of type Int32. MyType是Int32类型的变量。 The same is true when the type of MyType is Type, only in that situation MyType is a variable of type Type. 当MyType的类型为Type时也是如此,仅在这种情况下MyType是Type类型的变量。

The key to understanding this is the difference between the "Type class" and the "compiler type". 理解这一点的关键是“类型类”和“编译器类型”之间的区别。

I hope this helps. 我希望这有帮助。 It's not the easiest thing to explain in words. 用言语解释不是最容易的事情。

This isn't really about Convert.ChangeType - it's about casting. 这实际上与Convert.ChangeType无关-与强制转换有关。 The type you're casting to has to be known at compile time , although it could be a generic type. 您要转换的类型必须在编译时就知道了,尽管它可能是通用类型。 In this case, as far as the compiler is concerned MyType could refer to any type. 在这种情况下,就编译器而言, MyType可以引用任何类型。 It doesn't "know" that it will definitely have the value typeof(int) , so it won't just emit a cast to int . 它并不“知道”它肯定具有值typeof(int) ,因此它不会仅仅将类型转换为int

In this case, what would you expect the compile-time type of intTest3 to be? 在这种情况下,您期望intTest3的编译时类型是什么?

What's the bigger picture here? 这里的大局是什么? What are you really trying to do? 到底想干什么? How would you want to use the value of intTest3 ? 您将如何使用intTest3的值?

MyType is a Type , which is a variable containing the definition information of a struct / class. MyType是一个Type ,它是一个包含结构/类的定义信息的变量。 Casting by putting the type in brackets eg (int)value; 通过将type放在方括号中进行转换,例如(int)value; requires the item in the brackets to be an actual class or struct, not a variable. 要求方括号中的项目是实际的类或结构,而不是变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM