How to access members of an struct like an array in C#?

前端 未结 5 1283
余生分开走
余生分开走 2021-01-26 16:16

Lets say I have a struct with more than hundred elements with complex names. And I am passing a struct of the struct type described to a function using ref, like this:



        
5条回答
  •  误落风尘
    2021-01-26 17:13

    You can do this in .NET BUT as several others have already posted: DO NOT DO IT.

    Some Code

    a.GetType().GetProperties() [0].SetValue (a, newvalue, null);
    

    EDIT: several reasons not to do this:

    • the order is not guaranteed !

    • what happens when there are no properties ?

    • what happens with readonly properties ?

    So again: DO NOT DO THIS!

提交回复
热议问题