在 .NET 1.1 中扩展密封类的干净/直观的方法是什么?

发布于 2024-07-26 01:11:06 字数 805 浏览 7 评论 0原文

我正在构建一个将在产品的多个版本中使用的库,其工作的一部分是增强 .NET 1.1 中不存在的一些功能,因为我们需要使用它。 最好的例子可能是 String.IsNullOrEmpty 方法,我们相当依赖它的功能。

.NET 中的 String 类是密封的; 我考虑过使用适配器或装饰器模式通过包装原始字符串来扩展类的功能,但我不相信这对于我想要做的事情来说是一种非常直观的方法。 我还查看了 StackOverflow 上的另一篇文章关于类似的问题,但它再次提出了我刚才提到的问题。

我绝对可以使用 Adapter 来创建我正在寻找的功能,但我无法想象对每个字符串都这样做:

bool isEmpty = new StringExtensionAdapter(myXmlNode.SelectSingleNode(myXpathString)).IsNullOrEmpty();
if (isEmpty)
{
    // Do something
}

我不一定不喜欢这种方法,但它似乎会导致大量的内存管理,其中不同的解决方案可能会更好。 另外,我并不热衷于创建“StringUtility”库,因为这种方法使我远离了我想要遵循的面向对象的设计。

任何意见都会有帮助; 如果我只是疯了并且应该使用其中一种方法,请告诉我。 我更多的是寻找最佳实践而不是解决方案,因为我有几个。

I'm in the process of architecting a library that will be used in multiple releases of a product, and part of its job is to augment some of the functionality that doesn't exist in .NET 1.1, since we're required to use it. Probably the best example of this is the String.IsNullOrEmpty method, whose functionality we rely on fairly heavily.

The String class in .NET is sealed; I've thought about using either the Adapter or Decorator patterns to extend the functionality of the class by wrapping the original string, but I'm not convinced that this is a very intuitive approach for what I'm trying to do. I've also looked at another post on StackOverflow about a similar question, but it again raises the issue I just mentioned.

I could definitely use Adapter to create the functionality I'm looking for, but I can't imagine doing this for every string:

bool isEmpty = new StringExtensionAdapter(myXmlNode.SelectSingleNode(myXpathString)).IsNullOrEmpty();
if (isEmpty)
{
    // Do something
}

I don't necessarily dislike this approach, but it seems like it results in a lot of memory management where a different solution might be better. Also, I'm not crazy about creating a "StringUtility" library since that approach takes me further away from the object-oriented design I'd like to follow.

Any input would be helpful; if I'm just crazy and should use one of these approaches, let me know. I'm searching more for best practices than a solution, since I have several.

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

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

发布评论

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

评论(4

青衫负雪 2024-08-02 01:11:06

我个人更喜欢带有静态 IsNullOrEmpty 方法的“StringUtility”库。 本质上,您将创建扩展方法,而无需良好的调用语法他们。

I would prefer the "StringUtility" library with a static IsNullOrEmpty method personally. In essence you would be creating extension methods without the nice syntax for calling them.

习ぎ惯性依靠 2024-08-02 01:11:06

根据定义,不存在干净的方法。 我认为适配器方法是 .NET 1.1 中最好的方法。 当然,这需要你在与外界打交道时来来回回。

By definition, there is no clean approach. I think the adapter approach is the best you can do in .NET 1.1. Of course, this will require you to go back and forth when dealing with the outside world.

白芷 2024-08-02 01:11:06

我同意马修的观点,没有干净的方法。 装饰器模式不适用,因为它依赖于继承(或者至少是多态性),并且您无法装饰 System.String,因为您无法创建具有相同接口的类(因为它是密封的)。

适配器基本上用于将一个接口适配到另一个接口。 这并不是您真正想要在这里做的事情。 本质上,您想到的似乎是一个包装器,但正如马修指出的那样,您将不得不来回转换很多。

也许您可以将新类型命名为“Text”并实现与字符串之间的隐式转换,以最大程度地减少需要编写的强制转换数量。 如果您选择该路线,请确保将类型设计为不可变类型,因为这与字符串本身的行为相同。

I agree with Matthew that there is no clean approach. The Decorator pattern is not applicable, since it relies on inheritance (or, at least, polymorphism), and you can't Decorate System.String, since you can't create a class that has the same interface (since it's sealed).

Adapter is basically used to Adapt one interface to another. That's not really what you want to do here. Essentially, what you seem to have in mind is a Wrapper, but as Matthew points out, you will have to convert back and forth a lot.

Perhaps you could name your new type "Text" and implement implicit conversions to and from string to minimize the amount of casts you will need to write. If you choose that route, make sure that you design your type as an immutable type, because that's the same behavior as strings themselves.

迷爱 2024-08-02 01:11:06

您可以使用隐式运算符使事情变得更加“自然”:

public class SuperString
{
    public SuperString(string s) { S = s; }

    public static implicit operator SuperString(string s)
    {
        return new SuperString(s);
    }

    public string S { get; private set; }

    public bool IsNot() { return String.IsNullOrEmpty(S); }
}

[TestMethod]
public void Test_SuperString()
{
    SuperString ss = "wee";
    SuperString xx = "";
    if (xx.IsNot()) ss = "moo";
    System.Console.WriteLine(ss.S);
}

You can use an implicit operator to make things more "natural":

public class SuperString
{
    public SuperString(string s) { S = s; }

    public static implicit operator SuperString(string s)
    {
        return new SuperString(s);
    }

    public string S { get; private set; }

    public bool IsNot() { return String.IsNullOrEmpty(S); }
}

[TestMethod]
public void Test_SuperString()
{
    SuperString ss = "wee";
    SuperString xx = "";
    if (xx.IsNot()) ss = "moo";
    System.Console.WriteLine(ss.S);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文