ReadOnlyCollection中的对象[]

发布于 2024-07-14 14:30:59 字数 624 浏览 4 评论 0原文

我有一个通过反射调用最终得到的对象:

object readOnlyCollectionObject = propertyInfo.GetValue(someEntity, null);

我知道这个对象是一个通用的 ReadOnlycollection。 它可以是 ReadOnlyCollectionReadOnlyCollection 等。出于论证目的,我们只说它是 ReadOnlyCollection >。

尽管 Dog 派生自对象,但我知道 ReadOnlyCollection 并不是派生自 ReadOnlyCollection。 所以即使我使用反射来调用 CopyTo 方法我仍然需要知道 ReadOnlyCollection 的具体类型,这是我想要避免的。

我想知道如何以对象引用数组的形式从 ReadOnlyCollection 中获取所有元素,而不必知道 ReadOnlyCollection 的特定类型 (T)。

I have an object that I ended up with via a reflection call:

object readOnlyCollectionObject = propertyInfo.GetValue(someEntity, null);

I know this object is a generic ReadOnlycollection. It could be a ReadOnlyCollection<Cat>, ReadOnlyCollection<Dog>, etc. For argument sake, lets just say it is a ReadOnlyCollection<T>.

Even though a Dog derives from an object, I know that a ReadOnlyCollection<Dog> does not derive from a ReadOnlyCollection<object>. So even if I use reflection to call the CopyTo method I still need to know the specific type of ReadOnlyCollection, which is what I want to avoid.

I want to know how to get all the elements out of the ReadOnlyCollection as an array of object references without having to know the specific type (T) of the ReadOnlyCollection<T>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇到 2024-07-21 14:30:59

许多其他答案都提到了 Cast() 和 ToArray,这些都存在类型问题。 正如您所说,您不会知道您的财产将实现哪个专门的 IEnumerable。 但是,您可以确定它们都将实现非通用 ICollection 接口。

ICollection readOnlyCollectionObject = (ICollection)propertyInfo.GetValue(someEntity, null);
object[] objs = new ArrayList(readOnlyCollectionObject).ToArray();

ICollection readOnlyCollectionObject = (ICollection)propertyInfo.GetValue(someEntity, null);
object[] objs = new object[readOnlyCollectionObject.Count];
for (int i = 0; i < readOnlyCollectionObject.Count; i++)
    objs[i] = readOnlyCollectionObject[i];

编辑:忘记更新从 IEnumerable 到 ICollection 的转换

Many other answers mention Cast() and ToArray, those all have a problem with the type. As you say, you won't know which specialized IEnumerable your property will implement. However, you can be sure that they will all implement the non-generic ICollection interface.

ICollection readOnlyCollectionObject = (ICollection)propertyInfo.GetValue(someEntity, null);
object[] objs = new ArrayList(readOnlyCollectionObject).ToArray();

or

ICollection readOnlyCollectionObject = (ICollection)propertyInfo.GetValue(someEntity, null);
object[] objs = new object[readOnlyCollectionObject.Count];
for (int i = 0; i < readOnlyCollectionObject.Count; i++)
    objs[i] = readOnlyCollectionObject[i];

Edit: Forgot to update the cast from IEnumerable to ICollection

|煩躁 2024-07-21 14:30:59

那么,您可以在 上使用 Cast 扩展方法readonly 集合,然后将其转换为对象数组,但是,基于 C# 数组是协变这一事实,您可以简单地执行以下操作:

object[] objs = myReadOnlyCollection.ToArray();

(编辑)

正如 Pop Catalin 提到的,只有当 T 是引用类型时才有效。 否则请使用 Cast 方法。

(edit2)

您对问题的更新改变了很多事情......我认为您想要做的事情是不可能的。 您希望在编译时转换为仅在运行时可用的显式类型。 在这种情况下,访问集合的唯一方法是使用反射。

Well, you can use the Cast extension method on the readonly collection and then convert it to an array of objects but, based on the fact that in c# arrays are covariant, you can simply do:

object[] objs = myReadOnlyCollection.ToArray();

(edit)

As Pop Catalin mentioned, the only works if T is a reference type. Use the Cast method otherwise.

(edit2)

Your update to the question changes things quite a bit ... I think that what you're trying to do is not possible. You want to cast to a explicit Type at compile time that is only available at runtime. In this case, the only way you have to access the collection is with reflection.

过期以后 2024-07-21 14:30:59
 var myArray = readOnlyCollection.Cast<object>().ToArray(); 
 var myArray = readOnlyCollection.Cast<object>().ToArray(); 
昔梦 2024-07-21 14:30:59

将集合转换为对象列表,然后调用 ToArray():

ReadOnlyCollection<string> s;
object[] o = s.Cast<object>().ToArray();

由于扩展方法的原因,仅适用于 C# 3.5。

Cast your collection to a list of objects, then call ToArray():

ReadOnlyCollection<string> s;
object[] o = s.Cast<object>().ToArray();

Works only in C# 3.5 because of the extension methods.

花想c 2024-07-21 14:30:59

您想要该系列的深度克隆吗? 如果您想深度克隆,请尝试以下代码进行内存深度克隆:

// deep copy in separeate memory space
public object Clone()
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, this);
    ms.Position = 0;
    object obj = bf.Deserialize(ms);
    ms.Close();
    return obj;
}

Do you want a deep clone of the collection? If you want to deep clone try this code below for memory deep cloning:

// deep copy in separeate memory space
public object Clone()
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, this);
    ms.Position = 0;
    object obj = bf.Deserialize(ms);
    ms.Close();
    return obj;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文