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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:33:57+00:00 2026-06-13T08:33:57+00:00

Let’s say I have the following class hierarchy (base interface included): IAction -> (abstract)

  • 0

Let’s say I have the following class hierarchy (base interface included):

IAction -> (abstract) BaseAction -> (concrete) ImmediateAction -> (concrete) MovementAction

Now, let’s say IAction exposes a method (well, really a different interface that IAction implements does but let’s keep things simple here!):

// Returns a new IAction instance deep copied from the current instance.
IAction DeepClone();

Good so far? We have our deep copy method, and ImmediateAction has some properties that it wants copied so it will provide not only an implementation of DeepClone(), but a copy constructor as well:

//Base Action implementation
protected BaseAction(BaseAction old)
{
    this.something = old.something;
}

//Immediate Action Implementation
protected ImmediateAction(ImmediateAction old)
   : base(old)
{
    this.anything = old.anything;
}

public IAction DeepClone()
{
    return new ImmediateAction(this);
}

Now, let’s say MovementAction doesn’t have anything inside of it that’s relevant in a DeepClone() at all, so it doesn’t implement the method or a copy constructor.

The problem that I’m having is this:

IAction x = new MovementAction();
IAction y = x.DeepClone();

//pleaseBeTrue is false
bool pleaseBeTrue = y is MovementAction;

Now, I understand what’s going on here – MovementAction does not implement DeepClone(), so ImmediateAction.DeepClone() is called instead, which instantiates a new ImmediateAction. Hence, the type of y in the above example is ImmediateAction instead of MovementAction.

So, after this lengthy preamble, my question is this: what is the best practice for this type of situation? Am I stuck? Do I simply have to implement a DeepClone() method no matter what for every class along the hierarchy? Is the pattern that I am using here incorrect, and there’s a better way?

One final note: I would like to avoid reflection if at all possible.

  • 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-13T08:33:58+00:00Added an answer on June 13, 2026 at 8:33 am

    Could use an extension method and do incremental cloning

    public static class DeepCopyExt
    {
        public static T DeepCopy<T>(this T item)
            where T : ThingBase, new()
        {
            var newThing = new T();
            item.CopyInto(newThing);
            return newThing;
        }
    }
    
    public abstract class ThingBase
    {
        public int A { get; set; }
    
        public virtual void CopyInto(ThingBase target)
        {
            target.A = A;
        }
    }
    
    public class ThingA : ThingBase
    {
    }
    
    public class ThingB : ThingA
    {
        public int B { get; set; }
    
        public override void CopyInto(ThingBase target)
        {
            var thingB = target as ThingB;
    
            if(thingB == null)
            {
               throw new ArgumentException("target is not a ThingB");
            }
    
            thingB.B = B;
            base.CopyInto(thingB);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var b = new ThingB
            {
                A = 1,
                B = 3
            };
    
            //c is ThingB
            var c = b.DeepCopy();
    
            var b1 = new ThingA
            {
                A = 1,
            };
    
            //c1 is ThingA
            var c1 = b1.DeepCopy();
    
            Debugger.Break();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following structure: abstract class Hand {} class Rock extends
Let's say I have the following classes : public class MyProductCode { private String
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a class hierarchy. I could theoretically maintain its functionality with
Let me explain best with an example. Say you have node class that can
Let's say I have a custom class CustomClass, and I have a collection deriving
Let's say I have the following data frame: > myvec name order_no 1 Amy
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>

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.