I have a dynamic value
which I have created a couple extension methods for as follows:
public static bool IsA(this object obj, Type t)
{
return ObjectExtensions.Methods.IsA(obj, t);
}
public static bool IsA(this string obj, Type t)
{
return t == typeof (string);
}
The content of the above methods is out of the scope for this question, but the method headers should show that I have two extension methods: one on object
and one on string
.
The code that is actually trying to utilize the above code is here:
if (!(value.IsA(typeof(string))))
{
//...
}
In the above snippet, value
could be a string
, an IEnumerable
, a model generated from my EF scaffold, etc... It's dynamic.
My issue is that when the type of value
is a string, I get the error:
RuntimeBinderException: 'string' does not contain a definition for 'IsA'
which is a total lie, because when I manually cast value
to string
via ((string)value)
, the IsA
method is suddenly found.
How do I get my extension to work without having to manually cast to a type? (which defeats the purpose of what I'm trying to do (abstract all the crap from checking what something is))