I did follow the following posts' suggestions
- Reflecting over all properties of an interface, including inherited ones?
- How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)
- Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection
and modified my test to include the bindingflags as seen below.
Goal:
my end goal is to test if the retrieved property is readOnly
.
Issue: ChildClass/Inherited class property does not even show up on reflection.
Test to check if property exists
- fails
.
On GetProperties(BindingFlags.FlattenHiearchy)
returns only one property which is the Result
which is the parent class's property
//My failing test
Assert.NotNull(typeof(ChildClass).GetProperty("Id",BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
//My Alternate version which still fails
Assert.NotNull(typeof(ChildClass).GetProperty("Id",BindingFlags.FlattenHierarchy));
// My class structure against which my test is running.
class ChildClass:BaseClass<MyCustomObject>
{
public ChildClass (Guid id)
{
Id = id;
}
public readonly Guid Id;
}
class BaseClass<T>:IMyInterFace<T>
{
public T Result { get; private set; }
}
public interface IMyInterFace<T>
{
T Result { get; }
}
EDIT 9/14/2016
I apologize I missed the fact that I actually did know or understand I can Assert.NotNull
if i say GetField
- but that wouldnt help me achieve the end goal - to check if it is readonly
- I am now unsure as to if this is even possible can someone confirm? thanks!.