Is there any way to create an extension method whose parameter's only constraint is having specifically-named properties. e.g.:
public static bool IsMixed<T>(this T obj) where T:?
{
return obj.IsThis && obj.IsThat;
}
I tried to declare the obj as dynamic but it's not allowed.
This feature is often called "duck typing". (Because when you call foo.Quack() all you care about is that it quacks like a duck.) Non-dynamic duck typing is not a feature of C#, sorry!
If you really have no type information about the argument, you can use dynamic in C# 4:
public static bool IsAllThat(this object x)
{
dynamic d = x;
return d.IsThis || d.IsThat;
}
But it would be better to come up with some interface or some such thing that describes the types at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With