1

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))

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • 3
    "because in c# there is no common ancestor between all types"??? - `System.Object` – Igor Nov 24 '15 at 17:43
  • 3
    "in c# there is no common ancestor between all types" => ummm... `object`? – Jon Nov 24 '15 at 17:43
  • `string` does *not* contain a definition for `IsA`. You can verify this by running `typeof(string).GetMethods("IsA")`. Whether it should see your extension method (a static method like any other) or not is another thing :) – Luaan Nov 24 '15 at 17:46
  • 1
    Possible duplicate of [Will the dynamic keyword in C#4 support extension methods?](http://stackoverflow.com/questions/258988/will-the-dynamic-keyword-in-c4-support-extension-methods) – CoderDennis Nov 24 '15 at 17:48
  • Possible duplicate of [Extension method and dynamic object](http://stackoverflow.com/questions/5311465/extension-method-and-dynamic-object) – willaien Nov 24 '15 at 17:52
  • @Igor I somewhere earlier today that they were seperate -- something about value types vs object types. But looking at the MSDN documentation: https://msdn.microsoft.com/en-us/library/system.string.aspx It clearly says that string is a type of object. Even decimals are an object (cause decimal is a ValueType) https://msdn.microsoft.com/en-us/library/system.valuetype.aspx I'll update my question to remove the erroneous statement. – NullVoxPopuli Nov 24 '15 at 17:57
  • @jon That reply is for you as well ^ – NullVoxPopuli Nov 24 '15 at 17:58
  • @NullVoxPopuli - value types vs reference types – Igor Nov 24 '15 at 18:00
  • @CoderDennis that question seems to be the closest to my question – NullVoxPopuli Nov 24 '15 at 18:01

1 Answers1

4

Dynamic is determined at runtime. Unfortunately, the DLR doesn't consider extension methods - those are compile-time sugar.

You can, however, do this:

IsA(value, typeof(string))

Extension methods are really just regular static methods with some syntactic sugar that lets you treat them like methods on that instance, even though it's really not.

Edit: Alternately, you should be able to cast the dynamic to System.Object, and then invoke the extension method, like so:

(value as object).IsA(typeof(string))

willaien
  • 2,647
  • 15
  • 24