We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

how to fill Sql Table with Generic Reflection method?

programmerist
on
Medium Priority
250 Views
Last Modified: 2012-06-21
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
Comment
Watch Question

Author

Commented:
ARE YOU THERE?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.