从对象存储一个值列表到对象列表的对象的问题

发布于 2025-02-04 16:25:52 字数 1011 浏览 3 评论 0原文

我有一个将对象作为输入的函数。仅清单< t>各种t将作为类型传递,但是由于某些外部/API原因,该论点需要作为对象保留。

void Receive(object collection)

我们不要讨论这是一个不好的设计:)假设这是一个约束。

情况如下。 我想这次将参数施放或转换为枚举(或列表也可以做的)对象。 同样,它必须是枚举的/对象列表,而不是特定类型的列表。部分是因为我不知道确切的类型。

void Receive(object collection)
{
   var myList = (IEnumerable<object>)collection;
}

让我感到困惑的是,这似乎仅适用于参考类型。 当这样称呼时,它

 Receive(new List<Foo>() { new Foo()}}

可以工作:这样称呼这样的话:

Receive(new List<int>() {2}
Receive(new List<(int,int)>() {(5,9)}

无法施放类型的对象'system.collections.generic.list 1 [system.int32]'tote'system.collections.generic.generic.enumosity 1 [system.object]

我猜这与缺乏一些价值类型的转换操作员。

问题:

如何施放它,因此当参考类型的列表或值类型作为参数传递时,它起作用了吗?

我已经看到人们在将特定类型的列表列入对象列表时也有类似的问题,但这很容易(例如,我们可以使用Tolist)。我的情况更加困难,因为我首先没有呼叫任何方法的地方。我只有这个对象,所以我需要先进行某种铸造。

I have a function that takes object as an input. Only List< T > of various T will be passed here as the type, but the argument needs to stay as object for some external/API reasons.

void Receive(object collection)

Let's not discuss that this is a bad design :) Assume that this is a constraint.

The situation is as follows.
I want to cast or convert my parameter to an enumerable (or a list could do too) of objects this time.
Again, it must be enumerable/list of objects, not of the specific type. Partly because I don't know the exact type.

void Receive(object collection)
{
   var myList = (IEnumerable<object>)collection;
}

What boggles me is that this only seems to work with reference types.
It works when called like this:

 Receive(new List<Foo>() { new Foo()}}

It does not work when called like this:

Receive(new List<int>() {2}
Receive(new List<(int,int)>() {(5,9)}

Unable to cast object of type 'System.Collections.Generic.List1[System.Int32]' to type 'System.Collections.Generic.IEnumerable1[System.Object]

I guess this is related to the lack of some conversion operator for value types.

Question:

How to cast it, so it works when either a list of reference types or value types is passed as the argument?

I have seen people having similar issues when casting a list of specific type to a list of objects but this is easy (e.g. we can use ToList). My situation is more difficult because I don't have the IEnumerable in the first place to call any methods. I only have the object, so I need to do some kind of casting first.

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

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

发布评论

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

评论(1

∞觅青森が 2025-02-11 16:25:52

如何施放

已经遇到错误,这意味着这是不可能的。您需要改变方法。

我建议将集合转换为system.collections.ienumerable。您可以在操作中使用它并将项目视为对象。

public void Receive(object collection)
{
    var myList = (IEnumerable)collection;
    
    // value is object here
    foreach(var value in myList)
    {

    }
}

How to cast it

You are already getting error, it means that's impossible. You need to change your approach.

I propose to convert the collection to System.Collections.IEnumerable. You can use it in foreach operation and treat the items as object.

public void Receive(object collection)
{
    var myList = (IEnumerable)collection;
    
    // value is object here
    foreach(var value in myList)
    {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文