小编Lan*_*e H的帖子

C#如何将具有IList的对象转换为IList <object>?

我有一个实现IList接口的对象,我想把它投射到IList<object>或者 List<object>,我试过了

IList<object> a=(IList<object>)b;
List<object> a=(IList<object>)b;
IList<object> a=(List<object>)b;
List<object> a=(List<object>)b;
Run Code Online (Sandbox Code Playgroud)

这些都不起作用.请帮忙,谢谢.澄清:

b是从外部作为参数传递的对象.它实现了IList接口.例如,

public class a
{
  string name;
  List<a> names;
}
public void func(object item)
{
  object dataLeaves = data.GetType().GetProperty("names").GetValue(dataInput, null);
  if (dataLeaves != null && dataLeaves.GetType().GetInterfaces().Any(t =>t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IList<>)))
  {
    List<object> a=(List<object>) dataLeaves; //Need to convert the dataLeaves to list or IList
  }
}
Run Code Online (Sandbox Code Playgroud)

c#

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1