Here is my code:
public static class DynamicExtensions
public static void Add(this ExpandoObject obj, string path){
dynamic _obj = obj;
if (_obj == null) throw new ArgumentNullException("obj");
_obj.path = path;
}
}
But I got the error of “‘System.Dynamic.ExpandoObject’ does not contain a definition for ‘Add'”, when I call it in this way:
dynamic obj = new ExpandoObject();
obj.Add("p1");
How to fix it?
Thanks in advance!
The problem is using
dynamicwith extension methods – the two just don’t go together. This would be fine:… but with just
dynamic, there’s no extension method support.From section 7.6.5.2 of the C# 5 spec:
While the compiler could remember the
usingdirectives which it would have to check to apply extension methods, it just doesn’t – perhaps for performance reasons, or perhaps because it was just felt to be a lot of work for relatively little benefit.