Using Reflection to get all the properties of an object not implemented by the interface

I want to use reflection to scroll through object properties that DO NOT implement an interface

In fact, I want to achieve the opposite of this. How can I use reflection to get properties that explicitly implement the interface?

I want to bind objects to another object where any properties that are not defined by the interface are instead added to the KeyValuePairs list.

+6
source share
1 answer

Using this example:

 interface IFoo { string A { get; set; } } class Foo : IFoo { public string A { get; set; } public string B { get; set; } } 

Then, using this code, I only get PropertyInfo for B

  var fooProps = typeof(Foo).GetProperties(); var implementedProps = typeof(Foo).GetInterfaces().SelectMany(i => i.GetProperties()); var onlyInFoo = fooProps.Select(prop => prop.Name).Except(implementedProps.Select(prop => prop.Name)).ToArray(); var fooPropsFiltered = fooProps.Where(x => onlyInFoo.Contains(x.Name)); 
+6
source

All Articles