方法 为:
Public List<Class1> Method()
{}反射调用
Type t = dicType[strFullName];
object objService = dicObj[strFullName];
MethodInfo mi = t.GetMethod(strMethod);
object obj = mi.Invoke(objService,null) ;返回的obj 是 Object 类型的,怎么转成List<Class1>?
Public List<Class1> Method()
{}反射调用
Type t = dicType[strFullName];
object objService = dicObj[strFullName];
MethodInfo mi = t.GetMethod(strMethod);
object obj = mi.Invoke(objService,null) ;返回的obj 是 Object 类型的,怎么转成List<Class1>?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var list = Enumerable.Range(0, 5).Select(x => new { ID = x, Name = "Name" + x.ToString() }).ToList();
object obj = list;
var itor = obj.GetType().GetMethods().Where(x => x.Name == "GetEnumerator").First().Invoke(obj, new object[] { }) as System.Collections.IEnumerator;
while (itor.MoveNext())
{
object current = itor.Current;
string Name = current.GetType().GetProperties().Where(x => x.Name == "Name").First().GetValue(current, new object[] { }).ToString();
Console.WriteLine(Name);
}
}
}
}
那个DLL 是反射调用的,List<Class1> 这个类型是DLL中的,没有加引用,就不知道这个类型,不能用as 的
不是悖论,场景是这样的,有多个DLL,返回的类型是List<Class1> , 现在要用配置方式+反射的方式调用不同的DLL
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var list = Enumerable.Range(0, 5).Select(x => new { ID = x, Name = "Name" + x.ToString() }).ToList();
object obj = list;
foreach (var item in (obj as IEnumerable<object>))
{
string Name = item.GetType().GetProperties().Where(x => x.Name == "Name").First().GetValue(item, new object[] { }).ToString();
Console.WriteLine(Name);
}
}
}
}
其实这样就可以了。不过只有C#4.0+才支持。