Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Non-Inherited Properties

Tags:

I'm trying to read all of the properties of a given object, reading in only those that are declared on the object's type, excluding those that are inherited. IE:

class Parent {    public string A { get; set; } }  class Child : Parent {    public string B { get; set; } } 

And so I want to only get B back. Reading the docs, I assumed below was what I needed, but that actually returned nothing at all.

var names = InstanceOfChild.GetType().GetProperties(BindingFlags.DeclaredOnly).Select(pi => pi.Name).ToList(); 
like image 671
Adam Rackis Avatar asked May 12 '11 16:05

Adam Rackis


People also ask

Which CSS properties are not inherited?

CSS properties such as height , width , border , margin , padding , etc. are not inherited.

Are width and height CSS properties are inherited by default?

CSS height and width Valuesauto - This is default. The browser calculates the height and width.


2 Answers

Just need a couple other BindingFlags

var names = InstanceOfChild.GetType().GetProperties(    BindingFlags.DeclaredOnly |    BindingFlags.Public |      BindingFlags.Instance).Select(pi => pi.Name).ToList(); 
like image 132
Clayton Avatar answered Sep 22 '22 18:09

Clayton


Try this:

var names = InstanceOfChild.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Select(pi => pi.Name).ToList(); 

I added the BidningFlags.Instance and BindingFlags.Public to the search parameters which according to the MSDN documentation respectfully:

Specifies that instance members are to be included in the search.

and

Specifies that public members are to be included in the search.

like image 22
Nick Berardi Avatar answered Sep 20 '22 18:09

Nick Berardi