如何在 C# 3.5 的超类中定义强制转换运算符?

发布于 2024-09-27 04:30:41 字数 1565 浏览 3 评论 0原文

我有一个容器类,用于向标准数据类型(如 int、string 等)添加一些属性。这个容器类封装了这样一个(标准类型)对象的对象。 然后其他类使用容器类的子类来获取/设置添加的属性。现在我希望子类可以在其封装对象和自身之间隐式转换,而无需在其中添加额外的代码。

下面是我的类的一个简化示例:

  // Container class that encapsulates strings and adds property ToBeChecked
  // and should define the cast operators for sub classes, too.
  internal class StringContainer
  {
    protected string _str;

    public bool ToBeChecked { get; set; }

    public static implicit operator StringContainer(string str)
    {
      return new StringContainer { _str = str };
    }

    public static implicit operator string(StringContainer container)
    {
      return (container != null) ? container._str : null;
    }
  }

  // An example of many sub classes. Its code should be as short as possible.
  internal class SubClass : StringContainer
  {
    // I want to avoid following cast operator definition
    //    public static implicit operator SubClass(string obj)
    //    {
    //      return new SubClass() { _str = obj };
    //    }
  }

  // Short code to demosntrate the usings of the implicit casts.
  internal class MainClass
  {
    private static void Main(string[] args)
    {
      SubClass subClass = "test string"; // ERROR: Cannot convert source type 'string' to 'SubClass'

      string testString = subClass; // No error
    }
  }

我的真实容器类有两个类型参数,一个用于封装对象的类型(字符串、整数……),第二个用于子类类型(例如 SubClass)。

如何使代码可以

SubClass subClass = "test string"; // ERROR: Cannot convert source type 'string' to 'SubClass'

通过子类中的最少代码运行?

I have a container class for adding some properties to standard data types like int, string and so on. This container class encapsulate an object of such an (standard type) object.
Other classes then use sub classes of the container class for getting/setting the added properties. Now I want that the sub classes can implicitly cast between its encapsulated objects and itself without having extra code in it.

Here is a simplified example of my classes:

  // Container class that encapsulates strings and adds property ToBeChecked
  // and should define the cast operators for sub classes, too.
  internal class StringContainer
  {
    protected string _str;

    public bool ToBeChecked { get; set; }

    public static implicit operator StringContainer(string str)
    {
      return new StringContainer { _str = str };
    }

    public static implicit operator string(StringContainer container)
    {
      return (container != null) ? container._str : null;
    }
  }

  // An example of many sub classes. Its code should be as short as possible.
  internal class SubClass : StringContainer
  {
    // I want to avoid following cast operator definition
    //    public static implicit operator SubClass(string obj)
    //    {
    //      return new SubClass() { _str = obj };
    //    }
  }

  // Short code to demosntrate the usings of the implicit casts.
  internal class MainClass
  {
    private static void Main(string[] args)
    {
      SubClass subClass = "test string"; // ERROR: Cannot convert source type 'string' to 'SubClass'

      string testString = subClass; // No error
    }
  }

My real container class has two type parameters, one for the type of encapsulated object (string, int,...) and a second for the sub class type (e.g. SubClass).

How can I make the code

SubClass subClass = "test string"; // ERROR: Cannot convert source type 'string' to 'SubClass'

runnable by minimal code in sub classes?

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

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

发布评论

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

评论(1

静待花开 2024-10-04 04:30:41

我认为没有办法在基类中定义转换运算符。

基类对子类一无所知,因此它没有足够的信息来构造它。例如,如果您的 SubClass 类型只有一个需要一些参数的构造函数怎么办?基类不知道子类,因此无法以任何方式构造子类。

也许您可以使用另一种方法来参数化 StringContainer 类型。例如,您可以不使用实现继承(子类),而是将一些函数(Func<...> 类型的委托)传递给 StringContainer 类。这样,用户可以参数化该类,并且您的隐式转换仍然有效。

I don't think there is a way to define conversion operator in the base class.

The base class doesn't know anything about the sub class, so it doesn't have enough information to construct it. For example, what if your SubClass type had only a constructor that requires some arguments? The base class doesn't know about the sub class, so it cannot construct it in any way.

Maybe you could use another way to parameterize the StringContainer type. For example, instead of using implementation inheritance (sub classes), you could instead pass some functions (delegates of type Func<...>) to the StringContainer class. This way, the user could parameterize the class and your implicit conversion would still work.

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