locked
Get PropertyInfo of Nullable property RRS feed

  • Question

  • Hello,

    first of all, I'm using .NET Framework 3.5.

    I have a class with a public nullable property (DateTime?). I need to have a PropertyInfo of this property, but I am getting null when calling this code:

    PropertyInfo pi = typeof([MyClassName]).GetProperty([NullablePropertyName]);

    I can see in watch when debugging that the real type is System.Reflection.RuntimePropertyInfo. But when I try to assign it to variable of object type, I have still null.

    Q: How can I get the PropertyInfo of nullable property? I have list of properties and I call GetValue/SetValue later in my code.

    Monday, March 5, 2012 3:42 PM

Answers

  • Hi,

    I am wonderig a little about that. I just did a quick test with a Windows Forms application in which I tried to get the same behaviour. So I simply added.
    public DateTime? NullablePropertyName { get; set; }
    and inside a button event I did:
    Type t = typeof (Form1);
    PropertyInfo pi = t.GetProperty("NullablePropertyName");

    And I got a PropertyInfo back (System.Nullable`1[System.DateTime]) - as expected.

    Maybe you can give your real code? Did you misspelled the property name?

    WIth kind regards,

    Konrad

    • Proposed as answer by Anthony Trudeau Monday, March 5, 2012 10:15 PM
    • Marked as answer by Roman Lonik Tuesday, March 6, 2012 6:57 AM
    Monday, March 5, 2012 3:52 PM

All replies

  • Hi,

    I am wonderig a little about that. I just did a quick test with a Windows Forms application in which I tried to get the same behaviour. So I simply added.
    public DateTime? NullablePropertyName { get; set; }
    and inside a button event I did:
    Type t = typeof (Form1);
    PropertyInfo pi = t.GetProperty("NullablePropertyName");

    And I got a PropertyInfo back (System.Nullable`1[System.DateTime]) - as expected.

    Maybe you can give your real code? Did you misspelled the property name?

    WIth kind regards,

    Konrad

    • Proposed as answer by Anthony Trudeau Monday, March 5, 2012 10:15 PM
    • Marked as answer by Roman Lonik Tuesday, March 6, 2012 6:57 AM
    Monday, March 5, 2012 3:52 PM
  • Hello Konrad,

    you were right, I really misspelled the property name (and spent about 2 hours to find answer to my question).

    Thank you very much for your time.

    Monday, March 5, 2012 4:02 PM