gpt4 book ai didi

c# - 如何访问在运行时定义类型的对象?

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:37 26 4
gpt4 key购买 nike

我正在使用 FileHelpers 使用以下代码解析结构未知的 CSV 文件:

            string cd = string.Format(@"[DelimitedRecord(""{0}"")]
public sealed class ImportClass {{
[FieldQuoted('{1}')]
public string[] Fields;
}}", _delimiter, _quote);
Type t = DelimitedClassBuilder.ClassFromString(cd);

var engine = new FileHelperAsyncEngine(t);

engine.BeginReadFile(filename);
object record;
while ((record = engine.ReadNext()) != null) {

}

engine.Close();

这似乎工作得很好。当我使用调试器逐步执行它时,记录是一个 ImportClass 类型的对象,并且字段字段已正确填充文件中的数据。

我遇到的问题是如何实际提取该数据?我无法将记录转换为 ImportClass,因为该类型在编译时未知。我真的需要使用反射还是有更简单的方法来做到这一点?

最佳答案

老实说,我能想到的最简单的方法就是使用 IronPython。构造代码字符串并将其传递给 python 引擎。我们用 DLR 和 IronPython 取代了大量的反射。

或者如你所说,你可以反射(reflection)。

编辑:反射(reflect)评论:您只需要将 2 个 iron python 程序集作为引用包括在内。这并不难。诚实地。这与安装船载其他东西无关。

关于c# - 如何访问在运行时定义类型的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2242332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com