Search code examples
c#reflectiontypechecking

How can I use reflection with varying classes?


What I'm trying to achieve here is that I want to insert data into a database from file using a CRUD class with type . I'm gonna demonstrate a simple example of the situation.

public class SubjectTester<T>where T : class
    {
        public void Create()
        {
            var db_context = new DbContext();
            //here I get file content and do stuff with it
            //here I want create an instance of T and add the data from the file
            //here I will use the Add method from my DbContext and save the changes
        }
    }
    public class DbContext 
    {
        //has some stuff
    }
    public class TestSubjectA
    {
        //some properties
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class TestSubjectB
    {
        //some properties
        public double Sum { get; set; }
    }
    public class TestSubjectC
    {
        //some properties
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsRigged { get; set; }
    }

As you can see the problem is that some tables have more columns than others and also the data types are different aswell. I'm doing this because I want to dynamically add data depending on the class that I get. Also I don't want to have a bunch of if elses to check what is the type of T... I hope I was clear in my explanation.


Solution

  • First I'm going to give a couple of usefull links:

    Microsoft reflection

    All methods and properties of reflection class

    The changed example:

    public class SubjectTester<T>where T : class
        {
            public void Create()
            {
                var db_context = new DbContext();
                var instance = Activator.CreateInstance<T>();
                foreach(var property in typeof(T).GetProperties())
                {
                    if (property.PropertyType.Name == typeof(String).Name)
                    {
                        property.SetValue(instance, "Text");
                        Console.WriteLine(property.GetValue(instance));
                    }
                    
                }
               //save chages to context here
            }
        }
        public class DbContext 
        {
            //has some stuff
        }
        public class TestSubjectA
        {
            //some properties
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class TestSubjectB
        {
            //some properties
            public string Name { get; set; }
        }
        public class TestSubjectC
        {
            //some properties
            public int Id { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
        }
    

    The changes in the code are the following:

    1. create an instance of that class
    2. get all properties of the class and run them through foreach loop
    3. You need to do some type checking however you want to do it the shown here is an example and wont work for other classes with different property types and number of properties
    4. after doing the type checking assign the values to the properties

    That's all I was able to get together for the time being. I hope I explained it well enough.

    Sorry if it is not descriptive enough and if I didn't explain it well!