另一个 C# 反射
我有一个 csv 文件,其中每一行都是不同类型的记录。我正在解析 csv 并希望将行(各种类型的记录)存储在各种类型的自定义类中。
在每一行,我需要根据记录类型实例化不同的类。
因此,从其他反射示例中,我有以下代码;
Type type = Type.GetType("myNamespace." + className);
object recordclass = Activator.CreateInstance(type);
所以我有一个名为 recordclass 的正确类型的对象,但如何使用它?
我真正想做的就是访问类的属性并填充行数据,然后添加到容器类。
我想我错过了一些关于反射的运行时性质的事情。请帮我把这些点连起来!
希望一切都有意义!
TIA, 加里
I have a csv file where each row is a different type of record. I am parsing the csv and want to store the rows (varied types of records) in various types of custom classes.
At each row i need to instantiate a different class based on the record type.
So taken from other reflection examples, I have the code below;
Type type = Type.GetType("myNamespace." + className);
object recordclass = Activator.CreateInstance(type);
so i have an object named recordclass of the correct type, but how do I use it?
all I really want to do is access the properties of the class and populate the row data, and then later add to a container class.
I guess im missing something about the runtime nature of reflection. Please help me connect the dots!
Hope that all makes sense!
TIA,
Gary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过您提供的示例,您可以将对象转换为您需要的实际类型:
或者考虑使用工厂返回填充的记录对象:
With the example you give you could cast your object to the actual type you need:
Alternatively look into using a Factory to return populated record objects:
如果您想通过反射将值存储到记录类的属性中,请使用此
If you want to store values into your recordclass's property via reflection use this
如果您阅读文档,您将看到,
Type.GetType
需要完整的限定类型名称。If you read the docs, you will see,
Type.GetType
requires a full qualified type name.如果我正确理解问题,您有一个包含记录文本值的文件。每条记录都存储在一行中,每行的开头是一个标识符,表示要构建哪种记录。
可以为此使用反射,但并不是真正必要。使用反射的问题是您需要知道不同记录类型的所有属性才能按名称访问它们。此时,您可能正在使用类型化对象,但如果您使用单个例程来创建所有记录(使用 CreateInstance()),那么您拥有的只是一个非类型化对象。
另一个解决方案是拥有一组例程,每个例程接收一个 IEnumerable(以逗号分隔的输入行,不包括记录 id)并返回一个对象(或一条记录,如果您有基本记录类)使用工厂来选择每行使用哪个例程。
您通过某个 ID 向工厂注册例程(您所做的记录中的第一个字段很好,它可以是记录类名称,但不一定是),并使用第一个字段迭代 CSV 行件从工厂选择方法并建立记录。
希望这个例子能更好地解释一下:?对代码量感到抱歉
示例中的构建器仅返回空记录,但从行片段填充它们应该很容易。另一个版本是仅传递行或一组行(如果一条记录可以覆盖多行)(但如果记录包含不同数量的行,则更复杂)
hth,
艾伦.
If I understand the problem correctly you have a file which contains text values for records. Each record is stored in a single line, and the start of each line is an identifier to say which kind of record is to be built.
It is possible to use reflection for this but not really neccessary. The problem with using reflection is that you need to know all the properties for the different record types in order to access them by name. At this point, you may as well be working with typed objects but if you are using the a single routine to create all the records (using CreateInstance()) all you have is an untyped object.
Another solution is to have a set of routines each of which take in, say an IEnumerable (the input line split by the comma, excluding the record id) and return an object (or a record, if you have a base record class) and use a factory to select which routine to use for each row.
You register the routines with the factory by some ID (the first field in the record as you are doing is good, it can be the record class name but doesn't have to be) and the iterate through the CSV lines, using the first piece to select the method from the factory and building the record.
Hopefully the example will explain a bit better :? Sorry about the volume of code
The builders in the example just return empty records but populating them from the row pieces should be easy. Another version is to just pass in the row, or a set of rows if a record can cover a number of rows (a but more complicated if the records take in different numbers of rows)
hth,
Alan.