0

I try to get my object properties with:

PropertyInfo[] p = typeof(Myobj).GetProperties()

but I receive only

{System.Reflection.PropertyInfo[0]}

My object looks like:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class Myobj
{
  public Subobj1 sub1= new Subobj1();
  public Subobj2 sub2= new Subobj2();
  //...
}

What am I doing wrong?

Jämes
  • 6,945
  • 4
  • 40
  • 56
szaman
  • 6,666
  • 13
  • 53
  • 81

4 Answers4

3

Try creating real properties. You are creating fields instead.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class Myobj
{
    public Subobj1 Sub1 {get; set;}
    public Subobj2 Sub2 {get; set;}
}
meilke
  • 3,280
  • 1
  • 15
  • 31
1

That's because sub1 and sub2 are not properties, they're fields. Change your class declaration to something like this:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    class Myobj
    {
        public Subobj1 sub1 {get; set; }
        public Subobj2 sub2 {get; set; }

...
    }

And initialize the properties with new objects inside your constructor.

Optionally, you can try using the GetFields method instead, but that's not a good approach.

dcastro
  • 66,540
  • 21
  • 145
  • 155
0

sub1 and sub2 are fields not properties. Try declare

public Subobj1 sub1 { get; set; }
public Subobj2 sub2 { get; set; }

public Myobj()
{
    sub1 = new Subobj1();
    sub2 = new Subobj2();
}

If you don't want to change the fields to properties you can use typeof(Myobj).GetFields();

Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
0
[StructLayout(LayoutKind.Sequential, Pack = 1)]
    class Myobj
    {
        public Myobj() {
             sub1 = new Subobj1();
             sub2 = new Subobj2();
        }

        public Subobj1 sub1 { get; set; }
        public Subobj2 sub2 { get; set; }
    }
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68