在 class 而不是 class 的集合上使用 foreach
Use foreach on a class not a collection of a class
所以我有一个带有字符串、浮点数、日期时间和数据表的 class
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;
}
但我无法找到搜索 class 的各个字段的任何帮助,只是在 class 类型的列表上。
我遇到了错误
foreach 语句不能对 'ASM001.Data' 类型的变量进行操作,因为 'ASM001.Data' 不包含 'GetEnumerator'
的 public 定义
我想知道这是否可行,或者我是否需要硬编码按名称检查每个字符串字段并返回 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(以下示例应该构建和运行):
The following example retrieves the fields of MyClass and displays the
field values.
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();
所以我有一个带有字符串、浮点数、日期时间和数据表的 class
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;
}
但我无法找到搜索 class 的各个字段的任何帮助,只是在 class 类型的列表上。
我遇到了错误 foreach 语句不能对 'ASM001.Data' 类型的变量进行操作,因为 'ASM001.Data' 不包含 'GetEnumerator'
的 public 定义我想知道这是否可行,或者我是否需要硬编码按名称检查每个字符串字段并返回 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(以下示例应该构建和运行):
The following example retrieves the fields of MyClass and displays the field values.
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();