简体   繁体   English

C#Constructor重载:new object.FromOtherObject()?

[英]C# Constructor overloads: new object.FromOtherObject()?

I have two different types of nodes, I need to be able to convert between them in a relatively easy fashion. 我有两种不同类型的节点,我需要能够以相对简单的方式在它们之间进行转换。 I was thinking of doing this in the constructor as it would make code a lot cleaner. 我正在考虑在构造函数中执行此操作,因为它会使代码更清晰。

NodeA nodea = new NodeA();

NodeB nodeb = new NodeB.FromNodeA(nodea);

I've been googling for a couple hours and haven't been able to find a way to do this. 我一直在谷歌搜索几个小时,但未能找到办法做到这一点。 The best solution I've come up with is; 我想出的最好的解决方案是;

public NodeB() { }

public static NodeB FromNodeA(NodeA theNodeA)
{
    NodeB b = new NodeB();
    b.Value = theNodeA.value;
    // etc..
}

But this makes code look a little more funky like so; 但是这使代码看起来更像一个时髦的东西;

NodeB b = NodeB.FromNodeA(nodea);

Ideally I'd like to be able to do this: 理想情况下,我希望能够这样做:

NodeB b = new NodeB().FromNodeA(nodea);

Edit; 编辑; Or is the static method I've already done the more (traditionally) correct way of accomplishing this? 或者是静态方法我已经做了更多(传统上)正确的方法来完成这个?

You could just have a constructor with a NodeA parameter: 你可以拥有一个带有NodeA参数的构造函数:

NodeB nodeB = new NodeB(nodeA);

... but having a factory method is also a pretty idiomatic solution. ...但是采用工厂方法也是一种非常惯用的解决方案。 It has various benefits, including being able to have multiple methods with different names but the same parameter types - eg 它具有各种好处,包括能够使用具有不同名称但具有相同参数类型的多个方法 - 例如

TimeSpan.FromSeconds(...)
TimeSpan.FromMinutes(...)

It also allows the method more flexibility: 允许该方法更灵活:

  • It could return null in some cases (eg for null input) 在某些情况下它可能返回null(例如,对于null输入)
  • It could return a reference to an existing object 它可以返回对现有对象的引用
  • It could return an instance of a subclass (eg Encoding.GetEncoding ) 它可以返回子类的实例(例如Encoding.GetEncoding
  • You can convert the method group to a delegate, which is handy for LINQ operations (no need for a lambda expression) 您可以将方法组转换为委托,这对LINQ操作很方便(不需要lambda表达式)

One upside of making it a constructor is that if you have a subclass of NodeB , that could have a constructor which also takes a NodeA and chains to the NodeB(NodeA) constructor. 使它成为构造函数的一个好处是,如果你有一个NodeB的子类,那么它可以有一个构造函数,它NodeA和链带到NodeB(NodeA)构造函数。 It's hard to achieve the same thing using the factory method approach without code duplication. 在没有代码重复的情况下使用工厂方法方法很难实现相同的功能。

And as per Alessandro's comment, you could have a user-defined conversion operator, either explicit or implicit. 根据Alessandro的评论,您可以拥有一个用户定义的转换运算符,无论是显式还是隐式。 I would be very careful with implicit conversions though - they can feel like a great idea at the time, but end up leading to code which is unclear later. 我会对隐式转换非常小心 - 当时它们可能感觉像个好主意,但最终会导致代码在以后不清楚。

Finally, you could also have a ToNodeB() method on NodeA instead - either implemented directly, or as an extension method. 最后,你也可以在NodeA上使用ToNodeB()方法 - 直接实现,或者作为扩展方法。

So to round it all up, here are the options for how the "user code" might look: 所以为了解决这个问题,以下是“用户代码”的外观选项:

NodeA nodeA = ...;
// One of...
NodeB nodeB = nodeA;                  // Implicit conversion
NodeB nodeB = (NodeB) nodeA;          // Explicit conversion
NodeB nodeB = new NodeB(nodeA);       // Constructor
NodeB nodeB = NodeB.FromNodeA(nodeA); // Factory method
NodeB nodeB = nodeA.ToNodeB();        // Instance or extension method on NodeA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM