所以我有一个接受泛型类型参数的类,如果type参数是给定类型的子类,则会进行一些特殊处理.
IEnumerable<T> models = ... // Special handling of MySpecialModel if (filterString != null && typeof(MySpecialModel).IsAssignableFrom(typeof(T))) { var filters = filterString.Split(...); models = from m in models.Cast<MySpecialModel>() where (from t in m.Tags from f in filters where t.IndexOf(f,StringComparison.CurrentCultureIgnoreCase) >= 0 select t) .Any() select (T)m; }
但是我在最后一行得到例外
Cannot convert type 'MySpecialModel' to 'T'
The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint.
我在这里错过了什么?
更新