I have a class that implements an interface. I would like to study only 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 execute this without magic lines:
var myClassInstance = new MyClass();
foreach (var pi in myClassInstance.GetType().GetProperties()) {
if(pi.Name == "MyOtherPropery1" || pi.Name == "MyOtherPropery2") {
continue;
}
}
How can I replace this:
if(pi.Name == 'MyOtherPropery1' || pi.Name == 'MyOtherPropery2')
Instead of checking to find out if my property name is a ==magic string, I just want to check if the property is implemented from my interface.
source
share