lundi 18 mai 2020

Get fields from DataContext class, but exclude related tables

I'm trying to iterate through a class T, which may or may not be a LINQ DataContext class. It works for normal classes, but when passing in a database class from a Linq data context, it also iterates over the related tables with their related properties (like a deep copy).

I'm trying to just get all the "primitive" fields if it's a database class. I.e. it should return all the fields in the table. I've tried to filter on IsPrimitive(), but it does not return all the fields (it might for example exclude nullable int32's amongst others). I've also tried using things like BindingFlags.Public and/or BindingFlags.Instance but to no avail.

public static T MyClone<T>(T original)
{
    T newObject = (T)Activator.CreateInstance(original.GetType());

    foreach (var originalProp in original.GetType().GetProperties())
    {
        //Do something
    }




Aucun commentaire:

Enregistrer un commentaire