Click here to Skip to main content
15,888,315 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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

C#
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.

C#
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.
Posted
Updated 7-Mar-16 6:15am
Comments
Maciej Los 7-Mar-16 12:49pm    
Do not repost!!!
mortiii 8-Mar-16 1:29am    
no repost... there are 2 questions ...
- how to set the property "isGenericType" set true, creating a class at runtime
- is there a another way to select a row from the object
- in my other question there isnt a solution, only links with expression trees, where the class is known at compiletime ...

i found the links by my self, but im not able to create a expression tree with runtime known types ...
i would be grateful for a solution that would answer my question. i have now tried often, but have failed. thats the reason why i´m asking.

thanks

1 solution

 
Share this answer
 
Comments
Maciej Los 7-Mar-16 12:49pm    
5ed!
Sascha Lefèvre 7-Mar-16 13:38pm    
Thank you, Maciej :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900