简体   繁体   English

无法转换对象匿名通用列表

[英]Unable to Cast Object Anonymous Generic List

If you have any idea how I would enumerate the object list please let me know. 如果您有任何想法要枚举对象列表,请告诉我。 Here is how it is created. 这是它的创建方式。 I've been unable to cast it to a List, IList, Enumerable, IEnumerable for some reason. 由于某种原因,我一直无法将其转换为List,IList,Enumerable,IEnumerable。 I'm guessing it's because of how it was created (by a third party). 我猜这是因为它是由第三方创建的。 Just seeing if anyone has any ideas. 只要看看是否有人有任何想法。

The error is as follows: 错误如下:

Unable to cast object of type '<>f__AnonymousType5`1[System.Collections.Generic.List`1[SugarRest.Model.AMP_Product_Line]]' to type 'System.Collections.Generic.List`1[SugarRest.Model.AMP_Product_Line]'.

The object is created as follows: 对象创建如下:

private static AMP_Contract CreateCrmContract(ContractDetailViewModel model, int bookmanContractNumber, int renewedFromContractNumber)
        {
            List<AMP_Product_Line> productLines = CreateProductLinesPrint(model, bookmanContractNumber);

            //Contract
            AMP_Contract ampContract = new AMP_Contract();

            ...

            ampContract.amp_amp_contracts_amp_amp_product_lines = new { productLines };

            return ampContract;
        }

public class AMP_Contract
    {              
        ...
        public object amp_amp_contracts_amp_amp_product_lines { get; set; }
        ...
    }

The object that I am trying to access/enumerate is as follows: 我尝试访问/枚举的对象如下:

vc

在此处输入图片说明

I also tried this, however, the object in question is not enumerable as it it an object. 我也尝试过此操作,但是,该对象不可枚举,因为它是一个对象。

在此处输入图片说明

First off: whoever wrote this code is actively attempting to stop you from accessing that collection. 首先:编写此代码的人都在积极尝试阻止您访问该集合。 You should think very hard about whether it is the right thing to try to defeat their attempt; 您应该认真思考是否应该挫败他们的企图。 they probably are hiding that for a reason. 他们可能将其隐藏是有原因的。

The easiest way to do that is to use dynamic to read the value out of the anonymous type. 最简单的方法是使用dynamic从匿名类型中读取值。 You can then do a dynamic conversion to the sequence type you need: 然后,您可以动态转换为所需的序列类型:

var contract = CreateCrmContract(...whatever...);
dynamic d = contract.amp_amp_contracts_amp_amp_product_lines;
IEnumerable<AMP_Product_Line> lines = d.productLines;

And now we are back in the statically typed world: 现在我们回到了静态类型世界:

foreach (AMP_Product_Line line in lines)
  Console.WriteLine(line);

you must write a method for this 您必须为此编写一个方法

public static object ToNonAnonymousList<T>(this List<T> list, Type t)
{

   //define system Type representing List of objects of T type:
   var genericType = typeof(List<>).MakeGenericType(t);

   //create an object instance of defined type:
   var l = Activator.CreateInstance(genericType);

   //get method Add from from the list:
   MethodInfo addMethod = l.GetType().GetMethod("Add");

   //loop through the calling list:
   foreach (T item in list)
   {

      //convert each object of the list into T object 
      //by calling extension ToType<T>()
      //Add this object to newly created list:
      addMethod.Invoke(l, new object[] { item.ToType(t) });
   }

   //return List of T objects:
   return l;
}

use of 用于

var genericType = typeof(List<>).MakeGenericType(t);

review for example 例如审查

https://www.codeproject.com/Articles/38635/Converting-anonymous-types-to-any-type https://www.codeproject.com/Articles/38635/Converting-anonymous-types-to-any-type

无法投射“object {System.Collections.Generic.List”类型的 object<string[]> }”作为ICollection<object><div id="text_translate"><p> 为什么不能将 object 在运行时的类型为object {System.Collections.Generic.List&lt;string[]&gt;}为ICollection&lt;object&gt; ?</p><p> 我确定它很明显,但我无法弄清楚。</p><p> 它给出了例外 -</p><blockquote><p> 无法将类型为“System.Collections.Generic.List 1[System.String[]]' to type 'System.Collections.Generic.ICollection ]”。</p></blockquote></div></object></string[]> - Unable to cast an object of type “object {System.Collections.Generic.List<string[]>}” as ICollection<object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换通用对象 - Unable to cast generic object 将 Object 转换为通用列表 - Cast Object to Generic List 将对象转换为通用列表 - Cast an object into generic list 无法转换&#39;System.Collections.Generic.List`1类型的对象 - Unable to cast object of type 'System.Collections.Generic.List`1 无法将匿名列表转换为LINQ中的已知列表 - Unable to cast anonymous list into known list in LINQ 无法将通用记录转换为对象 - Unable to cast Generic record to object 无法将物件投放至清单 - Unable to cast object to list 无法将通用列表转换为自定义列表类型 - Unable to cast Generic List to Custom List type 无法将“System.Collections.Generic.List”类型的 object 转换为“System.Collections.Generic.ICollection”类型 - Unable to cast object of type 'System.Collections.Generic.List to type 'System.Collections.Generic.ICollection 无法投射“object {System.Collections.Generic.List”类型的 object<string[]> }”作为ICollection<object><div id="text_translate"><p> 为什么不能将 object 在运行时的类型为object {System.Collections.Generic.List&lt;string[]&gt;}为ICollection&lt;object&gt; ?</p><p> 我确定它很明显,但我无法弄清楚。</p><p> 它给出了例外 -</p><blockquote><p> 无法将类型为“System.Collections.Generic.List 1[System.String[]]' to type 'System.Collections.Generic.ICollection ]”。</p></blockquote></div></object></string[]> - Unable to cast an object of type “object {System.Collections.Generic.List<string[]>}” as ICollection<object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM