Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Generics and Casting Issue
    primarykey
    data
    text
    <p>I am having trouble casting an object to a generic IList. I have a group of in statements to try to work around this, but there has to be a better way to do this.</p> <p>This is my current method:</p> <pre><code>string values; if (colFilter.Value is IList&lt;int&gt;) { values = BuildClause((IList&lt;int&gt;)colFilter.Value, prefix); } else if (colFilter.Value is IList&lt;string&gt;) { values = BuildClause((IList&lt;string&gt;)colFilter.Value, prefix); } else if (colFilter.Value is IList&lt;DateTime&gt;) { values = BuildClause((IList&lt;DateTime&gt;)colFilter.Value, prefix); } else if (...) //etc. </code></pre> <p>What I want to do is this: </p> <pre><code>values = BuildClause((IList&lt;colFilter.ColumnType&gt;)colFilter.Value, prefix); </code></pre> <p>or</p> <pre><code>values = BuildClause((IList&lt;typeof(colFilter.ColumnType)&gt;)colFilter.Value, prefix); </code></pre> <p>or</p> <pre><code>values = BuildClause((IList&lt;colFilter.ColumnType.GetType()&gt;)colFilter.Value, prefix); </code></pre> <p>Each of these produces this compiler error: The type or namespace name 'colFilter' could not be found (are you missing a using directive or an assembly reference?)</p> <p>In my example, colFilter.ColumnType is int, string, datetime, etc. I am not sure why this does not work. </p> <p>Any ideas?</p> <p><strong>EDIT: This is C#2.0</strong></p> <p><strong>EDIT #2</strong></p> <p>Here is the BuildClause method (I have overloads for each type):</p> <pre><code>private static string BuildClause(IList&lt;int&gt; inClause, string strPrefix) { return BuildClause(inClause, strPrefix, false); } private static string BuildClause(IList&lt;String&gt; inClause, string strPrefix) { return BuildClause(inClause, strPrefix, true); } private static string BuildClause(IList&lt;DateTime&gt; inClause, string strPrefix) { return BuildClause(inClause, strPrefix, true); } //.. etc for all types private static string BuildClause&lt;T&gt;(IList&lt;T&gt; inClause, string strPrefix, bool addSingleQuotes) { StringBuilder sb = new StringBuilder(); //Check to make sure inclause has objects if (inClause.Count &gt; 0) { sb.Append(strPrefix); sb.Append(" IN("); for (int i = 0; i &lt; inClause.Count; i++) { if (addSingleQuotes) { sb.AppendFormat("'{0}'", inClause[i].ToString().Replace("'", "''")); } else { sb.Append(inClause[i].ToString()); } if (i != inClause.Count - 1) { sb.Append(","); } } sb.Append(") "); } else { throw new Exception("Item count for In() Clause must be greater than 0."); } return sb.ToString(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload