views:

2061

answers:

13
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'm getting a build error where I am casting the "list" element to a generic IList .... can anyone see a glaring error here?

+5  A: 

I think

var castList = list.Cast<typeof(T)>();

should be

var castList = list.Cast<T>();

@Jon Limjap The most glaring error I can see is that an IList is definitely different from an IList<T>. An IList is non-generic (e.g., ArrayList).

The original question was already using an IList<T>. It was removed when someone edited the formatting. Probably a problem with Markdown.

Fixed now.

John
+1  A: 

T is already a type parameter, you don't need to call typeof on it. TypeOf takes a type and returns its type parameter.

FlySwat
You are wrong. `typeof` takes a compile-time type identifier and returns a System.Type instance for it.
SLaks
A: 

@Jon and @Jonathan is correct, but you also have to change the return type to

IList<T>

also. Unless that is just a markdown bug.

@Jonathan, figured that was the case.

I am not sure what version of nHibernate you are using. I haven't tried the gold release of 2.0 yet, but you could clean the method up some, by removing some 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>();
}
Dale Ragan
A: 

The most glaring error I can see is that an IList is definitely different from an IList<T>. An IList is non-generic (e.g., ArrayList).

So your method signature should be:

public static IList<T> LoadObjectListAll()
Jon Limjap
+1  A: 

The IList is an IList<T>, it just got fubared by markdown when she posted it. I tried to format it, but I missed escaping the <T>..Fixing that now.

FlySwat
+7  A: 

T is not a type nor a System.Type. T is a type parameter. typeof(T) returns the type of T. The typeof operator does not act on an object, it returns the Type object of a type. http://msdn.microsoft.com/en-us/library/58918ffs.aspx

@John is correct in answering your direct question. But the NHibernate code there is a little off. You shouldn't be configuring the ISessionFactory after getting the ISession, for example.

public static T[] LoadObjectListAll()
{
    var session = GetNewSession();
    var criteria = session.CreateCriteria(typeof(T));
    var results = criteria.List<T>();
    return results.ToArray();        
}
Matt Hinze
A: 

CLI only supports generic arguments for covariance and contravariance when using delegates, but when using generics there are some limitations, for example, you can cast a string to an object so most people will assume that you can do the same with List<string> to a List<object> but you can't do that because there is no covariance between generic parameters however you can simulate covariance as you can see in this article. So it depends on the runtime type that it is generated by the abstract factory.

Erick Sgarbi
+1  A: 

CLI only supports generic arguments for covariance and contravariance when using delegates, but when using generics there are some limitations, for example, you can cast a string to an object so most people will assume that you can do the same with List to a List but you can't do that because there is no covariance between generic parameters however you can simulate covariance as you can see in this article. So it depends on the runtime type that it is generated by the abstract factory.

That reads like a markov chain... Bravo.

FlySwat
+1  A: 

"The original question was already using an IList<T>. It was removed when someone edited the formatting. Probably a problem with Markdown."

Thats what i saw but it was edited by someone and that's the reason why I put my explanation about covariance but for some reason i was marked down to -1.

Erick Sgarbi
+1  A: 

@Jonathan Holland

T is already a type parameter, you don't need to call typeof on it. TypeOf takes a type and returns its type parameter.

typeof "takes" a type and returns its System.Type

Matt Hinze
+1  A: 

You have too many temporary variables which are confusing

instead of

var returnList = session.CreateCriteria(typeof(T));
var list = returnList.List();
var castList = list.Cast<typeof(T)>();
return castList;

Just do

return session.CreateCriteria(typeof(T)).List().Cast<T>();
Orion Edwards
A: 

Matt is closest to the answer, I think. It's still not working. According to VS 2008 the rest of you are wrong, those are all options I thought about and tried.... (exept for eliminating unnec variables)

This is very frustrating. According for every bit of documentation I read var castList = list.Cast(); When I comment it out and step through all the other values look great, stepping away for a while onto somethign else.

should be accurate, I mean I can change it tby iterating through the list and adding the values to the new one but that's so dang ugly.

Sara Chipps
A: 

Matt, you are awesome, thanks.

(why is his CORRECT answer -1?)

Sara Chipps