IList.Cast<typeof(T)>() returns error, syntax looks ok
-
09-06-2019 - |
Question
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?
Solution
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();
}
OTHER TIPS
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 anIList<T>
. AnIList
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.
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.
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.
T is already a type parameter, you don't need to call typeof on it. TypeOf takes a type and returns its type parameter.
"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.
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>();
@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
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.
@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>();
}
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()