0

こんにちは私は次の方法を持っています。

public static DataTable ConvertToDataTable<T>(List<T> lstData)
{
    PropertyDescriptorCollection objPropertiesCollection = TypeDescriptor.GetProperties(typeof(T));
    DataTable dtResult = new DataTable();

    foreach (PropertyDescriptor objProperty in objPropertiesCollection)
    {
        dtResult.Columns.Add(objProperty.Name, Nullable.GetUnderlyingType(objProperty.PropertyType) ?? objProperty.PropertyType);
    }

    foreach (T item in lstData)
    {
        DataRow dr = dtResult.NewRow();

        foreach (PropertyDescriptor objProperty in objPropertiesCollection)
        {
            dr[objProperty.Name] = objProperty.GetValue(item) ?? DBNull.Value;
        }

        dtResult.Rows.Add(dr);
    }

    return dtResult;
}

そして私は次のクラスを持っています

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ClientesPagos.Clases
{
    [Serializable]
    public class Servicio
    {
        public int IdServicio;
        public string Nombre;
        public string Descripcion;
        public string Imagen;
        public decimal Precio;
        public int Cantidad;
        public decimal Total;

        public Servicio()
        {

        }

        public Servicio(int id, string nombre, string descripcion, string img, decimal precio, int cantidad)
        {
            IdServicio = id;
            Nombre = nombre;
            Descripcion = descripcion;
            Imagen = img;
            Precio = precio;
            Cantidad = cantidad;
            Total = cantidad * precio;
        }
    }
}

問題はメソッドの最初の行にあります

PropertyDescriptorCollection objPropertiesCollection =   
    TypeDescriptor.GetProperties(typeof(T));

objPropertiesCollection を満たさないため、次の手順は機能しません。

クラスに何か問題がありますか?最初のメソッドで動作していない可能性がありますか?

4

2 に答える 2