Access private fields

前端 未结 4 1690
无人及你
无人及你 2020-12-06 10:07

Is it possible to get or set private fields?

I want to get System.Guid.c. Is there a way to access it or should I just copy the code from the strut and

4条回答
  •  抹茶落季
    2020-12-06 10:31

    While it's possible to do this with reflection, it may be easier to simply retrieve c from System.Guid.ToByteArray().

    byte[] guid = guid.ToByteArray();
    short c = (short)((guid[7] << 8) | guid[6]);
    

    Since this approach uses public and documented methods, it is less subject to change between versions. (In general, relying on private implementation details should be avoided, since these details can change from version to version.)

提交回复
热议问题