Can anyone see what I am doing wrong below? The type has the public property that the service method is trying to access so why is it not being picked up by reflection?
Public class SomeClass
{
private YetAnotherClass yetAnotherClass;
public SomeClass(SomeOtherClass otherclass)
{
this.yetAnotherClass = otherclass.SomeProperty;
}
public YetAnotherClass SomeProperty
{
get { return this.yetAnotherClass; }
}
}
Public class ServiceClass
{
public void DoSomething(SomeClass someclass)
{
Type type = someclass.GetType();
FieldInfo[] fieldsinfo = type.GetFields(BindingFlags.Public | BindingFlags.Instance); // returns empty collection
FieldInfo fieldinfo = type.GetField("SomeProperty"); // returns null reference exception
}
}
Cheers
Stewart
SomeProperty is - as the name says - a property. Use GetProperty and GetProperties instead! That leads to PropertyInfo instead of FieldInfo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With