简体   繁体   English

尝试实例化动态类型的数组时发生RuntimeBinderException(后期绑定)

[英]RuntimeBinderException when trying to instantiate an array of a dynamic type (late-binding)

I am trying to use Activator.CreateInstance() to instantiate an array that has fields of a dynamic type (I get the type that I must use for array fields during the runtime as Type arrayType = arrayFieldType.MakeArrayType() ). 我正在尝试使用Activator.CreateInstance()实例化具有动态类型的字段的数组(我在运行时将必须用于数组字段的Type arrayType = arrayFieldType.MakeArrayType()Type arrayType = arrayFieldType.MakeArrayType() )。

singleSet.ZaznamyObjektu = Activator.CreateInstance(arrayType, new object[] { rowCount });

( rowCount is an integer.) I have chosen this approach according to How do I create a C# array using Reflection and only type info? rowCount是一个整数。)我根据如何使用反射仅创建信息来创建C#数组的方式选择了这种方法 but it keeps giving me RuntimeBinderException: 但是它一直给我RuntimeBinderException:

Cannot implicitly convert type 'object' to 'PodperneZarizeniTypeZaznamObjektu[]'. 无法将类型'object'隐式转换为'PodperneZarizeniTypeZaznamObjektu []'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

But I do not know how to make cast or conversion when I cannot use the exact name of the type. 但是当我不能使用类型的确切名称时,我不知道如何进行转换或转换。 I have also tried to use Array.CreateInstance() but it was giving me similar exception: 我也尝试过使用Array.CreateInstance()但是它给了我类似的异常:

Cannot implicitly convert type 'System.Array' to 'PodperneZarizeniTypeZaznamObjektu[]'. 无法将类型'System.Array'隐式转换为'PodperneZarizeniTypeZaznamObjektu []'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

but it keeps giving me RuntimeBinderException: 但是它一直给我RuntimeBinderException:

Cannot implicitly convert type 'object' to 'PodperneZarizeniTypeZaznamObjektu[]'. 无法将类型'object'隐式转换为'PodperneZarizeniTypeZaznamObjektu []'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

That doesn't look like a runtime exception at all. 看起来根本不是运行时异常。 That looks like a compile time error. 看起来像是编译时错误。

In comments you are saying that the type of singleSet.ZaznamyObjektu is PodperneZarizeniTypeZaznamObjektu[] . 在评论你是说的类型singleSet.ZaznamyObjektuPodperneZarizeniTypeZaznamObjektu[] Activator.CreateInstance returns an object , bear in mind that CreateInstance is potentialy valid for any type. Activator.CreateInstance返回一个object ,请记住, CreateInstance对于任何类型都可能有效。 You can't assign an object to an array typed property. 您不能将object分配给数组类型的属性。

Your problem seems to be that you are simply missing a cast: 您的问题似乎是您只是缺少演员表:

singleSet.ZaznamyObjektu = (PodperneZarizeniTypeZaznamObjektu[])Activator.CreateInstance(arrayType, new object[] { rowCount });

Now, do note, that this will fail miserably in the following scenarios: 现在,请注意,在以下情况下这将严重失败:

  1. arrayField is a reference type and there is no valid identity preserving reference conversion between an arrayField and a PodperneZarizeniTypeZaznamObjektu . arrayField引用类型 ,在arrayFieldPodperneZarizeniTypeZaznamObjektu之间没有有效的身份保留引用转换。

     (Mammal[])tigers; //valid (Insect[])tigers; //evidently not. 
  2. arrayField is a value type and its type is not PodperneZarizeniTypeZaznamObjektu 's type. arrayField是一个值类型 ,其类型不是PodperneZarizeniTypeZaznamObjektu的类型。 Even if there is an implicit / explicit cast operator, it will fail; 即使存在隐式/显式强制转换运算符,它也会失败; array type variance is not permitted with value types. 值类型不允许使用数组类型方差。

     (long[])(ints); //not valid even though an implicit cast //from int to long exists 

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

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