c#如何通过索引访问类实例的字段 [英] c# how to access a field of a class instance by index

查看:79
本文介绍了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个用于控制应用程序。

解决方案

  var  classInstance = File.ReadLines(csvfilePath)
.Select(line = > line.Split(' ,'))
.Select(fields = < span class =code-keyword>> new MyClass {Field0 = fields [ 0 ],Field1 = fields [ 1 ]})
.ToList();



then你可以从classInstance [0],classInstance [1]等位置访问你的实例。


更新:

没有LINQ

<前lang =cs> class 程序
{
静态 void Main( string [] args)
{
List< MyClass> classInstance = new List< MyClass>();
var lines = File.ReadLines( CsvFile.csv);
foreach string line in 行)
{
var fields = line.Split(' );
classInstance.Add( new MyClass(){Field0 = fields [ 0 ],Field1 =字段[ 1 ]});
}
}
}
public class MyClass
{
public string Field0 {获得; set ; }
public string Field1 { get ; set ; }
// 定义所有其他字段
}


 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.IO;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 APAR_csvTryout
{
class Program
{
静态 void Main( string [] args)
{

List< myclass> classInstance = new List< myclass>();
var lines = File.ReadLines( @ .. \..\AcctSummaryInCSV.csv);
foreach string line in 行)
{
var fields = line.Split(' );
// var count = postions.Count;
var count = fields.Count();

string [] postions = new string [ 20 ];

for int i = 0 ; i < count; i ++)
{
postions [i] = fields [i];
}

for int i = count; i < span class =code-keyword><
20 ; i ++)
{
postions [i] = ;
}


classInstance.Add( new MyClass()
{
Field0 = postions [ 0 ],
Field1 = postions [ 1 ],
Field2 =位置[ 2 ],
Field3 =位置[ 3 ],
Field4 =位置[ 4 ],
Field5 =位置[ 5 ],
Field6 =位置[ 6 ],
Field7 =位置[ 7 ],
Field8 =位置[ 8 ],
Field9 =位置[ 9 ],
Field10 =位置[ 10 ],
Field11 =位置[ 11 ],
Field12 =位置[ 12 ],
Field13 =位置[ 13 ],
Field14 = postions [ 14 ],
Field15 = postions [ 15 ],
Field16 = postions [ 16 ],
Field17 = postions [ 17 ],
Field18 =位置[ 18 ],
Field19 =位置[ 19 ]
} );
}
}
}
public class MyClass
{
public string Field0 {获得; set ; }
public string Field1 { get ; set ; }
public string Field2 { get ; set ; }
public string Field3 { get ; set ; }
public string Field4 { get ; set ; }
public string Field5 { get ; set ; }
public string Field6 { get ; set ; }
public string Field7 { get ; set ; }
public string Field8 { get ; set ; }
public string Field9 { get ; set ; }
public string Field10 { get ; set ; }
public string Field11 { get ; set ; }
public string Field12 { get ; set ; }
public string Field13 { get ; set ; }
public string Field14 { get ; set ; }
public string Field15 { get ; set ; }
public string Field16 { get ; set ; }
public string Field17 { get ; set ; }
public string Field18 { get ; set ; }
public string Field19 { get ; set ; }
}
} < / myclass > < / myclass >


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.

解决方案

var classInstance = File.ReadLines(csvfilePath)
               .Select(line => line.Split(','))
               .Select(fields=> new MyClass { Field0 = fields[0], Field1 = fields[1] })
               .ToList();


then you can access your instances from position like classInstance[0], classInstance[1]

UPDATE:
without LINQ

class Program
{
    static void Main(string[] args)
    {
        List<MyClass> classInstance = new List<MyClass>();
        var lines = File.ReadLines("CsvFile.csv");
        foreach (string line in lines)
        {
            var fields = line.Split(',');
            classInstance.Add(new MyClass() { Field0 = fields[0], Field1 = fields[1] });
        }
    }
}
public class MyClass
{
    public string Field0 { get; set; }
    public string Field1 { get; set; }
    // define all other fields
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace APAR_csvTryout
{
    class Program
    {
        static void Main(string[] args)
        {

            List<myclass> classInstance = new List<myclass>();
            var lines = File.ReadLines(@"..\..\AcctSummaryInCSV.csv");
            foreach (string line in lines)
            {
                var fields = line.Split(',');
                //var count = postions.Count;
                var count = fields.Count();

                string[] postions = new string[20];

                for (int i = 0; i < count; i++)
                {
                    postions[i] = fields[i];
                }
                
                for (int i = count; i < 20; i++)
                {
                    postions[i] = "";
                }


                    classInstance.Add(new MyClass()
                    {
                        Field0 = postions[0],
                        Field1 = postions[1],
                        Field2 = postions[2],
                        Field3 = postions[3],
                        Field4 = postions[4],
                        Field5 = postions[5],
                        Field6 = postions[6],
                        Field7 = postions[7],
                        Field8 = postions[8],
                        Field9 = postions[9],
                        Field10 = postions[10],
                        Field11 = postions[11],
                        Field12 = postions[12],
                        Field13 = postions[13],
                        Field14 = postions[14],
                        Field15 = postions[15],
                        Field16 = postions[16],
                        Field17 = postions[17],
                        Field18 = postions[18],
                        Field19 = postions[19]                        
                    });
            }
        }
    }
    public class MyClass
    {
        public string Field0 { get; set; }
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public string Field5 { get; set; }
        public string Field6 { get; set; }
        public string Field7 { get; set; }
        public string Field8 { get; set; }
        public string Field9 { get; set; }
        public string Field10 { get; set; }
        public string Field11 { get; set; }
        public string Field12 { get; set; }
        public string Field13 { get; set; }
        public string Field14 { get; set; }
        public string Field15 { get; set; }
        public string Field16 { get; set; }
        public string Field17 { get; set; }
        public string Field18 { get; set; }
        public string Field19 { get; set; }
    }
}</myclass></myclass>


这篇关于c#如何通过索引访问类实例的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆