5

I have a console aplication where I am trying to obtain the values of the properties of an object dynamically:

  class Program
  {
    static void Main(string[] args)
    {
      DtoCartaCompromiso test = new DtoCartaCompromiso() { CodProducto = 1,
        DescProducto = "aaa",
        CodProveedor = 2,
        DescProveedor = "bbb",
        FechaExpiracion = DateTime.Now,
        FechaMaxEntrega = DateTime.Now,
        NumLote = "22" };

      var testlist = new List<DtoCartaCompromiso>();
      testlist.Add(test);

      List<Header> columns = new List<Header>() { new Header{Name= "CodProducto"},new Header{Name=  "NumLote"},new Header{Name=  "DescProducto"},new Header{Name=  "CodProveedor"},new Header{Name=  "DescProveedor"},new Header{Name=  "FechaExpiracion"},new Header{Name=  "FechaExpiracion"},new Header{Name=  "FechaMaxEntrega"} };

      foreach (var d in testlist)
      {
        foreach (var col in columns)
        {
            string value = ((d.GetType().GetProperty(col.Name).GetValue(d, null)) ?? "").ToString();
            Console.WriteLine(value);
        }
      }
      Console.Read();    
    }
  }

  public class DtoCartaCompromiso
  {
    public int CodProducto;
    public string NumLote;
    public string DescProducto;
    public int CodProveedor;
    public string DescProveedor;
    public Nullable<DateTime> FechaExpiracion;
    public Nullable<DateTime> FechaMaxEntrega;
  }

  public class Header
  {
      public string Name;
  }

i am getting the error "Object reference not set to an instance of an object" when I get to the line:

string value = ((d.GetType().GetProperty(col.Name).GetValue(d, null)) ?? "").ToString();

the error seems to occur when I get to the GetProperty() method, but I dont understand why

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
mmilan
  • 1,738
  • 8
  • 31
  • 57
  • Break up this long line into individual function calls, assign intermediate values to locals and see for yourself what is null. Probably the property is not public or the name does not match. – Anton Tykhyy Dec 03 '13 at 20:21

3 Answers3

16

The problem is that you don't have properties there in your classes, they are public fields really. A public property looks like

public string PropertyName { get; set; }

but in your case there is lack of both getters and setters.

Change GetProperty() to GetField() and it will work. Or make your fields properties. Personally, I would go with the second option since it is a better idea to use properties instead of public fields.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • Thanks! I thought that I was using properties, I used the prop code snippet for everying in the DtoCartaCompromiso class. could you tell me why they are being considered fields? – mmilan Dec 03 '13 at 20:35
  • 1
    Is strange that `prop` did not generated getter and setter for you. Basically, properties look like I have showed in my answer. If it don't have a getter, or setter, or both, it is not a property. Read some tutorial about this on the internet, please. – Ondrej Janacek Dec 03 '13 at 20:37
  • thanks again, I really should have seen that earlier! – mmilan Dec 03 '13 at 20:40
  • Thank so much for that GetField() method... I have spent an hour to find out why GetProperty() didn't return anything. – Jan Kadeřábek Feb 24 '17 at 23:18
0

Best guess without knowing more about your application, is the following sub-exression results in null:

d.GetType().GetProperty(col.Name)

At that point, the subsequent .GetValue() call will fail with your reported error.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • it seems that it is returning null, any idea on why? the DtoCartaCompromiso is public, and all its properties are also public. the properties contain data. – mmilan Dec 03 '13 at 20:28
  • Whatever the value of `col.Name`, the type of `d` does **not** contain a public property with that name. It may seem like it does or should, but I promise you that a close enough inspection will show it doesn't. One tip is to remember that this lookup is case sensitive. – Joel Coehoorn Dec 03 '13 at 20:31
  • @JoelCoehoorn He used public fields instead of properties, that was the problem ;) – Ondrej Janacek Dec 03 '13 at 20:33
-1

Your property is probably not public. Make it public or pass System.Reflection.BindingFlags.NonPublic to your GetProperty call.

More info: http://msdn.microsoft.com/en-us/library/zy0d4103(v=vs.110).aspx

John Gibb
  • 10,603
  • 2
  • 37
  • 48