IList.Cast <typeof (T)> () returns an error, the syntax looks fine
public static IList<T> LoadObjectListAll<T>() { ISession session = CheckForExistingSession(); var cfg = new NHibernate.Cfg.Configuration().Configure(); var returnList = session.CreateCriteria(typeof(T)); var list = returnList.List(); var castList = list.Cast<typeof(T)>(); return castList; } So, I get a build error when I overlay a list item on a generic IList ... can anyone see a blatant error here?
T not a type or System.Type . T is a type parameter. typeof(T) returns type T The typeof operator does not affect the object; it returns a Type object of type. http://msdn.microsoft.com/en-us/library/58918ffs.aspx
@Jon is right in answering your direct question. But NHibernate code doesn't work a bit. You should not configure ISessionFactory after receiving ISession , for example.
public static T[] LoadObjectListAll() { var session = GetNewSession(); var criteria = session.CreateCriteria(typeof(T)); var results = criteria.List<T>(); return results.ToArray(); } I think,
var castList = list.Cast<typeof(T)>(); it should be
var castList = list.Cast<T>(); @Jon Limjap The most striking mistake I see is that a
IListdefinitely different fromIList<T>.IListnot common (e.g.ArrayList).
The original question already used IList<T> . It was deleted when someone edited the formatting. Probably a problem with Markdown.
Fixed.
The CLI only supports general arguments for covariance and contravariance when using delegates, but when using generics, there are some restrictions, for example, you can impose a string on an object, so most people will assume that you can do the same with List to List, but you donβt you can do this because there is no covariance between the generic parameters, however you can imitate the covariance, as you can see in this article. Thus, it depends on the type of runtime that it generates in the abstract factory.
It reads like a chain of stamps ... Bravo.
"The original question already used IList<T> . It was deleted when someone edited the formatting. Probably a problem with Markdown."
Here is what I saw, but it was edited by someone and that is the reason why I posted my explanation regarding covariance, but for some reason I was marked to -1.
@Jon and @Jonathan are correct, but you must also change the return type to
IList<T> also. If this is not just a markdown mistake.
@ Jonathan , I realized that it is.
I'm not sure which version of nHibernate you are using. I have not tried the release of gold version 2.0 yet, but you can clear the method to some by deleting a few lines:
public static IList<T> LoadObjectListAll() { ISession session = CheckForExistingSession(); // Not sure if you can configure a session after retrieving it. CheckForExistingSession should have this logic. // var cfg = new NHibernate.Cfg.Configuration().Configure(); var criteria = session.CreateCriteria(typeof(T)); return criteria.List<T>(); } The CLI only supports general arguments for covariance and contravariance when using delegates, but when using generics, there are some restrictions, for example, you can attach a string to an object, so most people will assume that you can do the same from List<string> to List<object> , but you cannot do this because there is no covariance between the generic parameters, however you can simulate the covariance, as you can see in this article. Thus, it depends on the type of runtime that it generates in the abstract factory.