简体   繁体   中英

Getting base type in reflection for datatable

I am working on dynamically creating datatables from my classes I have. Basically, I am using FieldInfo and Type, and GetFields to dynamically create the datatables. The problem is my classes use System.Nullable which datatables cannot handle. I am trying to get the base type, and then later set the column to Nullable = true

EDIT: To be honest I have not tried many things yet. I am not very good with types at the moment. My main goal is I want to get the property types of whatever object I throw to my method and create a datatable with columns names the same as my properties with the same type. So the spot where my code fails is when its gets to myFieldInfo[i].FieldType. This does not actually seem to return the type, but instead returns a string describing the type. I could manually create types by analyzing the string, but I feel that is over the top for what I need to do.

EDIT: So ignore my code I included. MY GOAL: I want to pass an object to my method, have it dynamically get the properties, then create a datatable with columns being named the same as the properties and having the same type (or whatever type is equivalent for datatable). It does not necessarily need to use FieldInfo, that was just my starting spot.

Here is a copy of my basic code.

        DataTable peopleTable = new DataTable();
        pPeople myPerson = new pPeople();

        FieldInfo[] myFieldInfo;
        Type myType = typeof(pPeople);
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);


        for (int i = 0; i < myFieldInfo.Length; i++)
        {
            peopleTable.Columns.Add(myFieldInfo[i].Name, myFieldInfo[i].FieldType);
            peopleTable.Columns[i].AllowDBNull = true;
        }

EDIT: This is what my final code looks like to create the datatable. Thanks everyone for the help;

        DataTable peopleTable = new DataTable();
        pPeople myPerson = new pPeople();

        FieldInfo[] myFieldInfo;
        Type myType = typeof(pPeople);
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);



        for (int i = 0; i < myFieldInfo.Length; i++)
        {
            if (Nullable.GetUnderlyingType(myFieldInfo[i].FieldType) == null)
            {
                peopleTable.Columns.Add(myFieldInfo[i].Name, myFieldInfo[i].FieldType);
            }
            else
            {
                peopleTable.Columns.Add(myFieldInfo[i].Name, Nullable.GetUnderlyingType(myFieldInfo[i].FieldType));
                peopleTable.Columns[i].AllowDBNull = true;
            }
        }

As I understand, you want to find out non-nullable equivalent of a given nullable type ("int" out of "int?", for example). The latter is in fact

Nullable<int>

so you need to get a generic argument. Try this:

typeof(int?).GetGenericArguments()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM