C#。遍历所有struct成员 [英] C#. Loop through all struct members

查看:519
本文介绍了C#。遍历所有struct成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我刚刚碰到一个问题,我希望它很简单:)



我的结构包含各种数据类型:

  string ,Nullable< datetime>,可空<双>和Nullable< int64> 



完全有102个变量。

我需要的是做一个for或foreach看看那个结构并检查价值。



这里我要避免的是像这样单独引用每个var:



< pre lang =c#> MyStruct str = new MyStruct();
str.var0 = xxxx;
str.var1 = xxxx;
...
...
str.var101 = xxxx;





如果你们至少可以告诉我的话,我将不胜感激。不需要详细的例子,因为我更愿意自学:)



问题的更新





我这样做的原因如下:

我有一个数据库,我需要从数据库中获取tada将其存储在应用程序中。每个结构代表来自DB的单行数据(我正在使用

 OleDBDataReader 

)。然后 - 我将所有内容放入

 List< MyStruct> 

我需要LINQ查询。

最终结果将是另一个List< mystruct_2> ;



谢谢!

解决方案

这非常讨厌。你可以做到这一点,但作为一般规则并不是一个好主意,除非结构的实际构成对你来说是设计时间,但实例是在运行时。

你需要玩反思这样做:

 FieldInfo [] fi =  typeof (MyStruct).GetFields(BindingFlags。 Public | BindingFlags.Instance); 
foreach (FieldInfo info in fi)
{
Console。的WriteLine(info.Name);
}



这将为您提供结构中字段的名称,您可以使用它们(再次使用Reflection)为您提供实际的例子。但这真的不是一个好主意,除非你真的,真的必须 - 作为一个解决方案,以节省你输入它是一个大锤来破解坚果! :笑:


你可以使用反思



 使用 System.Reflection; 
// ...
// ...
// ...


public void Foo( ref object structObject)
{
FieldInfo []成员= structObject.GetType()。GetFields();

object tempValueToAssign = 测试字符串;

foreach (FieldInfo fi 成员)
{
// 执行FieldInfo fi的更新
fi.SetValue(structObject,tempValueToAssign);
}
}





干杯,

Edo


您可以使用反射。例如,请参阅此 Stack Overflow 问题:迭代结构成员 [ ^ ]。

Hello Everyone,

I just bumped to an issue and I hope it's an easy one :)

I have a structure that contains various data types:

string, Nullable<datetime>, Nullable<double> and Nullable<int64>


Totally there are 102 variables.
What I need is to do a for or foreach look in that structure and check the values.

The thing I am trying to avoid here is to reference each var separatelly like this:

MyStruct str = new MyStruct();
str.var0 = "xxxx";
str.var1 = "xxxx";
...
...
str.var101 = "xxxx";



I would appreciate if you guys could at least show me the way. Don't need the detailed example as I would prefer to learn myself :)

UPDATE OF THE QUESTION



The reason I am doing this is the following:
I have a DB and I need to take the tada from the DB and store it in the application. Each structure represents a single line of data from the DB (I am using

OleDBDataReader

for that). Then - I am putting everything into

List<MyStruct>

which I need for LINQ queries.
The final result would be another List<mystruct_2>

Thanks!

解决方案

That's pretty nasty. You can do it, but it's not a good idea as a general rule unless the actual makeup of the structure is unavailable to you are design time, but the instance is at run time.
You need to play with Reflection to do this:

FieldInfo[] fi = typeof(MyStruct).GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo info in fi)
{
    Console.WriteLine(info.Name);
}


That will give you the names of the fields within the structure, and you can use those (again with Reflection) to give you the values of the actual instance. But it really isn't a good idea unless you really, really have to - as a solution to save you typing it's a sledgehammer to crack a nut! :laugh:


You can use Reflection!

using System.Reflection;
//...
//...
//...


public void Foo (ref object structObject)
{
  FieldInfo[] members = structObject.GetType().GetFields();

  object tempValueToAssign = "A test string";

  foreach (FieldInfo fi in members)
  {
     // perform update of FieldInfo fi
     fi.SetValue(structObject, tempValueToAssign); 
  }
}



Cheers,
Edo


You may use reflection. See, for instance, this Stack Overflow question: "Iterating through Struct members"[^].


这篇关于C#。遍历所有struct成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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