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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:02+00:00 2026-05-26T13:13:02+00:00

I have a Class X wich uses a Class Y. The X creates the

  • 0

I have a Class X wich uses a Class Y. The X creates the Y, but X must create Y with THE SAME constructor method was used to create instance-Y passed to X.

  • It is not a Clone, because I want a NEW object-Y not equals to values of instance-Y passed to X.
  • It is not a instance because I do not want the SAME object-Y what is pased as instance-Y to X.

I would like to pass the “constructor method and parameters” of class Y to class X and, with this information, create the new Y-instance using the ctor-method-passed.

And I don’t want to devel all ‘Class Y’ constructor logic because, in this case both of them will be very highly coupled.

I have done a little spike to explain myself a bit better.

Thanks.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TheSon son1 = new TheSon();

            son1.TheValue = "Another Value";

            TheFather<TheSon> father1 = new TheFather<TheSon>(son1);
            Console.WriteLine("Result is {0}:", "Empty constructor".Equals(father1.MySon.TheValue));
            Console.WriteLine("\tbecause prop. must be: '{0}' and it is: '{1}'", "Empty constructor", father1.MySon.TheValue);

        }

        public class TheFather<T> where T: TheSon
        {
            public TheSon MySon { get; set; }

            public TheFather(T mySon) {
                // I would like to NOT use the same object but
                // use the constructor that was used to build the passed object-instance.
                // 
                // Or perhaps pass a concrete TheSon constructor to the 'TheFather'...
                this.MySon = (TheSon)mySon;
            }
        }

        public class TheSon 
        {
            public string TheValue { get; set; }

            public TheSon()
            {
                this.TheValue = "Empty constructor";
            }

            public TheSon(string value)
            {
                this.TheValue = value;
            }

            public TheSon(string value, int element)
            {
                this.TheValue = value + "-> " + Convert.ToString(element);
            }        
        }
    }
}

=========SOLUTION:
Adding this constructor to the TheFather class:

public TheFather(Func<T> sonFactory)
        {
            this.MySon = (T)sonFactory();
        }

And with this example:

static void Main(string[] args)
        {
            // Uncomment one of this to change behaviour....
            //Func<TheSon> factory = () => new TheSon();
            Func<TheSon> factory = () => new TheSon("AValue");
            //Func<TheSon> factory = () => new TheSon("AValue", 1);

            TheFather<TheSon> father1 = new TheFather<TheSon>(factory);
            Console.WriteLine("Result is {0}:", "AValue".Equals(father1.MySon.TheValue));
            Console.WriteLine("\tbecause prop. must be: '{0}' and it is: '{1}'", "AValue", father1.MySon.TheValue);
        }

Works like a charm…. 🙂

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. Editorial Team
    Editorial Team
    2026-05-26T13:13:03+00:00Added an answer on May 26, 2026 at 1:13 pm

    You can simply use a factory to create TheSon objects:

    Func<TheSon> factory = () => new TheSon(); // creates one with default ctor
    

    This way you can get a new object each time, but created in exactly the same way (this is not limited to a constructor; you can also include any additional code you want). Use it like this:

    var oneSon = factory(); // creates one son
    var secondSon = factory(); // creates another with the same constructor
    var father = new TheFather(factory()); // ditto
    

    Update: You can also change TheFather‘s constructor to accept a factory if you want to create TheSon inside TheFather. For example:

    public TheFather(Func<T> sonFactory) {
        this.MySon = (TheSon)sonFactory();
    }
    

    and

    var father = new TheFather(factory);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you have a surfaceView class, used for animation/game/... you have the onTouchEvent() method
I have class method that returns a list of employees that I can iterate
I have a movie with a document class (Main.as) wich load 2 SWF: private
Sup Guys, I Have a Function on my frmMain Class wich will update my
I have an Abstract Domain Class in Grails wich has fields that all the
I have web application wich uses jsf 2.0 and spring 3.0 The problem is
I have a Shape class wich contains public Pen outlinePen; What I try to
i have done as Vdex suggested here: https://stackoverflow.com/a/5801502/973485 And used the RenderPartialToString method he
I have a Repositry class wich initiates like this: public ContactRepository(string sqlStr, string username)
in my php script at the top i have require_once('../registration/include/membersite_config.php'); wich is a class

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.