【问题标题】:Convert generic object list to non-generic type list将泛型对象列表转换为非泛型类型列表
【发布时间】:2015-06-04 06:15:27
【问题描述】:

我正在尝试将 System.Object 对象的 List 转换为强类型对象的 List

这是我得到的错误:

“System.Collections.Generic.List`1[System.Object]”类型的对象无法转换为“System.Collections.Generic.List`1[TestApp.Tsc_Mrc_Step]”类型。

目的是因为我正在为我的项目编写一个业务数据层,您所要做的就是将您的类和属性命名为与数据库中的实体相同的名称,并且数据层将自动填充引用的表作为类型在类中声明。

业务数据层使用反射、泛型和对象来处理所有这些。

下面是我尝试将对象列表放入已知类型列表的代码。问题是,对象是已知类型,但我将其作为对象传递....如何在不知道它是什么的情况下将其转换为已知类型?

            bool isCoollection = false;
            Type t = GetTypeInsideOfObjectByTypeName(o, tableName, out isCoollection);

            List<object> objectColl = new List<object>();

            object obj = Activator.CreateInstance(t);
            if (obj != null)
            {
                PropertyInfo[] objectProps = obj.GetType().GetProperties();
                foreach (PropertyInfo op in objectProps)
                {
                    if (HasColumn(reader, op.Name))
                    {
                        op.SetValue(obj, reader[op.Name]);
                    }
                }

                if (isCoollection)
                {
                    objectColl.Add(obj);
                }
            }

            if (isCoollection)
            {
                IEnumerable<object> objs = objectColl.AsEnumerable();

                SetObject(o, objs);
            }
            else
            {
                SetObject(o, obj);
            }

这里是 SetObject:

            public static void SetObject(object parentObject, object newObject)
            {
                PropertyInfo[] props = parentObject.GetType().GetProperties();
                string typeName = newObject.GetType().Name;
                foreach (PropertyInfo pi in props)
                {
                    if (pi.PropertyType.Name.ToLower() == typeName.ToLower())
                    {
                        pi.SetValue(parentObject, newObject);
                    }
                    else if (!pi.PropertyType.IsValueType && !pi.PropertyType.Namespace.ToLower().Contains("system"))
                    {
                        SetObject(pi.GetValue(parentObject), newObject);
                    }
                }
            }

【问题讨论】:

  • 你能发布你的尝试吗?
  • 你能添加你使用的代码(或者至少是暗示强类型列表的sn-ps
  • 同时使用反射泛型通常意味着你不应该使用either

标签: c# object generics reflection


【解决方案1】:

如果您知道列表中的所有值都是所需类型:

List<Object> objects;
List<Cat> cats = objects.Cast<Cat>().ToList();

如果不是所有的值都属于该类型,并且您想剔除不属于该类型的值:

List<Object> objects;
List<Cat> cats = objects.OfType<Cat>().ToList();

两者都需要 LINQ。

如果直到运行时才知道类型,则必须使用反射。

How to call a generic method through reflection

【讨论】:

  • 但我只知道类型,而不是类名:(
  • @deccks 但是...类型是类名
  • 但我怎么能这样做:Type t = GetTypeInsideOfObjectByTypeName(o, tableName, out isCoollection); IEnumerable 猫 = objects.OfType().AsEnumerable();
  • @deccks 添加了类似答案的链接。
【解决方案2】:

好的,我完成了我的目标。这一切都归功于动态变量。让我印象深刻的是,我能够使用 .NET 做到这一点。你怎么看?谢谢:)

Type t = GetTypeInsideOfObjectByTypeName(o, tableName, out isCoollection);

Type genericListType = typeof(List<>).MakeGenericType(t);

object coll = Activator.CreateInstance(genericListType);

dynamic objectColl = Convert.ChangeType(coll, coll.GetType());

dynamic d = Convert.ChangeType(obj, obj.GetType());

objectColl.Add(d);

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多