Declare a variable using a Type variable

后端 未结 6 1476
借酒劲吻你
借酒劲吻你 2020-12-06 09:28

I have this code:

Type leftType = workItem[LeftFieldName].GetType();

I then want to declare a variable of that type:

leftTy         


        
6条回答
  •  心在旅途
    2020-12-06 10:10

    This is not possible.

    Variable types are a compile-time concept; it would make no sense to declare a variable of a type which is not known until runtime.
    You wouldn't be able to do anything with the variable, since you wouldn't know what type it is.

    You're probably looking for the dynamic keyword.

提交回复
热议问题