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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:30:06+00:00 2026-06-17T08:30:06+00:00

I have one abstract class named A, and other classes (B, C, D, E,

  • 0

I have one abstract class named A, and other classes (B, C, D, E, …) that implements A

I also have a list of A objects.
I’d like to be able to cast dynamicly each of the object in that list to their “base” type (ie B, C, D, …) to be able to call their constructor in an other method.

Here is what I have done for now :

abstract class A { }
class B : A { }
class C : A { }
class D : A { }
class E : A { }
// ... 

class Program
{
    static void Main(string[] args)
    {
        List<A> list = new List<A> { new B(), new C(), new D(), new E() };
        // ...

        foreach (A item in list)
        {
            A obj  = foo(item);
        }
    }

    public static A foo(A obj)
    {
        if (obj.GetType() == typeof(B))
        {
            return bar((B)obj);
        }
        else if (obj.GetType() == typeof(C))
        {
            return bar((C)obj);
        }
        // ... same for D, E, ...
        return null;
    }

    public static T bar<T>(T obj) where T : class, new()
    {
        // To use the constructor, I can't have here an abstract class.
        T newObj = new T();
        return newObj;
    }

It works, but I’d like to find an other way but to test for each class that implements A if their type equals the type of my object, and cast it afterwards.

I have nearly 15 classes like B, C, D, … and I might have more.
In order to have something simple, clear and maintainable, I’d like to avoid this methods, and the 15+ “if(…) else(…)”.

Do you see a way to do so ?

  • 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-17T08:30:07+00:00Added an answer on June 17, 2026 at 8:30 am

    Modify bar in this way:

    public static T bar<T>(T obj) where T : class
    {
        var type = obj.GetType();
        return Activator.CreateInstance(type) as T;
    }
    

    Then modify foo:

    public static A foo(A obj)
    {
        return bar(obj);
    }
    

    Note that I had to remove the new() constraint. That had to be done to avoid casting your obj inside of foo. You can check at runtime if the type has a parameterless constructor, though.

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

Sidebar

Related Questions

I have one abstract class -let's say myBase. And I want all the classes
I have one abstract class and many child classes. In child classes are from
I have two classes (MVC view model) which inherits from one abstract base class.
I currently have 2 concrete methods in 2 abstract classes. One class contains the
Let we have one abstract class: classdef ACalculation < handle methods (Abstract) [result] =
I have one parent class, which is abstract class for now, for four different
I have a very simple scenario, using NHibernate: one abstract base class animal; two
have one time consuming step that flattens a bunch of files. basically i'd like
I have an abstract class called DatabaseRow that, after being derived and constructed, is
I'm currently experiencing a problem. I created 2 classes, one is abstract, the other

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.