C# - 传递具体构造函数方法来创建“子项”类

发布于 2024-12-11 03:19:22 字数 2753 浏览 0 评论 0原文

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

  • 它不是克隆,因为我想要一个新对象-Y 不等于传递给 X 的实例 Y 的值。
  • 它不是实例,因为我不希望将相同的对象 Y 传递给 X。

我想传递“构造函数方法和参数” ” 从 Y 类到 X 类并根据此信息,使用 ctor-method-passed 创建新的 Y 实例。

我不想开发所有“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);
            }        
        }
    }
}

=========解决方案: 将此构造函数添加到 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);
        }

工作起来就像一个魅力.... :-)

谢谢...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2024-12-18 03:19:22

您可以简单地使用工厂来创建 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

更新:如果您想在内创建TheSon,您还可以更改TheFather的构造函数以接受工厂父亲。例如:

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

var father = new TheFather(factory);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文