c# - Methods overloading -
When I call EntryPoint with a parameter type of TemplateA, then I always get an exception, because first Overload is always called
What I expect to happen is that due to dynamic binding, the most specific method (second surcharge) will be called.
Any ideas why?
Private objects _obj; Public Zero EntryPoint (Object P) {myFunc (P); } // First surcharge private zero myFunc (object container) {throwing new NotImplementedException; } // second overload private zero myFunc (TemplateA template) {_obj = new ObjTypeA (template); } // Third Overload Private Zero myFunc (TemplateB Template) {_obj = New ObjTypeB (template); }
If you use then you can use it in C # 4.0 Will be able to. Dynamic
instead of object
. Download Visual Studio 2010 Beta, if you want to give it a try, the compiler chooses exactly the same way to call the parameter based on the compile-time type.
It is not clear from your question whether you know about simple single dispatch polymorphism, because it will solve your example problem.
and elsewhere:
Private objects _obj; Public Zero EntryPoint (TemplateBase P) {_obj = p.MyFunc (); }
When you can not modify the TemplateN
classes, then there are alternatives for a representative from dictionary
mapping The easiest way to get access to type
will be entrypoint
method
dictionary < Type, Funk & lt; Object, object & gt; & Gt; _myFuncs; _myFuncs.Add (TemplateA), O => New ObjTypeA (TemplateA) o);); _myFuncs.Add (TypeP (TemplateB), o = & gt; New ObjTypeB (TemplateA) o););
Then he could see the representative for the execution of the type of object given.
If you want to duplicate the exact way of virtual functions, then you have to take care of the inheritance hierarchy.
Comments
Post a Comment