日期:2014-05-18  浏览次数:20805 次

如何判断是datatable还是list
我要为数据控件绑定数据,传入数据可能是datatable或list,所以我传入变量的类型为objet,但是传入后我如何判断是datatable还是list类型??

------解决方案--------------------
C# code

public partial class CSDN_Code_1 : System.Web.UI.Page
{
    public string typeHmtl = string.Empty;
    public string typeHmtl_1 = string.Empty;
    object data_type = new object();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Random rd = new Random();
            if (rd.Next(100) % 2 == 0)
            {
                data_type = new DataTable();
            }
            else 
            {
                data_type = new List<string>();
            }
            // 第1种方式判断
            if (data_type is DataTable)
            {
                typeHmtl = "DataTable";
            }
            else {
                typeHmtl = "List<string>";
            }
            // 第二种方式判断
            Type type=data_type.GetType();
            if (type == typeof(DataTable))
            {
                typeHmtl_1 = "DataTable";
            }
            else 
            {
                typeHmtl_1 = "List<string>";
            }

        }
    }
}