簡體   English   中英

在類上使用foreach而不是類的集合

[英]Use foreach on a class not a collection of a class

所以我有一個帶有字符串,浮點數,DateTimes和數據表的此類

public class Data : IEnumerator
{

    string m_PowerSwitch = "Not Tested",
        m_SerialNumber = "Not Tested",
        m_Reset = "Not Tested",
        m_WashPump = "Not Tested",

        m_PortB = "Not Tested",
        m_PortC = "Not Tested",
        m_PortD = "Not Tested",
        m_CurlyTube = "Not Tested",
        m_BypassTube = "Not Tested";

    float m_EC53115VMeasured = 0.0F,
        m_EC53165VMeasured = 0.0F,
        m_EC531624VMeasured = 0.0F,

        m_SolventLineBVolumeMeasured = 0.0F,
        m_SolventLineCVolumeMeasured = 0.0F,
        m_SolventLineDVolumeMeasured = 0.0F,
        m_CurlyTubeVolumeMeasured = 0.0F,
        m_BypassTubeVolumeMeasured = 0.0F;
} 

我想使用一個foreach語句,例如

        foreach (ASM001.ASM asm in P00001.Global.ListofAvailableASMs)
        {  
            if (asm.ASMData.EndTime == null)
                asm.ASMData.EndTime = endTime;

            foreach (object data in asm.ASMData)
            {
                if (data == "Not Tested")
                {
                    asm.ASMData.Result = "FAILED";
                }
                continue;
            }

但是我無法在類的單個列表上搜索類的各個字段找到任何幫助。

我收到錯誤的foreach語句,無法對類型為“ ASM001.Data”的變量進行操作,因為“ ASM001.Data”不包含“ GetEnumerator”的公共定義

我想知道這是否可行,或者我是否將不得不按名稱對每個字符串字段進行硬編碼並返回true或false。

如此一來,您現在要檢查的字符串比我要檢查的要多得多,這就是為什么我想知道是否有一種更快的方法來執行此操作。

使用反射(代碼釋義,這將不會生成)

Data data = ...
Type type = data.GetType();
FieldInfo[] fields = type.GetFields(...);
foreach(FieldInfo field in fields) {

    Console.WriteLine("{0} = {1}", field.Name, field.GetValue( data ) );
}

MSDN (以下示例構建並運行):

下面的示例檢索MyClass的字段並顯示字段值。

using System;
using System.Reflection;

public class MyClass
{
    public string myFieldA;
    public string myFieldB; 
    public MyClass()
    {
        myFieldA = "A public field";
        myFieldB = "Another public field";
    }
}

public class FieldInfo_GetValue
{
    public static void Main()
    {
        MyClass myInstance = new MyClass();
        // Get the type of MyClass.
        Type myType = typeof(MyClass);
        try
        {
            // Get the FieldInfo of MyClass.
            FieldInfo[] myFields = myType.GetFields(BindingFlags.Public 
                | BindingFlags.Instance);
            // Display the values of the fields.
            Console.WriteLine("\nDisplaying the values of the fields of {0}.\n",
                myType);
            for(int i = 0; i < myFields.Length; i++)
            {
                Console.WriteLine("The value of {0} is: {1}",
                    myFields[i].Name, myFields[i].GetValue(myInstance));
            }
        }  
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0}", e.Message);
        }
    }
}

可能的LINQ版本:

Data data = ...
FieldInfo[] fields = (from f in data.GetType().GetFields(BindingFlags.Instance|BindingFlags.NonPublic) where f.Name.StartsWith("m_") select f).ToArray();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM