InvokeMember,其中成员是具有反射的数组属性 [英] InvokeMember where the member is an array property with reflection

查看:42
本文介绍了InvokeMember,其中成员是具有反射的数组属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我上周问了类似的问题,但我认为这很令人困惑,所以我会尽量简化它.

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; }
}

现在假设我已经创建了另一个具有 MyPropertyClass 数组的类,如下所示:

Now suppose I have created another class with an array of MyPropertyClass like this:

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

现在是复杂的部分.我需要以某种方式动态创建一个 MyPropertyClass[] .到目前为止,我一直在用 List 尝试它.然后,使用此数组调用 InvokeMember.像这样:

Now here is the complicated part. I need to dynamically create a MyPropertyClass[] somehow. I've been trying it with a List<object> thus far. Then, make a call to InvokeMember with this array. 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);

此代码将引发异常.问题是,objList.ToArray() 创建了一个 object[] 并且当 InvokeMember 尝试将它分配给 MyPropertyClass[] 时,它会抱怨类型不匹配,即使所有元素都是 MyPropertyClass 类型.基本上我需要的是一种说法,嘿,objList.ToArray() 中的所有元素都将是 MyPropertyClass",即 object{MyPropertyClass[]} 同时让实际类型是任意的,它可能不是 MyPropertyClass,它可能是其他类型,我在编译时不知道.

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. Basically what I need is a way to say, "hey, all of the elements in objList.ToArray() are going to be MyPropertyClass" i.e 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.

到目前为止,我所拥有的只是我的尝试,如果您知道不同的方法,我会全神贯注.如果您想了解更多信息,请在此处查看我的旧问题:

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:

运行时将对象 [] 转换为自定义类型数组

我只是认为那里没有太多与我遇到的实际问题无关的额外内容.

I just think there is little too much extra stuff in there that's not related to the actual problem i'm having.

推荐答案

你可以像这样创建一个未指定类型的数组:

You can create an array of an unspecified type like this:

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

这篇关于InvokeMember,其中成员是具有反射的数组属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆