1

Hellow,

I am dealing with a small edge case where I need to figure out if an object is in fact dynamic object. For now I came up with this:

public static bool IsDynamic(this object obj)
{
    if (obj == null)
        return false;
    return obj is IDynamicMetaObjectProvider;
}

Is there a better way or other things that I should also be checking?

Thanks!

Valo
  • 1,872
  • 2
  • 15
  • 23
  • 5
    The fact that you're asking means that you want to do something with "dynamic objects" and not do it to non-dynamic objects. What is the thing you want to do? Since we don't know what you're trying to achieve it is hard to tell you what you could be doing better to achieve it. – Eric Lippert May 03 '17 at 20:18
  • 4
    This questions seems to have already been answered here : http://stackoverflow.com/a/7362847/3318781 – Christophe May 03 '17 at 20:19
  • @EricLippert, Like I said it is very small edge case in one of my projects (https://github.com/vmelamed/vm/tree/master/Aspects/Diagnostics) where I use reflection to dump the properties of a random object. Also I use Expressions to create the dumping code (no reflection), cache it, and the next time I want to dump an object of the same type I simply run the cached dumping code (>100 times faster). However this generation makes no sense for dynamic objects, because, well - they are dynamic and the cached code will be invalid. – Valo May 03 '17 at 20:36
  • @Christophe, it is not really the same question. – Valo May 03 '17 at 20:40
  • Interestingly enough, your strategy of "generate expression trees, cache them and re-use them" is precisely the strategy used by the dynamic runtime to make the second call to a dynamic call site faster. Were you inspired by the implementation of the dynamic runtime, or is this just a case of great minds thinking alike? – Eric Lippert May 03 '17 at 20:43
  • Eric, I am truly flattered! Thanks! No, I don't know much about the DLR implementation. I was inspired by another project where I wanted to serialize and deserialize expression trees, e.g. to XML... I guess come to it from the opposite end I guess. :) – Valo May 03 '17 at 20:58

0 Answers0