C#选择运行时已知类型的行 [英] C# select row of runtime known type
问题描述
我想导入一个包含五列的* .csv文件。
例如:
ID; Number1; SomeText; SomethingElse
使用Reflection.Emit我在运行时创建类并返回类型。
用文件中的信息填充它。 br />
现在我想查询对象,但linq方法已经可用,无法将其转换为列表< object> ;.
如何通过ID访问对象中的一行?有谁知道我如何创建一个
表达式树或其他东西来实现它?关于类类型没有任何信息
在编译时有字段。
我尝试过:
CreateType包含在运行时生成类的代码而不是必需的
var DynamicObject = CreateType(FirstPath);
输入NewDynList = typeof (List<>);
类型NewDynObj = DynamicObject.GetType();
类型NewGenObj = NewDynList.MakeGenericType(NewDynObj);
var NewDynGenList = Activator.CreateInstance(NewGenObj);
NewDynGenList = ImportCsv(NewDynGenList,PathforCSV,NewDynObj);
在该代码之后我得到一个对象包括列表< dynamicobject> ;这是在运行时创建的,但参数isGenericType未设置为true。所以我无法创建一个List< object>
其中包括Linq方法。
private object ImportCsv( object DynamicObjectList, string 路径, object DynamicObject)
{
类型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;
}
私有 T SetValue< T>( string value , string field,T source)
{
source.GetType()。 GetProperty(field).SetValue(source, value , null );
返回来源;
}
使用这2个方法i使用来自Csv的数据填充对象。 Field by Field逐行...
我的目标是比较具有相同结构的2 * .csv文件。
信息arnt排序,行数可能有差异...
所以我必须选择ID并比较列,如果其他文件包含它。
我花了很多时间搜索,但我发现没有什么对我有用,或者我正在寻找错误的关键字。
是的,您需要为此动态构建表达式树。但是已经有一些解决方案你可以使用:
动态地构建Lambda表达式 [ ^ ]
Dynamic Expresso [ ^ ]
< a href =http://www.codeproject.com/Articles/28580/LINQ-and-Dynamic-Predicate-Construction-at-Runtime>运行时LINQ和动态谓词构建 [ ^ ]
构建条款动态地在Linq [ ^ ]
GitHub - kahanu / System.Linq.Dynamic:这是用于.Net 4.0动态语言功能的Microsoft程序集。 [ ^ ]
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.
Yes, you need to dynamically build an expression tree for that. But there already are some solutions for this which you could use:
Build Lambda Expressions Dynamically[^]
Dynamic Expresso[^]
LINQ and Dynamic Predicate Construction at Runtime[^]
Build Where Clause Dynamically in Linq[^]
GitHub - kahanu/System.Linq.Dynamic: This is the Microsoft assembly for the .Net 4.0 Dynamic language functionality.[^]
这篇关于C#选择运行时已知类型的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!