铸造结果从泛型方法调用? [英] Casting Results from Generic Method Invocation?

查看:136
本文介绍了铸造结果从泛型方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前搞乱与仿制药,我试着写,我可以调用加载一切从一个数据库表只需指定表名的功能。

我大部分的方式出现;我一般的方法都似乎工作,但我不太清楚怎么投我的成果转化为可用的东西。

这是该方法的胆量至今:

 私有静态列表<实体codeBase的>获得codeLoadResults(codeTABLES表)
{
    名单<实体codeBase的>结果=新名单,其中,实体codeBase的>();
    大会ASSM =的Assembly.Load(新System.Reflection.AssemblyName(RR));
    键入TABLETYPE = assm.GetTypes()式(U => u.Name.ToLower()== table.ToString()TOLOWER())。FirstOrDefault()。
    MethodInfo的MI = typeof运算(SpecificEntity).GetMethod(LOADALL);

    MI = mi.MakeGenericMethod(TABLETYPE);
    mi.Invoke(NULL,NULL); //我怎么能投生成的对象变成一个名单,其中,实体codeBase的> ?

    返回结果;
}
 

解决方案

假设 SpecificEntity.LoadAll 返回某种类型从实体$导出列表C $ CBase的,你不能直接转换为名单,其中,实体codeBase的> ,但你可以转换为的IEnumerable<实体codeBase的> 。然后,您可以创建一个新的列表:

  VAR ecbList =(IEnumerable的<实体codeBase的>)mi.Invoke(NULL,NULL);
返回list.ToList();
 

这可能是更清洁但是,如果你可以从实体类型得到表名,可以直接通过名称,使用属性,或使用地图。然后你就可以得到codeLoadResults 通用的结果类型,例如

 私有静态列表< T>获得codeLoadResults()其中T:实体codeBase的
{
    大会ASSM =的Assembly.Load(新System.Reflection.AssemblyName(RR));
    类型TABLETYPE = //从T台上的类型
    MethodInfo的MI = typeof运算(SpecificEntity).GetMethod(LOADALL);

    MI = mi.MakeGenericMethod(TABLETYPE);
    返程(名单< T>)mi.Invoke(NULL,NULL);
}
 

如果你不使用.NET 4中,你不能施放一个名单,其中,TDerived> 的IEnumerable< TBASE> ,所以你必须要转换为的IEnumerable 第一:

 收益率((System.Collections.IEnumerable)mi.Invoke(NULL,NULL))
    .Cast<实体codeBase的>()
    .ToList();
 

I'm currently messing about with generics and I'm trying to write a function that I can call to load everything from a database table simply by specifying the table name.

I'm most of the way there; my generic methods all seem to work, but I'm not quite sure how to cast my results into something usable.

This is the guts of the method so far:

private static List<EntityCodeBase> GetCodeLoadResults(CodeTables table)
{
    List<EntityCodeBase> results = new List<EntityCodeBase>();
    Assembly assm = Assembly.Load(new System.Reflection.AssemblyName("RR"));
    Type tableType = assm.GetTypes().Where(u => u.Name.ToLower() == table.ToString().ToLower()).FirstOrDefault();
    MethodInfo mi = typeof(SpecificEntity).GetMethod("LoadAll");

    mi = mi.MakeGenericMethod(tableType);
    mi.Invoke(null, null); //how can I cast the resulting object into a List<EntityCodeBase> ?

    return results;
}

解决方案

Assuming SpecificEntity.LoadAll returns a list of some type derived from EntityCodeBase, you can't cast directly to a List<EntityCodeBase> but you can cast to IEnumerable<EntityCodeBase>. Then you can create a new list:

var ecbList = (IEnumerable<EntityCodeBase>)mi.Invoke(null, null);
return list.ToList();

It might be cleaner however, if you can get the table name from the entity type, either directly by name, using attributes, or using a map. Then you can make GetCodeLoadResults generic in the result type e.g.

private static List<T> GetCodeLoadResults() where T : EntityCodeBase
{
    Assembly assm = Assembly.Load(new System.Reflection.AssemblyName("RR"));
    Type tableType = //get table type from T
    MethodInfo mi = typeof(SpecificEntity).GetMethod("LoadAll");

    mi = mi.MakeGenericMethod(tableType);
    return (List<T>)mi.Invoke(null, null);
}

If you're not using .Net 4, you can't cast a List<TDerived> to an IEnumerable<TBase>, so you'll have to cast to IEnumerable first:

return ((System.Collections.IEnumerable)mi.Invoke(null, null))
    .Cast<EntityCodeBase>()
    .ToList();

这篇关于铸造结果从泛型方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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