简体   繁体   English

无需反射即可遍历C#中对象的所有字段

[英]Iterate through all fields of an object in C# without Reflection

I have a code that looks like this 我有一个看起来像这样的代码

if( evento.ObjetosUnity != null ) lista_elementos.AddRange( evento.ObjetosUnity.Cast<Elemento3DJS>() );
if( evento.Imagenes3D != null ) lista_elementos.AddRange( evento.Imagenes3D.Cast<Elemento3DJS>() );
if( evento.Botones != null ) lista_elementos.AddRange( evento.Botones.Cast<Elemento3DJS>() );
...and so on...

where I have to go through all the fields of an object. 我必须遍历对象的所有字段。 Is there an easier way of doing this? 有更简单的方法吗?

Edit: I am compiling an App to iOS and CANT use Reflection. 编辑:我正在将一个应用程序编译为iOS,并且不能使用Reflection。

The short answer is No . 简短的答案是“ 否”

Without reflection, you will have to spell it out. 没有反思,您将不得不阐明。 Want to use all fields? 是否要使用所有字段? Write the code for it. 为此编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM