继承的 .Net 类 ToString 不重写

发布于 2024-09-28 12:26:14 字数 1065 浏览 2 评论 0原文

我从我称为“DataTypes”的基础创建了一个继承类“StorageMedium”。 StorageMedium有两个属性,Name()和Capacity()。

在 DataTypes 对象(类库中的所有其他对象均继承自该对象)中,我抑制了 Equals、ReferenceEquals、GetHashCode、ToString 和 GetType 函数,因此这些函数在 Visual Studio 编辑器中不可浏览。

目的是因为类库最终将由用户而不是“程序员”使用,并且我想隐藏他们可能遇到的任何不必要的代码或函数。

我有第二个类“创建”StorageMedium 的实例:

    Shared ReadOnly Property DVD() As StorageMedium
        Get
            Return New StorageMedium(NewMedium.DVD)
        End Get
    End Property

    Shared ReadOnly Property CD() As StorageMedium
        Get
            Return New StorageMedium(NewMedium.CD)
        End Get
    End Property

在我的网页上,我想调用创建类并创建 StorageMedium 的实例,并将名称和容量显示为带有名称和容量的字符串

  Response.Write(StorageMedium.Utils.DVD)
  DVD: 4.7Gb

但是,当我使用 Response.Write 方法,它显示完整的类名

  Response.Write(StorageMedium.Utils.DVD)
  LC.Utils.Convert.Computer.DataType.StorageMedium

可以公平地假设这可能是由基本对象函数的抑制引起的,但是,有没有办法“重新连接”或重新创建默认函数来利用 ToString无需创建“ToOutput”等属性即可实现功能 按要求显示对象?

谢谢。

I have created an inherited class "StorageMedium" from a base I called "DataTypes".
StorageMedium has two properties, Name() and Capacity().

In the DataTypes object, which all other objects in the class library have been inherited from, I have suppressed the Equals, ReferenceEquals, GetHashCode, ToString and GetType functions so these functions are not browsable in the Visual Studio editor.

The purpose is due to the fact the class library will be eventually used by users are not "programmers", and I want to hide any unecessary code or functions they might come across.

I have a second Class that "creates" the instances of the StorageMedium:

    Shared ReadOnly Property DVD() As StorageMedium
        Get
            Return New StorageMedium(NewMedium.DVD)
        End Get
    End Property

    Shared ReadOnly Property CD() As StorageMedium
        Get
            Return New StorageMedium(NewMedium.CD)
        End Get
    End Property

On my webpage, I want to call the creation class and create instance of the StorageMedium and display the name and capacity as a string with the name and capacity

  Response.Write(StorageMedium.Utils.DVD)
  DVD: 4.7Gb

However, when I use the Response.Write method it displays the full class name

  Response.Write(StorageMedium.Utils.DVD)
  LC.Utils.Convert.Computer.DataType.StorageMedium

It's fair to assume this is probably caused by the suppression of the basic Object functions, however, is there a way to "rehook-up" or recreate a default function to utilitise the ToString functionality without creating a property like "ToOutput" to
display the object as required?

Thanks.

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

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

发布评论

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

评论(1

醉态萌生 2024-10-05 12:26:14

您可以尝试这样做:

public class StorageMedium {
    // Other code
    public static implicit operator String(StorageMedium instance) {
        return "StorageMedium"; // Or whatever string you prefer
    }
}

尽管重写 ToString() 显然是首选。

You could try this:

public class StorageMedium {
    // Other code
    public static implicit operator String(StorageMedium instance) {
        return "StorageMedium"; // Or whatever string you prefer
    }
}

Although overriding ToString() is obviously preferred.

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