为什么我不能使用"Type"在C#中创建新变量?

Cod*_*rue 1 c# types

int number = 5;

Type dynamicType = number.GetType();    // dynamic type is "int"

dynamicType x = (number as dynamicType);
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 9

您希望编译器x稍后在代码中处理变量?它不会知道任何关于它...所以它无法解决任何方法调用等.

基本上,你以后是动态类型在C#4"动态"伪类型支持:

int number = 5;
dynamic d = number;
// Calls to d are resolved at execution time, so this compiles:
d.ThisWillThrowAtExecutionTime();
Run Code Online (Sandbox Code Playgroud)


Jus*_*ren 5

Type是一个表示类型信息的对象.它不是变量的指示符,说它属于那种类型.