<pre><code>public static IList&lt;T&gt; LoadObjectListAll&lt;T&gt;()
{
    ISession session = CheckForExistingSession();
    var cfg = new NHibernate.Cfg.Configuration().Configure();
    var returnList = session.CreateCriteria(typeof(T));
    var list = returnList.List();
    var castList = list.Cast&lt;typeof(T)&gt;();
    return castList;
}
</code></pre>

<p>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? </p>

[{'answer_id': 43128, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 3, 'selected': False, 'text': '<p>I think </p>\n\n<pre><code>var castList = list.Cast&lt;typeof(T)&gt;();\n</code></pre>\n\n<p>should be </p>\n\n<pre><code>var castList = list.Cast&lt;T&gt;();\n</code></pre>\n\n<blockquote>\n  <p>@Jon Limjap The most glaring error I can see is\n  that an <code>IList</code> is definitely different from an <code>IList&lt;T&gt;</code>. An <code>IList</code> is non-generic\n  (e.g., <code>ArrayList</code>).</p>\n</blockquote>\n\n<p>The original question was already using an <code>IList&lt;T&gt;</code>. It was removed when someone edited the formatting. Probably a problem with Markdown.</p>\n\n<p><strong>Fixed now.</strong></p>\n'}, {'answer_id': 43130, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': "<p>T is already a type parameter, you don't need to call typeof on it. TypeOf takes a type and returns its type parameter.</p>\n"}, {'answer_id': 43131, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>@Jon and @Jonathan is correct, but you also have to change the return type to</p>\n\n<pre><code>IList&lt;T&gt;\n</code></pre>\n\n<p>also.  Unless that is just a markdown bug.</p>\n\n<p>@<a href="https://stackoverflow.com/questions/43126/ilistcasttypeoft-returns-error-syntax-looks-ok#43137">Jonathan</a>, figured that was the case.</p>\n\n<p>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:</p>\n\n<pre><code>public static IList&lt;T&gt; LoadObjectListAll()\n{\n    ISession session = CheckForExistingSession();\n    // Not sure if you can configure a session after retrieving it.  CheckForExistingSession should have this logic.\n    // var cfg = new NHibernate.Cfg.Configuration().Configure();\n    var criteria = session.CreateCriteria(typeof(T));\n    return criteria.List&lt;T&gt;();\n}\n</code></pre>\n'}, {'answer_id': 43132, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': -1, 'selected': False, 'text': '<p>The most glaring error I can see is that an <code>IList</code> is definitely different from an <code>IList&lt;T&gt;</code>. An <code>IList</code> is non-generic (e.g., <code>ArrayList</code>).</p>\n\n<p>So your method signature should be:</p>\n\n<pre><code>public static IList&lt;T&gt; LoadObjectListAll()\n</code></pre>\n'}, {'answer_id': 43137, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': '<p>The IList is an IList&lt;T&gt;, it just got fubared by markdown when she posted it. I tried to format it, but I missed escaping the &lt;T&gt;..Fixing that now.</p>\n'}, {'answer_id': 43141, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 4, 'selected': True, 'text': '<p><code>T</code> is not a type nor a <code>System.Type</code>.  <code>T</code> is a type parameter.  <code>typeof(T)</code> returns the type of <code>T</code>.  The <code>typeof</code> operator does not act on an object, it returns the <code>Type</code> object of a type. <a href="http://msdn.microsoft.com/en-us/library/58918ffs.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/58918ffs.aspx</a></p>\n\n<p>@John is correct in answering your direct question.  But the NHibernate code there is a little off. You shouldn\'t be configuring the <code>ISessionFactory</code> <em>after</em> getting the <code>ISession</code>, for example.</p>\n\n<pre><code>public static T[] LoadObjectListAll()\n{\n    var session = GetNewSession();\n    var criteria = session.CreateCriteria(typeof(T));\n    var results = criteria.List&lt;T&gt;();\n    return results.ToArray();        \n}\n</code></pre>\n'}, {'answer_id': 43145, 'author': 'Erick Sgarbi', 'author_id': 4171, 'author_profile': 'https://Stackoverflow.com/users/4171', 'pm_score': 0, 'selected': False, 'text': '<p>CLI only supports generic arguments for <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" rel="nofollow noreferrer">covariance and contravariance</a> 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 <code>List&lt;string&gt;</code> to a <code>List&lt;object&gt;</code> 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 <a href="http://blogs.msdn.com/kcwalina/archive/2008/04/02/SimulatedCovariance.aspx" rel="nofollow noreferrer">article</a>. So it depends on the runtime type that it is generated by the abstract factory.</p>\n'}, {'answer_id': 43147, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>That reads like a markov chain... Bravo.</p>\n"}, {'answer_id': 43153, 'author': 'Erick Sgarbi', 'author_id': 4171, 'author_profile': 'https://Stackoverflow.com/users/4171', 'pm_score': 1, 'selected': False, 'text': '<p>"The original question was already using an <code>IList&lt;T&gt;</code>. It was removed when someone edited the formatting. Probably a problem with Markdown."</p>\n\n<p>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.</p>\n'}, {'answer_id': 43160, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 1, 'selected': False, 'text': '<p>@Jonathan Holland</p>\n\n<blockquote>\n  <p>T is already a type parameter, you don\'t need to call typeof on it. TypeOf takes a type and returns its type parameter.</p>\n</blockquote>\n\n<p><code>typeof</code> "takes" a type and returns its <code>System.Type</code></p>\n'}, {'answer_id': 43195, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 1, 'selected': False, 'text': '<p>You have too many temporary variables which are confusing</p>\n\n<p>instead of</p>\n\n<pre><code>var returnList = session.CreateCriteria(typeof(T));\nvar list = returnList.List();\nvar castList = list.Cast&lt;typeof(T)&gt;();\nreturn castList;\n</code></pre>\n\n<p>Just do</p>\n\n<pre><code>return session.CreateCriteria(typeof(T)).List().Cast&lt;T&gt;();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/43126', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4140/']