0

I have been trying to get properties inside a class which implements an interface. My design is as below,

interface ABC { string Name { get; set; } } public class BCD:ABC { public string Name { get; set; } public string Age{ get; set; } public string Height{ get; set; } public string Weight{ get; set; } } 

Now using Reflection I have tried this,

main() { ABC abcObj = new BCD(); var typeOfObject = typeof(abcObj); var objectProperties = typeOfObject.GetProperties(BindingFlags.Public|BindingFlags.Instance); } 

What i got in objectproperties were the properties in the ABC class. However i need the properties from the BCD class too.

Can someone help on this?

1
  • Have you tried casting it as a BCD class? Commented Jul 21, 2017 at 13:47

1 Answer 1

2

Instead of using typeof (as that applies to class/interface names) try

var typeOfObject = abcObj.GetType(); 

to get the type of the instance.

When I run

ABC abcObj = new BCD(); var objectProperties = abcObj.GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < objectProperties.Length; i++) { Console.WriteLine("{0} ({1})", objectProperties[i].Name, objectProperties[i].PropertyType); } 

I get the following in the console:

Name (System.String)

Age (System.String)

Height (System.String)

Weight (System.String)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.