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 should access table property and generic <T> I want to set all TID to T_Table ,C_Table, Q_Table

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; }
    }
}

Open in new window

generic reflection
programmeristAsked:
Who is Participating?
 
TheAvengerCommented:
Can you debug the application and stop at the line that fails and check what is null? I suppose it will be the result of GetProperty(), because your properties are internal. You should make them public or call an overload of the GetProperty method which accepts some BindingFlags to get nonpublic properties. For more information you should check http://msdn.microsoft.com/en-us/library/zy0d4103.aspx.
0
 
programmeristAuthor Commented:
ARE YOU THERE?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.