Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 182287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:54:47+00:00 2026-05-11T14:54:47+00:00

Assume we have legacy classes, that can’t be modified: class Foo { public void

  • 0

Assume we have legacy classes, that can’t be modified:

class Foo  {     public void Calculate(int a) { } }  class Bar {     public void Compute(int a) {} } 

I want to write a helper with such signature:

void Calc(object obj, int a); 

Notice, that the first argument is of type ‘object’. The test code should be some like this:

ExampleX.Calc((object)new Foo(), 0); ExampleX.Calc((object)new Bar(), 0); 

The question is, what implementation you can imagine in addition to these:

// Using If/then class Example1 {     public static void Calc(object obj, int a)     {         if (obj is Foo)             ((Foo)obj).Calculate(a);         else if (obj is Bar)             ((Bar)obj).Compute(a);     } }  // Using reflection class Example2 {     private static Dictionary<Type, MethodInfo> _methods = new Dictionary<Type, MethodInfo>();      static Example2()     {         _methods.Add(typeof(Foo), typeof(Foo).GetMethod('Calculate'));         _methods.Add(typeof(Bar), typeof(Bar).GetMethod('Compute'));     }      public static void Calc(object obj, int a)     {         _methods[obj.GetType()].Invoke(obj, new object[] { a });     } }  // Using delegates class Example3 {     private delegate void CalcDelegate(object obj, int a);      private static Dictionary<Type, CalcDelegate> _methods = new Dictionary<Type, CalcDelegate>();      static Example3()     {         _methods.Add(typeof(Foo), (o, a) => ((Foo)o).Calculate(a));         _methods.Add(typeof(Bar), (o, a) => ((Bar)o).Compute(a));     }      public static void Calc(object obj, int a)     {         _methods[obj.GetType()](obj, a);     } }  // Using Reflection + overloaded methods class Example4 {     private delegate void CalcDelegate(object obj, int a);      public static void Calc(object obj, int a)     {         Type[] types = new Type[] {              obj.GetType(), typeof(int)         };          typeof(Example4).GetMethod('Calc', types).Invoke(null, new object[] { obj, a });     }      public static void Calc(Foo obj, int a)     {         obj.Calculate(a);     }      public static void Calc(Bar obj, int a)     {         obj.Compute(a);     } } 

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:54:47+00:00Added an answer on May 11, 2026 at 2:54 pm

    I would go for example 1, because its the simplest one and most obvious.

    I would use Example 2 only if you expect new types of objects with one of these two methods, and example 3 only if you have a lot (tens if not hundreds) of objects and performance starts being an issue.

    Edit: Or extension methods if you are .Net 3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the build pane for the target, set Header Search… May 11, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer make sure that you are initializing the view in onFinishInflate… May 11, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer You could take a bounding box of the polygon (min-max… May 11, 2026 at 8:21 pm

Related Questions

Assume we have legacy classes, that can't be modified: class Foo { public void
It's my understanding that nulls are not indexable in DB2, so assuming we have
I am preparing for the development of an enterprise-style application for a very small
I'm currently deciding on a platform to build a scientific computational product on, and
Is there any real issue - such as performance - when the hibernate object

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.