相关疑难解决方法(0)

如何从字符串表示中获取泛型类型?

我有MyClass<T>.

然后我有了这个string s = "MyClass<AnotherClass>";.如何从字符串中获取Type s

一种方法(丑陋)是解析"<"和">"并执行:

Type acType = Type.GetType("AnotherClass");  
Type whatIwant = typeof (MyClass<>).MakeGenericType(acType);
Run Code Online (Sandbox Code Playgroud)

但有没有更简洁的方法来获得最终类型,没有任何解析等?

c# reflection types

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

实体框架的通用存储库方法出错

我正在尝试创建一个通用方法在我的基类中用于我的存储库,我遇到了问题.这是方法......

        public virtual T First(System.Linq.Expressions.Expression<Func<T, bool>> where, List<string> properties)
    {
        IQueryable<T> query = null;
        if (where != null)
        {
            query = _context.CreateQuery<T>(String.Format("[{0}]", typeof(T).Name.ToString())).Where(where);
        }
        else
        {
            query = _context.CreateQuery<T>(String.Format("[{0}]", typeof(T).Name.ToString()));
        }

        foreach (string s in properties)
        {
            query = query.Include(s);
        }

        T _result = (T)query.First();

        return _result;
    }
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,它给了我这个错误:

无法在当前范围或上下文中解决"公司"问题.确保所有引用的变量都在范围内,加载了所需的模式,并正确引用了名称空间.近似转义标识符,第1行,第1列.

我知道它为什么这样做,我只是不知道如何解决它.我认为它正在这样做,因为我的ObjectContext不知道对象"公司",但它确实知道"公司".有想法该怎么解决这个吗??

错误发生在这一行:

T _result =(T)query.First();

谢谢!

.net c# entity-framework

2
推荐指数
2
解决办法
3530
查看次数

标签 统计

c# ×2

.net ×1

entity-framework ×1

reflection ×1

types ×1