开发者

how to fill Sql Table with Generic Reflection method?

Hi; I have 4 tables, one of them is main table also there is one to many relation between tables. TID is Foreign key and ID is PK. As a result. i don't want to fill table with classic method. I s开发者_运维百科hould access table property and generic <T> I want to set all TID to T_Table ,C_Table, Q_Table

how to fill Sql Table with Generic Reflection method?

MY CODES(this is test project not real project but logis is the same as real project) Below codes return to me ERROR( in first foreach loop): Null reference exception; Object reference not set to an instance of an object.


    using System.Reflection;

    namespace App.ReflectionToGeneric
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] PropertyNames = new string[] { "TID", "AnyID" };
                int[] Vals = new int[] { 1, 2 };
                new DataManager().Save<QTable>(PropertyNames, Vals);
            }
        }


        public class DataManager
        {
            IEnumerable<Table> list = new GetData().GetVals();
            public void Save<TModel>( string[] PropertyNames, int[] Vals ) where TModel : class, new()
            {

                var instance = new TModel();
                Type calcType = instance.GetType();
               // object calcInstance = Activator.CreateInstance(calcType);
                foreach (string PropertyName in PropertyNames)
                {
                    // ERROR RETURN TO ME BELOW   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    calcType.GetProperty(PropertyName).SetValue(instance, Vals[0], null);
                }
                foreach (string PropertyName in PropertyNames)
                {
                    Console.WriteLine(calcType.GetProperty(PropertyName).GetValue(instance, null).ToString());
                }

            }
       }

        public class GetData
        {
            public IEnumerable<Table> GetVals()
            {
                List<Table> list = new List<Table>();
                list.Add(new Table() { ID = 1, Name = "yusuf" });
                list.Add(new Table() { ID = 2, Name = "berkay" });
                return list;
            }
        }

        public class Table
        {
            internal int ID { get; set; }
            internal string Name { get; set; }
        }

        public class CTable
        {
            internal int ID { get; set; }
            internal int TID { get; set; }
            internal int AnyID { get; set; }
        }

        public class QTable
        {
            internal int ID { get; set; }
            internal int TID { get; set; }
            internal int AnyID { get; set; }
        }

        public class TTable
        {
            internal int ID { get; set; }
            internal int TID { get; set; }
            internal int AnyID { get; set; }
        }
    }


It looks like the problem is simply that the properties are non-public, so GetProperty("AnyID") etc will return null. To fetch non-public properties, you need binding flags:

calcType.GetProperty(PropertyName, BindingFlags.Instance|BindingFlags.NonPublic)

You might also want to loop at something like dapper which will do the binding for you, and is much faster (it pre-generates IL via the emit API, rather than per-item/per-member reflection).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜