本文介绍了c#如何通过索引访问类实例的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想将一个CSV文件导入一个以多个字符串开头的类,并以一些控制字段结束。 如果我使用http:/ /www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp并更改显示以填充一类字符串,有没有办法动态地执行此操作? 我可以像classInstance [i]一样索引每个字段。或者我是否必须单独阅读每个字段并添加位置,并通过向其余字段添加空白条目来验证文件结尾。 读取stringContents; if (stringContents) { classInstance.position1 = stringContents;} else { classInstance.position1 = ; } 读取stringContents; if (stringContents) { classInstance.position2 = stringContents;} else { classInstance.position2 = ; } stringContents是一个bool方法=假如果文件结束等等。 类当前有34个字段,其中前20个用于字符串导入,其余14个用于控制应用程序。解决方案 I want to import a CSV file into a class which begins with a number of strings, and ends with some control fields.If I use http://www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp and change the display to populate a class of strings, is there a way to dynamically do this?Could I index each field like classInstance[i]. Or would I have to read each field individually and add in position, and validate the end of file by adding blank entries to the remaining fields.read stringContents;if (stringContents) {classInstance.position1 = stringContents;}else{classInstance.position1 = "";}read stringContents;if (stringContents){classInstance.position2 = stringContents;}else{classInstance.position2 = "";}stringContents is a bool method = false if end of file etc.The Class current has 34 fields with the first 20 for the string imports and the remaining 14 to control the application. 解决方案 这篇关于c#如何通过索引访问类实例的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 14:15