如何对通用列表对象进行排序 [英] How to sort through a generic list object

查看:130
本文介绍了如何对通用列表对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码旨在使用具有嵌套类的json.net解析未知的json文件,并创建复选框以轻松浏览它.

My code is designed to parse an unknown json file using json.net that has nested classes and create checkboxes to easily look through it.

在解析过程中的某个时刻,它到达一个List对象

At some point during the parsing it arrives to a List object

我有类型和实际对象

该类型具有AssemblyQualifiedName "System.Collections.Generic.List`1 [[Calibration.Camera,CalibrationOrganizer,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]],mscorlib,Version = 4.0. 0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 "

The type has AssemblyQualifiedName "System.Collections.Generic.List`1[[Calibration.Camera, CalibrationOrganizer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

对象是List<Calibration.Camera>

如果我以这种方式遍历列表,那么它会起作用,但是问题是我假设一个已知的数据类型,但我没有.这只是json文件中许多数据类型之一

If I loop through the List this way it works, but the problem is I'm assuming a known data type, which I don't. This is just one of many data types in the json file

if (currentType.Name == "List`1")
{
    for (int i = 0; i < ((List<Calibration.Camera>)currentValue).Count; i++)
    {
        cbox = new CheckBox();
        cbox.Text = "[" + i.ToString() + "]";
        cbox.Name = prev_properties + "!" + curr_property;
        cbox.AutoSize = true;
        cbox.Location = new Point(1100 + prop_list.Count() * 100, i++ * 20); //vertical
        cbox.CheckedChanged += new EventHandler(ck_CheckedChanged);
        this.Controls.Add(cbox);
    }   
}

如果我尝试这样做,它将无法编译

If I try this, it wont compile

for (int i = 0; i < currentValue.Count; i++) {...}

,出现错误:Operator '<' cannot be applied to operands of type 'int' and 'method group'

如果我尝试此操作,它将崩溃

If I try this, it crashes

for (int i = 0; i < ((List<object>)currentValue).Count; i++)

,但例外:System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List1[Calibration.Camera]' to type 'System.Collections.Generic.List1[System.Object]'.'

所以我不确定我能做什么,

So Im not sure what I can do,

我可以解析AssemblyQualifiedName并将对象类型作为字符串获取,但是如何再次将其转换为对象类型?

I can parse the AssemblyQualifiedName and get the Object type as a string, but how do I convert it to an object type again?

推荐答案

如果提前进行转换,则要简单得多.例如:

It's a lot simpler if you cast it ahead of time. For example:

if (currentType.Name == "List`1")
{
    var list = currentValue as List<Calibration.Camera>;
    if (list == null) throw new ArgumentException("currentType and currentValue do not match.");
    foreach (var camera in list)
    {
        cbox = new CheckBox
        {
            Text = "[" + i.ToString() + "]",
            Name = prev_properties + "!" + curr_property,
            AutoSize = true,
            Location = new Point(1100 + prop_list.Count() * 100, i++ * 20) //vertical
        };
        cbox.CheckedChanged += new EventHandler(ck_CheckedChanged);
        this.Controls.Add(cbox);               
    }
}

在此示例中,我们使用as投射"并进行空检查以确保它是正确的类型(您可以抛出异常,也可以根据需要随意跳过它).

In this example, we "cast" using as and do a null check to ensure it's the right type (you can throw an exception or just skip it silently if you prefer).

如果不确定它是否是实际的List<Camera>,但您知道它是可枚举的,则可以将其强制转换为非泛型IEnumerable(而不是IEnumerable<T>),然后在其上使用OfType<>.这样将提取列表中与所需类型匹配的所有内容,并将其转换为通用/类型化的IEnumerable<T>,然后可以在其上使用ToList().

If you're not sure it's an actual List<Camera> but you know it's some kind of enumerable, you can cast to a non-generic IEnumerable (not IEnumerable<T>) and then use OfType<> on it. That will extract anything in the list that matches the type you want, and also cast it to a generic/typed IEnumerable<T>, which you can then use ToList() on.

if (currentType.Name == "List`1")
{
    var enumerable = currentValue as IEnumerable;
    if (enumerable == null) throw new ArgumentException("Doesn't seem to be a list.");
    var list = enumerable.OfType<Calibration.Camera>().ToList();
    foreach (var camera in list)
    {
        //etc....

这篇关于如何对通用列表对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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