C# Generics and Casting Issue

By : Jon
Source: Stackoverflow.com
Question!

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.

This is my current method:

string values;

if (colFilter.Value is IList<int>)
{
    values = BuildClause((IList<int>)colFilter.Value, prefix);
}
else if (colFilter.Value is IList<string>)
{
    values = BuildClause((IList<string>)colFilter.Value, prefix);
}
else if (colFilter.Value is IList<DateTime>)
{
    values = BuildClause((IList<DateTime>)colFilter.Value, prefix);
}
else if (...) //etc.

What I want to do is this:

values = BuildClause((IList<colFilter.ColumnType>)colFilter.Value, prefix);

or

values = BuildClause((IList<typeof(colFilter.ColumnType)>)colFilter.Value, prefix);

or

values = BuildClause((IList<colFilter.ColumnType.GetType()>)colFilter.Value, prefix);

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?)

In my example, colFilter.ColumnType is int, string, datetime, etc. I am not sure why this does not work.

Any ideas?

EDIT: This is C#2.0

EDIT #2

Here is the BuildClause method (I have overloads for each type):

private static string BuildClause(IList<int> inClause, string strPrefix)
{
    return BuildClause(inClause, strPrefix, false);
}

private static string BuildClause(IList<String> inClause, string strPrefix)
{
    return BuildClause(inClause, strPrefix, true);
}

private static string BuildClause(IList<DateTime> inClause, string strPrefix)
{
    return BuildClause(inClause, strPrefix, true);
}
//.. etc for all types

private static string BuildClause<T>(IList<T> inClause, string strPrefix, bool addSingleQuotes)
    {
        StringBuilder sb = new StringBuilder();

        //Check to make sure inclause has objects
        if (inClause.Count > 0)
        {
            sb.Append(strPrefix);
            sb.Append(" IN(");

            for (int i = 0; i < 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();
    }
By : Jon


Answers

The type for the IList must be known at compile time. Depending on what you want to do, you might be able to cast the list to an IList or IEnumerable (without generics) and then iterate over the objects

By : chris166


I don't understand the question. It works for me. It could be as simple as droping the cast? What am I missing?

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1 {

  class Foo


What does the function BuildClause() look like.

It seems to me that you can create BuildClause() as an extension method on IList, and you can append the values together. I assume that you just want to call .ToString() method on different types.

By : J.W.


This video can help you solving your question :)
By: admin