类中的通用方法

isx*_*ker 0 .net c# generics type-conversion

我的代码:

public class MyClass
{
    private WdmEntities _context;

    public List<T> GetTable<T>()
    {
        List<T> res = new List<T>();
        _context = new DbEntities();

        if (typeof(T) == typeof(tables))
            res = _context.tables.ToList();//error cannot implicitly convert type
        if (typeof(T) == typeof(columns))
            res = _context.columns.ToList();//error cannot implicitly convert type

        return res;
    }

}
Run Code Online (Sandbox Code Playgroud)

我有来自EntityModel的表和列类型

但我得到编译错误:

 cannot implicitly convert type
Run Code Online (Sandbox Code Playgroud)

如何更改我的代码以使其工作?

jga*_*fin 5

你需要投射每个项目:

public class MyClass
{
    private WdmEntities _context;

    public List<T> GetTable<T>()
    {
        List<T> res = new List<T>();
        _context = new DbEntities();
        if (typeof(T) == typeof(tables))

            // If the defined entity type is an abstract class or interface
            // while the returned type from the dbcontext really is of your object type
            res = _context.tables.Cast<T>().ToList();
        if (typeof(T) == typeof(columns))
            res = _context.columns.Cast<T>().ToList();

        return res;
    }

}
Run Code Online (Sandbox Code Playgroud)

或只是集合:

public class MyClass
{
    private WdmEntities _context;

    public List<T> GetTable<T>()
    {
        List<T> res = new List<T>();
        _context = new DbEntities();
        if (typeof(T) == typeof(tables))

            //Just casting the returned list, if the entity type is the same as T
            res = (List<T>)_context.tables.ToList();
        if (typeof(T) == typeof(columns))
            res = (List<T>)_context.columns.ToList();

        return res;
    }

}
Run Code Online (Sandbox Code Playgroud)

..取决于您的上下文的创建方式.


Mar*_*ell 5

在这样一行:

    if (typeof(T) == typeof(tables))
        res = _context.tables.ToList();
Run Code Online (Sandbox Code Playgroud)

编译器不会尝试理解此if测试如何影响分配规则; 就它而言,你试图分配List<table>给a List<T>,这不行,因为编译器不相信你table=== T.

您可以使用强制转换强制它:

    if (typeof(T) == typeof(tables))
        res = (List<T>)(object)_context.tables.ToList();
Run Code Online (Sandbox Code Playgroud)

但是,您可能想要检查您_context是否具有通用API.例如,在LINQ-to-SQL中:

res = _context.GetTable<T>().ToList();
Run Code Online (Sandbox Code Playgroud)

一个不相关的说明:调用ToList()未经过滤的表通常是一件非常糟糕的事情.

因此,最好返回IQueryable<T>ORM或首选的IQueryable<T>包装器.例如,使用LINQ-to-SQL,这可能是:

public Table<T> GetTable<T>()
{
    return ctx.GetTable<T>();
}
Run Code Online (Sandbox Code Playgroud)

或与实体框架:

public DbDet<T> GetTable<T>()
{
    return ctx.Set<T>();
}
Run Code Online (Sandbox Code Playgroud)

但是,您还应该仔细考虑数据上下文的生命周期管理; 在问题的代码中,你new是数据上下文 - 然后这个上下文是自由浮动的,并且不会被我认为是可接受的方式清理.你应该考虑更多的终身管理.