如何重写另一个程序集中的方法?

发布于 2024-07-14 21:37:37 字数 799 浏览 7 评论 0原文

我之前问过一个关于接口的问题并得到了一些很好的答复。 我真的很高兴开始开发真正灵活的代码。

我现在有一个关于重写虚拟方法的问题。

我目前正在使用社区服务器 SDK。 其中一个控件是“标签”面包屑。 因此它将列出相关对象上的“标签”。

<CSBlog:TagBreadCrumb />

我使用标签来允许管理员定位要在布局中显示的内容的特定位置,但是这些标签与最终用户无关。 例如,“Home_Main”标签用于定位主页上的主面板,但我不希望用户看到它。

由于我有社区服务器的 SDK,我找到了呈现“标签列表”的控件,并对它进行了修改以实现我想要的功能 - 但我的理解是,由于多种原因,修改核心并不好。

所以我想做的是创建我自己的控件,它基本上与基本控件几乎 100% 相同,但重写一种方法。

方法如下:

 public virtual string FormatTags(string[] tagList)

里面有用于格式化标签的代码,我想编写自己的代码来执行所有相同的操作,除了检查要排除的“标签”的配置文件。

所以我的理解是,我创建我的控件,派生自基本控件 - 并且我可以编写自己的 FormatTags() 方法 - 这是正确的吗?

我只需要更改它的一个方面,但这是否意味着我必须从基类复制该方法的原始代码 - 并更改我需要的内容? 从一个地方复制代码感觉不太正确,但我不知道在不更改基类中的代码的情况下扩展该方法的另一种方法。

感谢您的任何建议。

I asked a question about interfaces previously and got some excellent responses. I'm really excited to start developing really flexible code.

I now have a question about overriding a virtual method.

I am currently working with the Community Server SDK. One of the controls is a 'tag' bread crumb. So it will list the 'tags' on the relevant object.

<CSBlog:TagBreadCrumb />

I am using tags to allow adminstrators to target specific spots for content to appear in a layout, however those tags are not relevant to the end user. For example, 'Home_Main' tag is used to target the main panel on the home page, but I don't want user to see that.

Since I have the SDK for Community Server, I found the control which renders the 'tag list' and made modifications to it that do what I want - however my understanding is that it's not good to modify the core for many reasons.

So what I'd like to do is create my own control, which is essentially almost 100% identical to the base control, but override one method.

Here is the method:

 public virtual string FormatTags(string[] tagList)

Inside of there is code to format the tags, and I'd like to write my own code which does all of the same stuff, except checks a configuration file for 'tags' to exclude.

So my understanding is that I create my control, deriving from the base control - and I can write my own FormatTags() method - is that correct?

I only need to change one aspect of it, but does this mean I have to copy the original code for that method from the base class - and change what I need to in there? It just doesn't feel right copyign code from one spot, but I don't know of another way to extend the method without changing code in the base class.

Thanks for any suggestions.

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

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

发布评论

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

评论(5

寄与心 2024-07-21 21:37:37

在您的情况下(您想要禁止在面包屑中打印某些标签),您将在调用基本方法之前删除所有特殊标签。

因此,您的代码将类似于:

public override string FormatTags(string[] tagList)
{
  // strip special tags
  string[] newTagList = stripTags(tagList);
  return base.FormatTags(newTagList);
}

这样您就不需要了解基类如何格式化输出字符串。

如果您想修改输出字符串的格式,则不会对基本方法进行任何调用(因为该方法可能会在未来版本中更改其格式,您不希望受到影响)。

In your case (Where you want to suppress certain tags from being printed in the breadcrumbs), you would strip out all special tags before calling the base method.

So your code would look something like:

public override string FormatTags(string[] tagList)
{
  // strip special tags
  string[] newTagList = stripTags(tagList);
  return base.FormatTags(newTagList);
}

This way you don't need to understand how the base class formats the output string.

If you instead wanted to modify the format of the output string, you wouldn't make any calls to the base method (Since that method might be changing it's formatting in future releases, which you don't want to be impacted by).

木森分化 2024-07-21 21:37:37

您仍然可以运行派生方法并对其结果进行操作:

public override string FormatTags(string[] tagList) {
    string result = base.FormatTags(tagList);
    // do something with result
    return result;
}

You can still run the dervied method and operate on it's results:

public override string FormatTags(string[] tagList) {
    string result = base.FormatTags(tagList);
    // do something with result
    return result;
}
卷耳 2024-07-21 21:37:37

所以我的理解是,我创建了我的控件,派生自基本控件 - 并且我可以编写自己的 FormatTags() 方法 - 这是正确的吗?

没错; 当您重写时,您提供了自己的基类方法的实现。

在最好的情况下,会发生以下两种情况之一:

  • 有一种简单的方法可以将基类方法的输出转换为所需的输出结果。

  • 基类正在使用策略模式或其变体,以便您只需提供策略适当部分的替代实现。

不幸的是,这听起来不像是这里的情况,所以你可能必须从头开始编写它。

So my understanding is that I create my control, deriving from the base control - and I can write my own FormatTags() method - is that correct?

That's right; when you override, you provide your own implementation of a base class's method.

In the best case, one of two things happen:

  • There is an easy way to transform the output of the base class's method into the desired result.

  • The base class is using a Strategy pattern or variant thereof, so that you simply provide an alternate implementation of the appropriate portion of the Strategy.

Unfortunately, it doesn't sound like either of those is the case here, so you may have to write it from scratch.

孤芳又自赏 2024-07-21 21:37:37

所以我的理解是我创造了
我的控制,源自基础
控制 - 我可以自己写
FormatTags() 方法 - 这是正确的吗?

完全正确。

如果您发现需要复制大量代码,则应该考虑重构您的方法。 将要复制的代码放入您不会重写的另一个方法中,然后当您重写 FormatTags 时,更改您需要的内容,并仅通过调用其他方法来访问您要复制的代码。

So my understanding is that I create
my control, deriving from the base
control - and I can write my own
FormatTags() method - is that correct?

Absolutely correct.

If you're finding that you're needing to copy a lot of code around, you should look at restructuring your method. Put the code that's being copied into another method that you won't override, and then when you override FormatTags, change what you need to and just access that code you would have copied by calling the other method.

时光倒影 2024-07-21 21:37:37

查看扩展方法。 这些允许您通过在自己的静态类中定义函数来扩展无法编辑或扩展的密封类的功能。 这是我刚刚写的一个例子(可能有语法错误)。

public static class MyExtensions
{
    public static string AppendCrazyText(this string s, string crazyText)
    {
        return s + crazyText;
    }
}

现在,要访问它,我只需调用:

string myString = "Hello world";
string myCrazyText = ", lets go crazy!";

string myResult = myString.AppendCrazyText(myCrazyText);

Take a look at extension methods. These allow you to extend the capability of a sealed class that you cannot edit or extend, by defining your function in your own static class. Here's an example I've just written (may have syntax errors).

public static class MyExtensions
{
    public static string AppendCrazyText(this string s, string crazyText)
    {
        return s + crazyText;
    }
}

Now, to access this, I can simply call:

string myString = "Hello world";
string myCrazyText = ", lets go crazy!";

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