Regarding the access private strucuture members in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jane smith
    New Member
    • Mar 2010
    • 3

    Regarding the access private strucuture members in C#

    Hi,

    I am doing the unit testing in C#, Where in i have to access the private structure members from different assembly (to testing assembly). I am unable to access the private structure members even through reflection. Can any one help me with solution?

    Thanks in advance
    Jane smith
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Have you considered using public accessors in the code? Eg:

    Comment

    • Jane smith
      New Member
      • Mar 2010
      • 3

      #3
      Hi,

      Thanks for your reply. Let me clarify to some more extent. Here i want to access a private structure members using reflection and i dont want to instrument the source code.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        To clarify, are you looking just for the member name and type, or the actual data for the member? If data, then it's intentionally forbidden, hence the private modifier. If member name and type, then assuming it's working for your public members, could you post the code?

        Comment

        • Jane smith
          New Member
          • Mar 2010
          • 3

          #5
          Sure, I am pasting the code below where in the I have to set the value for "ELF_Header->e_shentsize.By te0" which is present in Reader.cs file into my TestReader.cs (test assembly). I am not able to access the structure member even through reflection.

          Thanks in advance
          Jane


          Reader.cs
          namespace Configurator
          {
          unsafe public partial class Reader
          {
          private Elf32_Ehdr* ELF_Header;
          unsafe private String Get_SecName(int NameOffset)
          {

          int Position =
          ELF_Header->e_shentsize.By te0 + ELF_Header->e_shentsize.By te1 * 256;
          Position =
          Position*(ELF_H eader->e_shstrndx.Byt e0 + ELF_Header->e_shstrndx.Byt e1 * 256);
          Position =
          Position + ELF_Header->e_shoff.Byte 0 + ELF_Header->e_shoff.Byte 1 * 256 + ELF_Header->e_shoff.Byte 2 * 65536 + ELF_Header->e_shoff.Byte 3 * 16777216;
          fixed (Byte* pTemp = &data[Position])
          {
          …….
          }
          String name = System.Text.Enc oding.ASCII.Get String(data, Position + NameOffset, 10);
          return name;
          }
          }
          }
          Definition.cs
          namespace Configurator
          {

          //header
          [StructLayout(La youtKind.Sequen tial, Pack = 8)]
          private unsafe struct Elf32_Ehdr
          {
          public unsafe Elf32_Off e_shoff;
          public unsafe Elf32_Half e_shentsize;
          public unsafe Elf32_Half e_shstrndx;
          };
          private unsafe struct Elf32_Off
          {
          public Byte Byte3;
          public Byte Byte2;
          public Byte Byte1;
          public Byte Byte0;
          };
          private unsafe struct Elf32_Half
          {
          public Byte Byte1;
          public Byte Byte0;
          };
          }

          Comment

          Working...