Reputation: 3890
What are the advantages in C# 4.0 (besides syntax) of using
DynamicObject.TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
over just calling a non-dynamic method with this signature:
public object MethodParser(string methodName, Dictionary<string, object> arguments)
where methodName is the name of the "method" arguments is a dictionary of the argument name and the argument value (MethodParser is just an arbitrary name)
In other words, calling
foo.NonExistentMethod(arg1:"a1", arg2:3.14m)
over
foo.MethodParser("NonExistentMethod", new Dictionary<string, object>(){{"arg1", "a1"}, {"arg2": 3.14m}})
Upvotes: 3
Views: 693
Reputation: 19230
That's a big question. Here are some thoughts I have on the subject.
Advantages
1) VS can provide intellisence for previously used dynamic members, which is a better IDE experience.
2) It's more consistent with other code, making it more easy to read and understand.
3) It can perform better because it's a layer on top of Reflection
, and can therefore cache delegates, thereby only invoking Reflection
once for a member.
4) It's interoperable with technologies such as COM, which isn't true when using Reflection
, and avoids the need to explicitly cast types between the calls.
5) It provides looser coupling than with Reflection
. It's possible to remap inner members without affecting the code that consumes it.
6) It's possible to virtualise an instance. The members called don't actually need to exist. The implementation is entirely extendable and allows for the creation of proxies/facades which hide implementations from the caller.
Disadvantages
1) Easily abused in scenarios where type checking is unnecessarily lost. It's as simple as typing a keyword vs understanding and writing code around Reflection
.
2) Using the nice syntax it's not possible to protect against magic strings.
I'm sure there's more just wanted to share some points that hadn't been raised.
Upvotes: 3
Reputation: 74939
The advantages of dynamic methods are that they can provide interoperability with dynamic languages, particularly those provided by the DLR, IronPython and IronRuby. The second method you proposed does not provide any interoperability.
Another huge advantage of readability. Someone reviewing and maintaining the code can read and understand the dynamic method call much easier than the dictionary based call.
Upvotes: 4
Reputation: 31809
You have more ability to easily performance tune without refactoring outside of the DynamicObject. For example, if while profiling you find that a specific dynamic method call on that object is a hotspot, you can just add that specific method statically on your DynamicObject, not only do you get a non-string-lookup dynamic call (after the first call), but you can then optimize your specific implementation separate from the rest of your dynamic code in a clean fashion.
Or you can just write a more optimized IDynamicMetaObjectProvider for your entire dynamic code rather than using DynamicObject (like Microsoft did for ExpandoObject) and then it wouldn't need to rely on string lookup for each subsequent invocation like MethodParser
and TryInvokeMember
would.
If the membername isn't really the important, it's hard to tell from the example you could just statically declare property getters that return dynamic with a DynamicObject using DynamicObject.TryInvoke
and then you'll get some autocomplete for the name portion and can still have dynamic argument names.
But obviously the main advantage, is the one we are supposed to discount, is that MethodParser
is much less readable.
Upvotes: 4