While you cannot do what you are looking for with common constraints, you can use reflection to check the type at runtime to determine if it has these properties and dynamically get its values.
Disclaimer: I am doing this from head to toe, maybe I'm a bit off track.
public static bool IsMixed(this object obj)
{
Type type = obj.GetType();
PropertyInfo isThisProperty = type.GetProperty("IsThis", typeof(bool));
PropertyInfo isThatProperty = type.GetProperty("IsThat", typeof(bool));
if (isThisProperty != null && isThatProperty != null)
{
bool isThis = isThisProperty.GetValue(this, null);
bool isThat = isThatProperty.GetValue(this, null);
return isThis && isThat;
}
else
{
throw new ArgumentException(
"Object must have properties IsThis and IsThat.",
"obj"
);
}
}
source
share