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,455
  • 3
  • 37
  • 52
szaman
  • 6,576
  • 13
  • 50
  • 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,212
  • 1
  • 13
  • 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
  • 63,281
  • 21
  • 137
  • 151
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,333
  • 2
  • 33
  • 31
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,117
  • 5
  • 34
  • 67