如何从键入的数据集中获取基础类型 [英] How to get underlying type from typed DataSet

查看:93
本文介绍了如何从键入的数据集中获取基础类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索很多东西,但没有发现有关此问题的任何信息.我正在记录我的应用程序,并且正在打印变量的类型及其值.我想对作为参数接收的每个对象以及返回的每个对象执行相同的操作.所以我要返回一个类型化的数据集(例如,定义为MyDataSetType的MyDataSet),但是我无法检索MyDataSetType的名称.

I've been searching a lot but I haven't found anything about this question. I'm making a log of my app and I'm printing types of variables and their values. I want to do the same for every object I receive as parameter, and for every object I return too. So I'm returning a typed dataset (MyDataSet that's defined as MyDataSetType e.g.) but I can't retrieve MyDataSetType name.

我有一个给定数据集的方法,该方法返回一个包含所有内容的字符串.像这样:

I have a method that given a dataset, returns a string with all the content. Something like this:

    string GetLogStringFromDataSetParameter(System.Data.DataSet incomingDataSet)
    {
        StringBuilder strReturn = new StringBuilder();
        strReturn.Append("DataSet (type ");
        strReturn.Append(GetTypeName(incomingDataSet));
        strReturn.Append("). ");
        // .. do some validations
        strReturn.Append("Contains ");
        strReturn.Append(incomingDataSet.Tables.Count);
        strReturn.Append(" tables.");
        for (int i = 0; i < incomingDataSet.Tables.Count; i++)
        {
            System.Data.DataTable table = incomingDataSet.Tables[i];
            strReturn.Append(" Tabla " + table.TableName + " (" + i + ") ");
            strReturn.Append(<Method to list table content>);
        }//yes, this could have been a foreach loop...
        return FormatStringToLog(strReturn);
     }  //end

如您所见,我正在使用自己的方法GetTypeName来检索类型化数据集的名称.通过该网站进行调查后,我已经采用了这种方法:

As you can see, I'm using my own method GetTypeName to retrieve de name of my typed dataset. I've made this method after some investigation through this site:

public static string GetTypeName<T>(T parameter)
{
    string strReturn = typeof(T).Name;
    if (strReturn.IndexOf("Nullable") >= 0)
        strReturn = Nullable.GetUnderlyingType(typeof(T)).Name;
    else if (strReturn.IndexOf("List") >= 0)
    {
        strReturn = "List of " + typeof(T).GetGenericArguments()[0].Name;
        if (strReturn.IndexOf("Nullable") >= 0)
            strReturn = "List of " +  Nullable.GetUnderlyingType(typeof(T).GetGenericArguments()[0]).Name;
    }
    return strReturn;
}

当我在GetLogStringFromDataSetParameter方法中时,如果尝试typeof(MyDataSet),它将正确返回MyDataSetType.但是,当我调用GetTypeName时,它仅返回DataSet(泛型类型).为什么是这样?有什么方法可以正确检索MyDataSetType而不直接调用typeof()吗?

When I'm inside of GetLogStringFromDataSetParameter method, if I try typeof(MyDataSet) it returns correctly MyDataSetType. But when I make the call to GetTypeName it returns DataSet only, the generic type. Why is this? Is there any way to retrieve correctly MyDataSetType without calling directly to typeof()?

我希望我已经解释得足够清楚了.预先感谢.

I hope I've explained everything clear enough. Thanks in advance.

推荐答案

这是因为typeof(T)与传入的数据集类型无关.

That is because typeof(T) has nothing to do with the incoming Dataset type.

在编译时,该方法针对普通的Datset类型实例化,而T为Dataset类型.

At compile-time the method is instantiated for the plain Datset type, and T is of type Dataset.

要解决此问题,只需使用parameter.GetType()而不是typeof(T)

To solve it, simply use parameter.GetType() instead of typeof(T)

这篇关于如何从键入的数据集中获取基础类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆