简体   繁体   English

InvokeMember,其中成员是具有反射的数组属性

[英]InvokeMember where the member is an array property with reflection

So I asked something similar last week, but I think it was pretty confusing, so Ill try to simplify it. 所以上周我问了类似的问题,但我认为这很令人困惑,因此我将尝试简化它。

Say for instance I have a class that contains only properties like this: 比如说我有一个只包含如下属性的类:

public class MyPropertyClass
{
    public int IntegerProperty { get; set; }
}

Now suppose I have created another class with an array of MyPropertyClass like this: 现在,假设我用MyPropertyClass数组创建了另一个类,如下所示:

public class AnotherPropertyClass
{
    public MyPropertyClass[] ArrayProperty { get; set; }
}

Now here is the complicated part. 现在这里是复杂的部分。 I need to dynamically create a MyPropertyClass[] somehow. 我需要以某种方式动态创建MyPropertyClass[] I've been trying it with a List<object> thus far. 到目前为止,我一直在尝试使用List<object> Then, make a call to InvokeMember with this array. 然后,使用此数组调用InvokeMember Something like this: 像这样:

//The list that I am adding elements to elsewhere in the code
List<object> objList = new List<object>();

//Adding a couple elements
objList.Add(new MyPropertyClass());
objList.Add(new MyPropertyClass());

//Create the parameter object array, has to be length one and contain an
//object array casted to MyPropertyClass or it will throw an exception.
object[] ob = new object[1] { objList.ToArray() };

//Instantiate the actual object I want to assign the array to.
object obj = new AnotherPropertyClass();

//The call to InvokeMember
obj.GetType().InvokeMember(
    "ArrayProperty",
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
    Type.DefaultBinder,
    obj,
    ob);

This code will throw an exception. 此代码将引发异常。 The problem is, objList.ToArray() creates an object[] and when InvokeMember tries to assign it to the MyPropertyClass[] , it complains about the type mismatch, even though all of the elements are MyPropertyClass types. 问题是,objList.ToArray()创建一个object[]并且当InvokeMember尝试将其分配给MyPropertyClass[] ,即使所有元素都是MyPropertyClass类型,它也会抱怨类型不匹配。 Basically what I need is a way to say, "hey, all of the elements in objList.ToArray() are going to be MyPropertyClass " ie object{MyPropertyClass[]} while letting the actual type be arbitrary, it might not be MyPropertyClass , it could be some other type, I don't know at compile time. 基本上,我需要一种说法,“嘿,objList.ToArray()中的所有元素都将是MyPropertyClass ”,即object {MyPropertyClass []},而让实际类型是任意的,则可能不是MyPropertyClass ,它可能是其他类型,我在编译时不知道。

What I have here is only my attempt so far, if you know a different approach i'm all ears. 到目前为止,我所尝试的只是我的尝试,如果您知道另一种方法,我将不知所措。 If you want more information, see my old question here: 如果您需要更多信息,请在这里查看我的旧问题:

runtime casting of an object[] to a custom type array 在运行时将object []强制转换为自定义类型数组

I just think there is little too much extra stuff in there that's not related to the actual problem i'm having. 我只是认为其中没有太多多余的东西与我遇到的实际问题无关。

您可以创建一个未指定类型的数组,如下所示:

Array array = Array.CreateInstance(someType, someSize);

You do it like this: 您可以这样做:

List<MyPropertyClass> objList = new List<MyPropertyClass>();
objList.Add(new MyPropertyClass());
objList.Add(new MyPropertyClass());

AnotherPropertyClass obj = new AnotherPropertyClass();

obj.GetType().GetProperty("ArrayProperty").SetValue(obj, objList.ToArray());

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

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