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 895025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:29:32+00:00 2026-05-15T14:29:32+00:00

I have a set of classes which I didn’t write, and they’re read only.

  • 0

I have a set of classes which I didn’t write, and they’re read only.
Let’s say, for the example, that those classes are the following ones:

public class Base { }
public class B : Base { }
public class C : B { }
public class D : Base { }

I want to add a method Foo() on all these classes, I am using extension methods:

public static class Extensions {

  public static void Foo(this Base obj) {
      dynamic dynobj = obj;
      try {
        dynobj.Foo();
      }
      catch (RuntimeBinderException ex) {
          Console.WriteLine(ex.Message);
      }
  }

  public static void Foo(this B b) {
      Console.WriteLine("Foo in B");
  }

  public static void Foo(this C c) {
      Console.WriteLine("Foo in C");
  }

}

As you can see, I’m trying to use the keyword dynamic, expecting it know the real type of my object and call its Foo() method. But… dynobj.Foo() always fails.

static void Main(string[] args) {
    List<Base> list = new List<Base>();
    list.Add(new B());
    list.Add(new C());
    list.Add(new D());

    list.ForEach(x => x.Foo());
}

I know I could use the Adaptor pattern, but I really have too many classes.

Is it a good idea to do that with dynamic? Is it possible to make this work?

Thank you.

  • 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-05-15T14:29:33+00:00Added an answer on May 15, 2026 at 2:29 pm

    I sort of resolved my problem by keeping only Base’ Foo() extension method and making the other extensions methods regular static methods taking B, C or D as a parameter.

     public static void Foo(this Base obj) {
          try {
            Foo_(obj as dynamic);
          }
          catch (RuntimeBinderException ex) {
              Console.WriteLine(ex.Message);
          }
      }
    
      private static void Foo_(B b) {
          Console.WriteLine("Foo in B");
          b.ListOfBs.ForEach(x => x.Foo());
      }
    
      private static void Foo_(C c) {
          Console.WriteLine("C.Name = {0}", c.Name);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes which have automatic properties only like public customerName {get; set;}. They
I have a set of classes which hold only data (no operations have been
I have a set of some classes which are all capable of being constructored
I have a set of XSDs from which I generate data access classes, stored
If have a set of classes that all implement an interface. interface IMyinterface<T> {
I have a requirement where in i have a set of classes and they
I'm using ASP.NET MVC RC2. I have a set of classes that were auto-generated
I have a set of callback classes that I use for handling callbacks with
I have written a set of classes and interfaces that are implemented in Moose
I have a set of core, complicated JavaScript data structures/classes that I'd like to

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.