Will C # 4 allow "dynamic casting"? If not, should C # support it?

I do not mean dynamic casting in the sense of casting a lower interface or base class into a more derived class, I mean accepting the definition of the interface that I created, and then dynamically adding another NOT object to this interface received from this interface, but supporting all the challenges.

For example,

interface IMyInterface { bool Visible { get; } } TextBox myTextBox = new TextBox(); IMyInterface i = (dynamic<IMyInterface>)myTextBox; 

This can be achieved at compile time for known types and runtimes for instances declared dynamic. The definition of an interface is known, like the type (in this example), so the compiler can determine whether the object supports the calls defined by the interface and does some magic for us to have a cast.

I assume this is not supported in C # 4 (I could not find a link to it), but I would like to know for sure. And if this is not so, I would like to discuss whether it should be included in the future version of the language or not, as well as the reasons and against. For me, this seems like a nice addition to provide a wider polymorphism in the code without the need to create whole new types to migrate existing frame types.

Update
So that someone would not accuse me of plagiarism, I did not know about Jon Skeet, who had already suggested this . However, it's nice to know that we thought of an extremely close syntax that suggests that it can be intuitive, at least. Meanwhile, “having an original idea” remains on my slave list for another day.

+6
polymorphism c # dynamic
source share
4 answers

I think John Skeet had such an offer ( http://msmvps.com/blogs/jon_skeet/archive/2008/10/30/c-4-0-dynamic-lt-t-gt.aspx ), but before so far I have not heard that C # 4.0 will have it.

+3
source share

I think that is problematic. You introduce a relationship between two classes that are not related.

Consider the following code.

 public interface IFoo { int MethodA(); int MethodB(); } public class Bar { int MethodA(); int MethodB(); } public class SomeClass { int MethodFoo(IFoo someFoo); } 

Should it be legal?

 int blah = someClass.MethodFoo((dynamic<IFoo>)bar); 

It seems like this should be legal, because the compiler should be able to dynamically type the bar as something that implements IFoo.

However, at this point you are linking IFoo and Bar by calling in a completely separate part of your code.

If you are editing Bar because it no longer needs MethodB, suddenly someClass.MethodFood is no longer working, although Bar and IFoo are not connected.

In the same way, if you add MethodC () to IFoo, your code will break again, although IFoo and Bar are supposedly unrelated.

The fact is that although this would be useful in individual cases when there are similarities between objects that you do not control, there is a reason that interfaces must be explicitly bound to objects, and the reason is that the compiler may have the object implement it.

+3
source share

C # does not need to support this, as it can be implemented very simply as a library.

I saw three or four separate implementations (I started writing one myself before I found them). Here is the most thorough treatment I've seen:

http://bartdesmet.net/blogs/bart/archive/2008/11/10/introducing-the-c-ducktaper-bridging-the-dynamic-world-with-the-static-world.aspx

It may be even easier to implement once the DLR is integrated at run time.

Since the shell / forwarder class for this interface can be generated once and then cached, and then one object of an unknown type can be wrapped once, there are many possibilities for caching call sites, etc., therefore, the performance should be excellent.

On the contrary, I think that the dynamic keyword, which is a language function and extremely complex, is an unnecessary and potentially catastrophic digression learned in a language that previously had a very clear philosophy of static typing, which gave it an obvious direction for further improvement. They had to adhere to this and make type conclusions better and better until typing became more invisible. There are so many areas where they can develop the language without breaking existing programs, but still they don’t, simply because of resource limitations (for example, the reason var cannot be used in more places because they will have to rewrite the compiler and they don’t have time).

They still do good things in C # 4.0 (dispersion functions), but there is much more that could be done to make the type system smarter, more automatic and more powerful at detecting problems during compilation. Instead, we essentially get the trick.

+1
source share

The opensource Impromptu-Interface framework does this using C # 4 and dlr.

 using ImpromptuInterface; interface IMyInterface { bool Visible { get; } } TextBox myTextBox = new TextBox(); IMyInterface i = myTextBox.ActLike<IMyInterface>(); 

Since it uses dlr, it will also work with ExpandoObject and DynamicObject.

+1
source share

All Articles