I want to import a *.csv File with five columns.
for example:
ID;Number1;SomeText;SomethingElse
With Reflection.Emit i create at runtime the class and get the Type back.
Fill it with the informations from the file.
Now i want to query the object but the linq methods arnt available and its not possible to convert it into a list<object>.
how i can access a row in the object over ID? does anyone know how i create a
expression tree or something else to acomplish that? There are NO informations
about the class type with there fields at compiletime.
What I have tried:
CreateType includes the code for generating the class at runtime not necassary
var DynamicObject= CreateType(FirstPath);
Type NewDynList = typeof(List<>);
Type NewDynObj = DynamicObject.GetType();
Type NewGenObj = NewDynList.MakeGenericType(NewDynObj);
var NewDynGenList = Activator.CreateInstance(NewGenObj);
NewDynGenList = ImportCsv(NewDynGenList , PathforCSV, NewDynObj );
after that code i get an object includig a list<dynamicobject> which is created at runtime, but the parameter "isGenericType" is NOT SET to true. so im not able to
create a List<object> which would include the Linq Methods.
private object ImportCsv(object DynamicObjectList, string Path, object DynamicObject)
{
Type NewDynObj = DynamicObject.GetType();
StreamReader readTemp = new StreamReader(Path);
string line_ = readTemp.ReadLine();
List<PropertyInfo> properties = DynamicObject.GetType().GetProperties().ToList();
while (readTemp.Peek() > 1)
{
line_ = readTemp.ReadLine();
string[] line_Values = line_.Split(';');
if (properties.Count() != 0)
{
object NewDynOBjrow = Activator.CreateInstance(NewDynObj);
for (int i = 0; i < line_Values.Length; i++)
{
NewDynOBjrow = SetValue(line_Values[i], properties[i].Name, NewDynOBjrow);
}
DynamicObjectList.GetType().GetMethod("Add").Invoke(DynamicObjectList, new[] { NewDynOBjrow });
}
}
readTemp.Close();
return DynamicObjectList;
}
private T SetValue<T>(string value, string field, T source)
{
source.GetType().GetProperty(field).SetValue(source, value, null);
return source;
}
With that 2 Methods i Fill the object with data from Csv. Field by Field row by row...
my target is to compare 2 *.csv Files with the same structure.
the informations arnt sorted and there could be differences in the count of rows...
so i have to select over "ID" and compare the columns, if the other file includes it.
I spent a lot of time searching, but i have found nothing that worked for me, or i was looking for with the wrong keywords.