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?

+4
source share
11 answers

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(); } 
+7
source

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 IList definitely different from IList<T> . IList not 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.

+5
source

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

+1
source

IList is IList <T>, he just got futared by markdowns when she published it. I tried to format it, but I missed the <T> escaping .. Fixed it now.

+1
source

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.

+1
source

"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.

+1
source

@ Jonathan Holland

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

typeof "accepts" the type and returns it System.Type

+1
source

You have too many temporary variables that are confusing

instead

 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>(); 
+1
source

@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>(); } 
0
source

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.

0
source

The most striking mistake I see is that IList definitely different from IList<T> . IList not common (e.g. ArrayList ).

So your method signature should be:

 public static IList<T> LoadObjectListAll() 
-1
source

Source: https://habr.com/ru/post/1276539/


All Articles