I want to try to not write all methods and classes with a similar code with SQL statements and follow the DRY principle. And now I need to get access to properties of a not known class.
public T Get(string pathToClass, string sqlStatement)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionString; //connectionString is field of class.
connection.Open();
SqlCommand command = new SqlCommand(sqlStatement);
SqlDataReader reader = command.ExecuteReader();
List<Type> types = new Reflect(pathToClass).GetTypes(); //Reflection is my class library
List<string> names = new Reflect(pathToClass).GetNames(); //it only reads the classes and returns properties's names and types.
object entity = new object(); //idk is it right or not, but seems it is.
for(int i = 0; reader.Read(); i++)
{
entity.SomeProperty = types[i].Parse(reader[names[i]].ToString()); //There is a wrong point, where I'm confused
}
connection.Close();
return (T)entity;
}