尝试实例化动态类型数组(后期绑定)时出现 RuntimeBinderException
RuntimeBinderException when trying to instantiate an array of a dynamic type (late-binding)
我正在尝试使用 Activator.CreateInstance()
来实例化一个具有动态类型字段的数组(我得到在运行时必须用于数组字段的类型 Type arrayType = arrayFieldType.MakeArrayType()
)。
singleSet.ZaznamyObjektu = Activator.CreateInstance(arrayType, new object[] { rowCount });
(rowCount
是一个整数。)我根据 How do I create a C# array using Reflection and only type info? 选择了这种方法,但它一直给我 RuntimeBinderException:
Cannot implicitly convert type 'object' to 'PodperneZarizeniTypeZaznamObjektu[]'. An explicit conversion exists (are you missing a cast?)
但是当我不能使用类型的确切名称时,我不知道如何进行转换或转换。我也尝试过使用 Array.CreateInstance()
但它给了我类似的异常:
Cannot implicitly convert type 'System.Array' to 'PodperneZarizeniTypeZaznamObjektu[]'. An explicit conversion exists (are you missing a cast?)
but it keeps giving me RuntimeBinderException:
Cannot implicitly convert type 'object' to 'PodperneZarizeniTypeZaznamObjektu[]'. An explicit conversion exists (are you missing a cast?)
这看起来根本不像运行时异常。这看起来像是一个编译时错误。
在评论中你说 singleSet.ZaznamyObjektu
的类型是
PodperneZarizeniTypeZaznamObjektu[]
。 Activator.CreateInstance
returns 和 object
,请记住 CreateInstance
可能对 任何 类型有效。您不能将 object
分配给类型为 属性 的数组。
你的问题似乎是你只是缺少演员表:
singleSet.ZaznamyObjektu = (PodperneZarizeniTypeZaznamObjektu[])Activator.CreateInstance(arrayType, new object[] { rowCount });
现在请注意,在以下情况下这将失败:
arrayField
是 引用类型 并且 arrayField
和 [=21] 之间没有有效的身份保留引用转换=].
(Mammal[])tigers; //valid
(Insect[])tigers; //evidently not.
arrayField
是一个 值类型 并且它的类型不是 PodperneZarizeniTypeZaznamObjektu
的类型。即使有隐式/显式转换运算符,也会失败;值类型不允许数组类型变化。
(long[])(ints); //not valid even though an implicit cast
//from int to long exists
我正在尝试使用 Activator.CreateInstance()
来实例化一个具有动态类型字段的数组(我得到在运行时必须用于数组字段的类型 Type arrayType = arrayFieldType.MakeArrayType()
)。
singleSet.ZaznamyObjektu = Activator.CreateInstance(arrayType, new object[] { rowCount });
(rowCount
是一个整数。)我根据 How do I create a C# array using Reflection and only type info? 选择了这种方法,但它一直给我 RuntimeBinderException:
Cannot implicitly convert type 'object' to 'PodperneZarizeniTypeZaznamObjektu[]'. An explicit conversion exists (are you missing a cast?)
但是当我不能使用类型的确切名称时,我不知道如何进行转换或转换。我也尝试过使用 Array.CreateInstance()
但它给了我类似的异常:
Cannot implicitly convert type 'System.Array' to 'PodperneZarizeniTypeZaznamObjektu[]'. An explicit conversion exists (are you missing a cast?)
but it keeps giving me RuntimeBinderException:
Cannot implicitly convert type 'object' to 'PodperneZarizeniTypeZaznamObjektu[]'. An explicit conversion exists (are you missing a cast?)
这看起来根本不像运行时异常。这看起来像是一个编译时错误。
在评论中你说 singleSet.ZaznamyObjektu
的类型是
PodperneZarizeniTypeZaznamObjektu[]
。 Activator.CreateInstance
returns 和 object
,请记住 CreateInstance
可能对 任何 类型有效。您不能将 object
分配给类型为 属性 的数组。
你的问题似乎是你只是缺少演员表:
singleSet.ZaznamyObjektu = (PodperneZarizeniTypeZaznamObjektu[])Activator.CreateInstance(arrayType, new object[] { rowCount });
现在请注意,在以下情况下这将失败:
arrayField
是 引用类型 并且arrayField
和 [=21] 之间没有有效的身份保留引用转换=].(Mammal[])tigers; //valid (Insect[])tigers; //evidently not.
arrayField
是一个 值类型 并且它的类型不是PodperneZarizeniTypeZaznamObjektu
的类型。即使有隐式/显式转换运算符,也会失败;值类型不允许数组类型变化。(long[])(ints); //not valid even though an implicit cast //from int to long exists