Overloading method without modifying classesHidden Features of C#?How can I return NULL from a generic method in C#?Function overloading in Javascript - Best practicesHow do I get a consistent byte representation of strings in C# without manually specifying an encoding?Understanding Python super() with __init__() methodsIEnumerable vs List - What to Use? How do they work?Python class inherits objectVisitor Pattern in C++ with multiple visitable parametersWhy not inherit from List<T>?Which Overloaded Method is Called in Java
Can a Beast Master ranger choose a swarm as an animal companion?
Chess software to analyze games
How could China have extradited people for political reason under the extradition law it wanted to pass in Hong Kong?
Does the Green Flame-Blade cantrip work with the Zephyr Strike spell?
How to detect a failed AES256 decryption programmatically?
Are there reliable, formulaic ways to form chords on the guitar?
Does git delete empty folders?
Vacuum collapse -- why do strong metals implode but glass doesn't?
Nuclear decay triggers
Using は before 欲しい instead が
Would it be illegal for Facebook to actively promote a political agenda?
Why do some academic journals requires a separate "summary" paragraph in addition to an abstract?
What can I do to keep a threaded bolt from falling out of it’s slot
Land Registry Clause
Gofer work in exchange for Letter of Recommendation
Levenshtein Neighbours
How does the Saturn V Dynamic Test Stand work?
Is there a commercial liquid with refractive index greater than n=2?
Do living authors still get paid royalties for their old work?
Are there any OR challenges that are similar to kaggle's competitions?
How do slats reduce stall speed?
Story about a demon trying to make a man insane
What is "super" in superphosphate?
90s(?) book series about two people transported to a parallel medieval world, she joins city watch, he becomes wizard
Overloading method without modifying classes
Hidden Features of C#?How can I return NULL from a generic method in C#?Function overloading in Javascript - Best practicesHow do I get a consistent byte representation of strings in C# without manually specifying an encoding?Understanding Python super() with __init__() methodsIEnumerable vs List - What to Use? How do they work?Python class inherits objectVisitor Pattern in C++ with multiple visitable parametersWhy not inherit from List<T>?Which Overloaded Method is Called in Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have access to a class structure, which I cannot modify, as follows:
Graphics
Circle
Line
etc.
Again, I cannot modify it! These all have individual properties such as Radius, FirstPoint, LastPoint, etc., as well as some common properties.
I want to create a method that accepts a Graphics object, and depending on the type of object, will run a ToJson method:
Graphics g = db.GetGraphic(123);
// Console.WriteLine(g.GetType()) prints "Circle"
// Should run some specific implementation for `Circle` type graphics, and
// have an overload for all types including Graphics
ToJson(g);
At first I imagined I could craftily overload the ToJson method:
ToJson(Graphics g) ...
ToJson(Circle g) ...
ToJson(Line g) ...
however this of course goes for the generic ToJson(Graphics) overload each time.
I'm sure I could do something like the following:
if (g is Circle) ...
if (g is Line) ...
if (g is Graphics) ...
or create a Dictionary to reduce the complexity for each type, but that doesn't feel like the best way of doing things
What I've considered
I've considered if there's some generic wrapper method I could use around each object (e.g., new JsonGraphics(g).ToJson()), but I do not want to have to perform any manual type checking myself.
I have looked at the double dispatch and visitor pattern, but I wasn't sure they met my requirements as they look like I have to modify these classes (or maybe I just didn't fully understand them), and (kinda obvious, however) generics are also basically out of the window as they require me to know in advance what type of Graphics object this is.
Two questions then:
Is there a better way to do this other than to use some Dictionary or something other if (g is Type)-like?
If I could modify the classes, how would the pattern look? This isn't possible in this case, but is double dispatch/visitor the best way to go in the case I could?
c# inheritance overloading visitor double-dispatch
|
show 9 more comments
I have access to a class structure, which I cannot modify, as follows:
Graphics
Circle
Line
etc.
Again, I cannot modify it! These all have individual properties such as Radius, FirstPoint, LastPoint, etc., as well as some common properties.
I want to create a method that accepts a Graphics object, and depending on the type of object, will run a ToJson method:
Graphics g = db.GetGraphic(123);
// Console.WriteLine(g.GetType()) prints "Circle"
// Should run some specific implementation for `Circle` type graphics, and
// have an overload for all types including Graphics
ToJson(g);
At first I imagined I could craftily overload the ToJson method:
ToJson(Graphics g) ...
ToJson(Circle g) ...
ToJson(Line g) ...
however this of course goes for the generic ToJson(Graphics) overload each time.
I'm sure I could do something like the following:
if (g is Circle) ...
if (g is Line) ...
if (g is Graphics) ...
or create a Dictionary to reduce the complexity for each type, but that doesn't feel like the best way of doing things
What I've considered
I've considered if there's some generic wrapper method I could use around each object (e.g., new JsonGraphics(g).ToJson()), but I do not want to have to perform any manual type checking myself.
I have looked at the double dispatch and visitor pattern, but I wasn't sure they met my requirements as they look like I have to modify these classes (or maybe I just didn't fully understand them), and (kinda obvious, however) generics are also basically out of the window as they require me to know in advance what type of Graphics object this is.
Two questions then:
Is there a better way to do this other than to use some Dictionary or something other if (g is Type)-like?
If I could modify the classes, how would the pattern look? This isn't possible in this case, but is double dispatch/visitor the best way to go in the case I could?
c# inheritance overloading visitor double-dispatch
If you could modify the types, you would add a virtual/abstract ToJson instance method and override as needed. A "rule engine" where you use reflection to find the right implementation of the ToJson code is probably the best way to go.
– Lasse Vågsæther Karlsen
Mar 27 at 14:42
So yes, a dictionary of type to delegate/object that implements this is probably what I would choose myself.
– Lasse Vågsæther Karlsen
Mar 27 at 14:43
Decorator pattern?
– Kenneth K.
Mar 27 at 14:43
1
As you note, to use the visitor pattern for double dispatch, you need the "visited" class to implement a method that calls the "visitor" class back on the appropriate method. I give a simple example here: ericlippert.com/2015/05/04/wizards-and-warriors-part-three You normally use the visitor pattern when you have a group of related objects, you have a variety of analysis or transformation passes you need to perform on those objects, and the exact details of each operation depend on the run-time type of both the visited and visitor objects.
– Eric Lippert
Mar 27 at 15:26
1
Thank you for your input Eric - I'll give that a read! 🙂
– Nick Bull
Mar 27 at 17:07
|
show 9 more comments
I have access to a class structure, which I cannot modify, as follows:
Graphics
Circle
Line
etc.
Again, I cannot modify it! These all have individual properties such as Radius, FirstPoint, LastPoint, etc., as well as some common properties.
I want to create a method that accepts a Graphics object, and depending on the type of object, will run a ToJson method:
Graphics g = db.GetGraphic(123);
// Console.WriteLine(g.GetType()) prints "Circle"
// Should run some specific implementation for `Circle` type graphics, and
// have an overload for all types including Graphics
ToJson(g);
At first I imagined I could craftily overload the ToJson method:
ToJson(Graphics g) ...
ToJson(Circle g) ...
ToJson(Line g) ...
however this of course goes for the generic ToJson(Graphics) overload each time.
I'm sure I could do something like the following:
if (g is Circle) ...
if (g is Line) ...
if (g is Graphics) ...
or create a Dictionary to reduce the complexity for each type, but that doesn't feel like the best way of doing things
What I've considered
I've considered if there's some generic wrapper method I could use around each object (e.g., new JsonGraphics(g).ToJson()), but I do not want to have to perform any manual type checking myself.
I have looked at the double dispatch and visitor pattern, but I wasn't sure they met my requirements as they look like I have to modify these classes (or maybe I just didn't fully understand them), and (kinda obvious, however) generics are also basically out of the window as they require me to know in advance what type of Graphics object this is.
Two questions then:
Is there a better way to do this other than to use some Dictionary or something other if (g is Type)-like?
If I could modify the classes, how would the pattern look? This isn't possible in this case, but is double dispatch/visitor the best way to go in the case I could?
c# inheritance overloading visitor double-dispatch
I have access to a class structure, which I cannot modify, as follows:
Graphics
Circle
Line
etc.
Again, I cannot modify it! These all have individual properties such as Radius, FirstPoint, LastPoint, etc., as well as some common properties.
I want to create a method that accepts a Graphics object, and depending on the type of object, will run a ToJson method:
Graphics g = db.GetGraphic(123);
// Console.WriteLine(g.GetType()) prints "Circle"
// Should run some specific implementation for `Circle` type graphics, and
// have an overload for all types including Graphics
ToJson(g);
At first I imagined I could craftily overload the ToJson method:
ToJson(Graphics g) ...
ToJson(Circle g) ...
ToJson(Line g) ...
however this of course goes for the generic ToJson(Graphics) overload each time.
I'm sure I could do something like the following:
if (g is Circle) ...
if (g is Line) ...
if (g is Graphics) ...
or create a Dictionary to reduce the complexity for each type, but that doesn't feel like the best way of doing things
What I've considered
I've considered if there's some generic wrapper method I could use around each object (e.g., new JsonGraphics(g).ToJson()), but I do not want to have to perform any manual type checking myself.
I have looked at the double dispatch and visitor pattern, but I wasn't sure they met my requirements as they look like I have to modify these classes (or maybe I just didn't fully understand them), and (kinda obvious, however) generics are also basically out of the window as they require me to know in advance what type of Graphics object this is.
Two questions then:
Is there a better way to do this other than to use some Dictionary or something other if (g is Type)-like?
If I could modify the classes, how would the pattern look? This isn't possible in this case, but is double dispatch/visitor the best way to go in the case I could?
c# inheritance overloading visitor double-dispatch
c# inheritance overloading visitor double-dispatch
edited Mar 27 at 14:42
Nick Bull
asked Mar 27 at 14:40
Nick BullNick Bull
6,6663 gold badges16 silver badges27 bronze badges
6,6663 gold badges16 silver badges27 bronze badges
If you could modify the types, you would add a virtual/abstract ToJson instance method and override as needed. A "rule engine" where you use reflection to find the right implementation of the ToJson code is probably the best way to go.
– Lasse Vågsæther Karlsen
Mar 27 at 14:42
So yes, a dictionary of type to delegate/object that implements this is probably what I would choose myself.
– Lasse Vågsæther Karlsen
Mar 27 at 14:43
Decorator pattern?
– Kenneth K.
Mar 27 at 14:43
1
As you note, to use the visitor pattern for double dispatch, you need the "visited" class to implement a method that calls the "visitor" class back on the appropriate method. I give a simple example here: ericlippert.com/2015/05/04/wizards-and-warriors-part-three You normally use the visitor pattern when you have a group of related objects, you have a variety of analysis or transformation passes you need to perform on those objects, and the exact details of each operation depend on the run-time type of both the visited and visitor objects.
– Eric Lippert
Mar 27 at 15:26
1
Thank you for your input Eric - I'll give that a read! 🙂
– Nick Bull
Mar 27 at 17:07
|
show 9 more comments
If you could modify the types, you would add a virtual/abstract ToJson instance method and override as needed. A "rule engine" where you use reflection to find the right implementation of the ToJson code is probably the best way to go.
– Lasse Vågsæther Karlsen
Mar 27 at 14:42
So yes, a dictionary of type to delegate/object that implements this is probably what I would choose myself.
– Lasse Vågsæther Karlsen
Mar 27 at 14:43
Decorator pattern?
– Kenneth K.
Mar 27 at 14:43
1
As you note, to use the visitor pattern for double dispatch, you need the "visited" class to implement a method that calls the "visitor" class back on the appropriate method. I give a simple example here: ericlippert.com/2015/05/04/wizards-and-warriors-part-three You normally use the visitor pattern when you have a group of related objects, you have a variety of analysis or transformation passes you need to perform on those objects, and the exact details of each operation depend on the run-time type of both the visited and visitor objects.
– Eric Lippert
Mar 27 at 15:26
1
Thank you for your input Eric - I'll give that a read! 🙂
– Nick Bull
Mar 27 at 17:07
If you could modify the types, you would add a virtual/abstract ToJson instance method and override as needed. A "rule engine" where you use reflection to find the right implementation of the ToJson code is probably the best way to go.
– Lasse Vågsæther Karlsen
Mar 27 at 14:42
If you could modify the types, you would add a virtual/abstract ToJson instance method and override as needed. A "rule engine" where you use reflection to find the right implementation of the ToJson code is probably the best way to go.
– Lasse Vågsæther Karlsen
Mar 27 at 14:42
So yes, a dictionary of type to delegate/object that implements this is probably what I would choose myself.
– Lasse Vågsæther Karlsen
Mar 27 at 14:43
So yes, a dictionary of type to delegate/object that implements this is probably what I would choose myself.
– Lasse Vågsæther Karlsen
Mar 27 at 14:43
Decorator pattern?
– Kenneth K.
Mar 27 at 14:43
Decorator pattern?
– Kenneth K.
Mar 27 at 14:43
1
1
As you note, to use the visitor pattern for double dispatch, you need the "visited" class to implement a method that calls the "visitor" class back on the appropriate method. I give a simple example here: ericlippert.com/2015/05/04/wizards-and-warriors-part-three You normally use the visitor pattern when you have a group of related objects, you have a variety of analysis or transformation passes you need to perform on those objects, and the exact details of each operation depend on the run-time type of both the visited and visitor objects.
– Eric Lippert
Mar 27 at 15:26
As you note, to use the visitor pattern for double dispatch, you need the "visited" class to implement a method that calls the "visitor" class back on the appropriate method. I give a simple example here: ericlippert.com/2015/05/04/wizards-and-warriors-part-three You normally use the visitor pattern when you have a group of related objects, you have a variety of analysis or transformation passes you need to perform on those objects, and the exact details of each operation depend on the run-time type of both the visited and visitor objects.
– Eric Lippert
Mar 27 at 15:26
1
1
Thank you for your input Eric - I'll give that a read! 🙂
– Nick Bull
Mar 27 at 17:07
Thank you for your input Eric - I'll give that a read! 🙂
– Nick Bull
Mar 27 at 17:07
|
show 9 more comments
1 Answer
1
active
oldest
votes
Without being able to modify the base class, or have access to the concrete type before it's turned into a generic Graphics type, unfortunately I don't think there's anything you can do except inspect the runtime type of the Graphics object.
You can use a switch statement (since C# 7.0), which is slightly cleaner than your if chain:
switch (g)
case Circle circle: ... break;
case Line line: ... break;
default: /* Oh no! */ break;
Personally I don't see much advantage in using a Dictionary over a switch statement like this - both can be put away into a little self-contained method (and so reduce the amount of violation of the open/close principle), but the switch will be significantly cheaper.
You can also use dynamic, which causes the runtime to do late binding:
dynamic d = g;
ToJson(d); // Picks the right ToJson overload corresponding to the runtime type of 'd'
... although, dynamic has a fairly large runtime cost, and is generally considered smell.
Theswitchis using some pattern matching? I wasn't aware switch statements in C# could do that! :)
– Nick Bull
Mar 27 at 14:48
1
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
1
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55379942%2foverloading-method-without-modifying-classes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Without being able to modify the base class, or have access to the concrete type before it's turned into a generic Graphics type, unfortunately I don't think there's anything you can do except inspect the runtime type of the Graphics object.
You can use a switch statement (since C# 7.0), which is slightly cleaner than your if chain:
switch (g)
case Circle circle: ... break;
case Line line: ... break;
default: /* Oh no! */ break;
Personally I don't see much advantage in using a Dictionary over a switch statement like this - both can be put away into a little self-contained method (and so reduce the amount of violation of the open/close principle), but the switch will be significantly cheaper.
You can also use dynamic, which causes the runtime to do late binding:
dynamic d = g;
ToJson(d); // Picks the right ToJson overload corresponding to the runtime type of 'd'
... although, dynamic has a fairly large runtime cost, and is generally considered smell.
Theswitchis using some pattern matching? I wasn't aware switch statements in C# could do that! :)
– Nick Bull
Mar 27 at 14:48
1
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
1
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
add a comment |
Without being able to modify the base class, or have access to the concrete type before it's turned into a generic Graphics type, unfortunately I don't think there's anything you can do except inspect the runtime type of the Graphics object.
You can use a switch statement (since C# 7.0), which is slightly cleaner than your if chain:
switch (g)
case Circle circle: ... break;
case Line line: ... break;
default: /* Oh no! */ break;
Personally I don't see much advantage in using a Dictionary over a switch statement like this - both can be put away into a little self-contained method (and so reduce the amount of violation of the open/close principle), but the switch will be significantly cheaper.
You can also use dynamic, which causes the runtime to do late binding:
dynamic d = g;
ToJson(d); // Picks the right ToJson overload corresponding to the runtime type of 'd'
... although, dynamic has a fairly large runtime cost, and is generally considered smell.
Theswitchis using some pattern matching? I wasn't aware switch statements in C# could do that! :)
– Nick Bull
Mar 27 at 14:48
1
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
1
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
add a comment |
Without being able to modify the base class, or have access to the concrete type before it's turned into a generic Graphics type, unfortunately I don't think there's anything you can do except inspect the runtime type of the Graphics object.
You can use a switch statement (since C# 7.0), which is slightly cleaner than your if chain:
switch (g)
case Circle circle: ... break;
case Line line: ... break;
default: /* Oh no! */ break;
Personally I don't see much advantage in using a Dictionary over a switch statement like this - both can be put away into a little self-contained method (and so reduce the amount of violation of the open/close principle), but the switch will be significantly cheaper.
You can also use dynamic, which causes the runtime to do late binding:
dynamic d = g;
ToJson(d); // Picks the right ToJson overload corresponding to the runtime type of 'd'
... although, dynamic has a fairly large runtime cost, and is generally considered smell.
Without being able to modify the base class, or have access to the concrete type before it's turned into a generic Graphics type, unfortunately I don't think there's anything you can do except inspect the runtime type of the Graphics object.
You can use a switch statement (since C# 7.0), which is slightly cleaner than your if chain:
switch (g)
case Circle circle: ... break;
case Line line: ... break;
default: /* Oh no! */ break;
Personally I don't see much advantage in using a Dictionary over a switch statement like this - both can be put away into a little self-contained method (and so reduce the amount of violation of the open/close principle), but the switch will be significantly cheaper.
You can also use dynamic, which causes the runtime to do late binding:
dynamic d = g;
ToJson(d); // Picks the right ToJson overload corresponding to the runtime type of 'd'
... although, dynamic has a fairly large runtime cost, and is generally considered smell.
edited Mar 27 at 15:05
answered Mar 27 at 14:47
canton7canton7
7,7351 gold badge19 silver badges31 bronze badges
7,7351 gold badge19 silver badges31 bronze badges
Theswitchis using some pattern matching? I wasn't aware switch statements in C# could do that! :)
– Nick Bull
Mar 27 at 14:48
1
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
1
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
add a comment |
Theswitchis using some pattern matching? I wasn't aware switch statements in C# could do that! :)
– Nick Bull
Mar 27 at 14:48
1
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
1
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
The
switch is using some pattern matching? I wasn't aware switch statements in C# could do that! :)– Nick Bull
Mar 27 at 14:48
The
switch is using some pattern matching? I wasn't aware switch statements in C# could do that! :)– Nick Bull
Mar 27 at 14:48
1
1
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
@Nick Yes, since C# 7.0
– canton7
Mar 27 at 14:49
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
Oh man, it's been a while. I last used C# when it was at 5.0, I feel old
– Nick Bull
Mar 27 at 14:52
1
1
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
To be fair, nothing ground-breaking has happened since 5.0 (apart from Roslyn) - it's been lots of little improvements. C# 8 is going to be ground-breaking again, though
– canton7
Mar 27 at 14:54
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55379942%2foverloading-method-without-modifying-classes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you could modify the types, you would add a virtual/abstract ToJson instance method and override as needed. A "rule engine" where you use reflection to find the right implementation of the ToJson code is probably the best way to go.
– Lasse Vågsæther Karlsen
Mar 27 at 14:42
So yes, a dictionary of type to delegate/object that implements this is probably what I would choose myself.
– Lasse Vågsæther Karlsen
Mar 27 at 14:43
Decorator pattern?
– Kenneth K.
Mar 27 at 14:43
1
As you note, to use the visitor pattern for double dispatch, you need the "visited" class to implement a method that calls the "visitor" class back on the appropriate method. I give a simple example here: ericlippert.com/2015/05/04/wizards-and-warriors-part-three You normally use the visitor pattern when you have a group of related objects, you have a variety of analysis or transformation passes you need to perform on those objects, and the exact details of each operation depend on the run-time type of both the visited and visitor objects.
– Eric Lippert
Mar 27 at 15:26
1
Thank you for your input Eric - I'll give that a read! 🙂
– Nick Bull
Mar 27 at 17:07