-3

I am trying to get the data type of a property dynamically in my app. I have the following line of code using the name of the backing variable for the property to get the type and it works correctly.

var tobj_Type = this.GetType().GetField("ii_ServerPort", BindingFlags.Public | BindingFlags.Instance).GetValue(this).GetType();

In this line of code, I attempt to use the name of the property to try to get the type.

var tobj_PropertyType = this.GetType().GetField("ServerPort", BindingFlags.Public | BindingFlags.Instance).GetValue(this).GetType();

It fails with the following error: Object reference not set to an instance of an object.

Any idea how I can use the property name here instead of the backing variable?

George M Ceaser Jr
  • 1,497
  • 3
  • 23
  • 52
  • Isn't there a `BindingFlag.Property` too? – Tanveer Badar Jan 28 '20 at 16:06
  • 1
    @TanveerBadar No, but there's a `GetProperty` method. – juharr Jan 28 '20 at 16:09
  • @TanveerBadar - I did not see that in any of the examples but there are. They are GetProperty, PutDispProperty, PutRefDispProperty and Set Property. I tried the GetProperty and received the same error. There is also a GetProperty that can be used instead of the GetField. I tried and received the same error also. Finally, I do find it a little rude that people down vote the question when exact answer was not given. :) – George M Ceaser Jr Jan 28 '20 at 16:13
  • "I tried the GetProperty and received the same error." can you please post this attempt? the code? please post also a code line that shows how you would access this variable directly or even may be the layout of the class which has this property – Mong Zhu Jan 28 '20 at 16:14
  • I worked on this for five hours. I feel a reasonable amount of effort was put in. Also even here, no one could tell me the exact combination of flags and functions that should be used to achieve the result. After the first post I attempted to use just the GetProperty binding flag. That did not work, then I change the function from GetField to GetProperty (still with the GetProperty binding flag( and that did not work. Johnathan Barclay's answer works. I used this this.GetType().GetProperty("ServerPort", BindingFlags.Public | BindingFlags.Instance).GetValue(this).GetType(); – George M Ceaser Jr Jan 28 '20 at 16:23
  • what type is `ServerPort`? probably something that can be null if not initialized?! you got the exception probably due to your call of `.GetValue(this)` which returned `null`. I can imagine that there was a lot of effort in trying to find a solution for this. I also find it hard to show the people here, that I made enough research before posting a question. I did not downvote, I almost never do. – Mong Zhu Jan 28 '20 at 16:45

2 Answers2

2

Use the GetProperty method instead of GetField

Your call to GetField returns null because their is no field called ServerPort, only a property.

Neil Moss
  • 6,598
  • 2
  • 26
  • 42
2

Yes, but as it is a property and not a field, you need to use GetProperty:

var tobj_PropertyType = this.GetType().GetProperty("ServerPort").PropertyType;

With GetProperty, BindingFlags.Public | BindingFlags.Instance is default and does not need to be specified explicitly.

Also, PropertyInfo has a PropertyType property, so the value does not need to be accessed.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35