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

  • SEARCH
  • Home
  • 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 8135965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:35:36+00:00 2026-06-06T10:35:36+00:00

Let’s say I have these class hierarchy : public abstract class Parent { }

  • 0

Let’s say I have these class hierarchy :

public abstract class Parent {
}

public class A : Parent {
    public void Update() { }
}

public class B : Parent {
    public void Update() { }
}

public class C : Parent {
    public void Update() { }
    public void Update(bool force) { }

}

As you can see, all descendant of Parent have an Update method, with no parameter.

I want to create an utility class that can work with any kind of Parent object and call Update at the end of the process. I am sure the Update method will be implemented, so I wrote this code :

public class ParentUtilities {
    private static readonly Dictionary<Type, MethodInfo> g_UpdateMethods = new Dictionary<Type, MethodInfo>{
        { typeof(A), typeof(A).GetMethod("Update", new Type[] {})},
        { typeof(B), typeof(B).GetMethod("Update", new Type[] {})},
        { typeof(C), typeof(C).GetMethod("Update", new Type[] {})}
    };

    public static void DoSomething(Parent p)
    {
          CalculateTheMeaningOfTheLife(p);             

          g_UpdateMethods[p.GetType()].Invoke(p, null);
    }
}

As I don’t have control over the class hierarchy (it’s from a 3rd party assembly). I can only change the utility class. How can I avoid such tweak ?

As I’m sticked to .Net 3.5 SP1, I can’t use dynamic.

  • 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. Editorial Team
    Editorial Team
    2026-06-06T10:35:37+00:00Added an answer on June 6, 2026 at 10:35 am

    If there’s just a known, (very) small set of child classes then you could do something like this:

    public class ParentUtilities
    {
        public static void DoSomething(Parent p)
        {
            CalculateTheMeaningOfTheLife(p);
    
            var a = p as A;
            if (a != null)
            {
                a.Update();
                return;
            }
    
            var b = p as B;
            if (b != null)
            {
                b.Update();
                return;
            }
    
            var c = p as C;
            if (c != null)
            {
                c.Update();
                return;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have the following: abstract class A; class B : public A;
Let me explain best with an example. Say you have node class that can
Let's say I have the following classes : public class MyProductCode { private String
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say I have a class like this : public class MyClass { public
Let's say I have a table with a Color column. Color can have various
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress

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.