6

I have an class that implements an interface. I'd like to examine only the property values that implement my interface.

So, let's say for example I have this interface:

public interface IFooBar { string foo { get; set; } string bar { get; set; } } 

And this class:

public class MyClass :IFooBar { public string foo { get; set; } public string bar { get; set; } public int MyOtherPropery1 { get; set; } public string MyOtherProperty2 { get; set; } } 

So, I need to accomplish this, without the magic strings:

var myClassInstance = new MyClass(); foreach (var pi in myClassInstance.GetType().GetProperties()) { if(pi.Name == "MyOtherPropery1" || pi.Name == "MyOtherPropery2") { continue; //Not interested in these property values } //We do work here with the values we want. } 

How can I replace this:

if(pi.Name == 'MyOtherPropery1' || pi.Name == 'MyOtherPropery2') 

Instead of checking to see if my property name is == to a magic string, I'd like to simply check to see if the property is implemented from my interface.

4 Answers 4

7

Why use reflection if you know the interface ahead of time? Why not just test if it implements the interface then cast to that?

var myClassInstance = new MyClass(); var interfaceImplementation = myClassInstance as IFooBar if(interfaceImplementation != null) { //implements interface interfaceImplementation.foo ... } 

If you really must use reflection, get the properties on the interface type, so use this line to get properties:

foreach (var pi in typeof(IFooBar).GetProperties()) { 
Sign up to request clarification or add additional context in comments.

4 Comments

I'm using the interface to compare the values of two objects that implement that interface, but may not be of the same type. Basically an equality check between two different types. The function with this code not only returns the equal/not equal, but the differences. That's the reason for the reflection.
Does my second suggestion help, to just iterate the properties on the interface type?
Yes, that's what I needed. I'll accept your answer as soon as the time for accepting expires. Thanks Paul.
If the objects are of different types, but implement the same interface, you can still cast them both to the interface. For example var a = new List<int>(); var b = new List<string>(); IList c = a; c = b; will compile and run.
3

I tend to agree that it looks like you want to cast to the interface, as Paul T. suggests.

The information you are asking about, however, is available in the InterfaceMapping type, available from the GetInterfaceMap method of the Type instance. From

http://msdn.microsoft.com/en-us/library/4fdhse6f(v=VS.100).aspx

"The interface map denotes how an interface is mapped into the actual methods on a class that implements that interface."

for example:

var map = typeof(int).GetInterfaceMap(typeof(IComparable<int>)); 

Comments

1

Perhaps have a secondary interface:

public interface IFooAlso { int MyOtherProperty1 { get; set; } string MyOtherProperty2 { get; set; } } 

Add the second interface:

public class MyClass :IFooBar, IFooAlso { public string foo { get; set; } public string bar { get; set; } public int MyOtherPropery1 { get; set; } public string MyOtherProperty2 { get; set; } } 

And test it like this:

var myClassInstance = new MyClass(); if(myClassInstance is IFooAlso) { // Use it } 

Comments

0

You can iterate through typeof(T).GetInterfaces and call GetProperties() on each of them.

Reference: http://msdn.microsoft.com/en-us/library/system.type.getinterfaces.aspx

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.