c# -传递具体构造方法来创建'child'类

本文关键字:创建 child 构造方法 | 更新日期: 2023-09-27 18:11:57

我有一个类X,它使用了一个类Y。X创建了Y,但是X必须使用与创建传递给X的实例Y相同的构造函数方法来创建Y。

  • 它不是克隆,因为我想要一个NEW对象- y不等于传递给x的实例- y的值
  • 它不是一个实例,因为我不想要同样的对象- y作为实例- y传递给x。

我想把类Y的"构造函数方法和参数"传递给类X,并根据这些信息,使用传递的方法创建新的Y实例。

我不想开发所有的'Class Y'构造函数逻辑,因为在这种情况下,它们都将是高度耦合的。

我做了一个小尖峰来更好地解释我自己。

谢谢。

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);
            }        
        }
    }
}

=========<解决方案strong> :将这个构造函数添加到TheFather类中:

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

在这个例子中:

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);
        }

像魅力一样工作....: -)

谢谢…

c# -传递具体构造方法来创建'child'类

您可以简单地使用工厂来创建TheSon对象:

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

这样你每次都可以得到一个新对象,但是以完全相同的方式创建(这不仅限于构造函数;您还可以包括任何您想要的附加代码)。像这样使用:

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

Update:如果你想在TheFather中创建TheSon,你也可以改变TheFather的构造函数来接受一个工厂。例如:

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

var father = new TheFather(factory);