How is the upcoming 'dynamic' keyword in .net 4.0 going to make my life better?

前端 未结 3 841
南方客
南方客 2021-01-04 11:54

I don\'t quite get what it\'s going to let me do (or get away with :)

3条回答
  •  感情败类
    2021-01-04 12:35

    The two big areas are:

    • working with COM assemblies where methods return vague types - so you can essentially use late binding
    • working with DLR types

    Other uses include things like:

    • duck-typing where there is no interface
    • Silverlight talking to the host page's DOM
    • talking to an xml file.

    In C# itself, this allows a few things, such as a basic approach to generic operators:

    static T Add(T arg1, T arg2) { // doesn't work in CTP
         return ((dynamic)arg1) + ((dynamic)arg2);
    }
    

    (of course, I'd argue that this is a better (more efficient) answer to this)

提交回复
热议问题