我有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)
但有没有更简洁的方法来获得最终类型,没有任何解析等?
我正在尝试创建一个通用方法在我的基类中用于我的存储库,我遇到了问题.这是方法......
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();
谢谢!