“运行时覆盖方法” C#

发布于 2024-10-01 02:43:53 字数 192 浏览 5 评论 0原文

我想在自定义 dll 中重写 ClassA 的 Print 方法。

public ClassA
{
    public void Print( string arg1, string arg2, string arg3, string arg4 )
    {
    }
}

这在 C# 中可能吗?

I want to override method Print of a ClassA in a custom dll.

public ClassA
{
    public void Print( string arg1, string arg2, string arg3, string arg4 )
    {
    }
}

Is this possible in C# ?

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

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

发布评论

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

评论(3

︶ ̄淡然 2024-10-08 02:43:53

我相信微软研究院的 Moles 也做了类似的事情。他们有一个系统,允许您覆盖例如 DateTime.Now 的工作,强制它返回特定的日期/时间。

看看 http://research.microsoft.com/en-us /projects/pex/default.aspx 了解更多信息。

I believe Moles from Microsoft Research does something similar. They have a system that allows you to override the working of e.g. DateTime.Now, forcing it to return a specific date/time.

Have a look at http://research.microsoft.com/en-us/projects/pex/default.aspx for more information.

z祗昰~ 2024-10-08 02:43:53

这与您所要求的不太一样,但它达到了类似的效果...

为什么不为您的操作定义一个接口。 ClassA 实现该接口。您的自定义策略也实现该接口。 ClassA 在启动时(实例化 ClassA 时)在内部创建接口的“默认”实现,但也有一个允许设置接口的属性。该接口甚至可以允许自定义策略指定它实际实现的接口的哪些成员:

interface IStrategy
{
  void Operation1(int x, int y);
  void Operation2(string a, string b);
}

class ClassA : IStrategy
{
  private IStrategy builtInStrategy = new BuiltInStrategy();

  public IStrategy CustomStrategy { get; set; }

  void Operation1(int x, int y);
  {
    if (CustomStrategy != null)
    {
      CustomStrategy.Operation1(x, y);
    }
    else
    {
      builtInStrategy.Operation1(x, y);
    }
  }

  void Operation2(string a, string b)
  {
    if (CustomStrategy != null)
    {
      CustomStrategy.Operation2(a, b);
    }
    else
    {
      builtInStrategy.Operation2(a, b);
    }
  }
}

您可以将自定义策略指定为 IStrategy 接口的一部分,以指示它不会“覆盖”特定操作。也许它可以返回一个 bool 而不是 void,或者也许每个操作都可以有一个 out bool 参数,如果自定义策略未覆盖某个操作,该参数将设置为 false。

根据可以覆盖的操作数量,您甚至可以考虑将每个操作置于其自己的接口中。如果在不实现某些其他操作的情况下实现一个操作是不合理的,则可以将操作分组到一个接口中。

interface IOperation1
{
  void Operation1(int x, int y);
}

interface IOperation2
{
  void Operation2(string a, string b);
}

interface IMath
{
  int Add(int i, int j);
  int Subtract(int i, int j);
  int Multiply(int i, int j);
  int Divide(int i, int j);
}

interface IStrategy
{
  //What operations should the Strategy have?
}

class ClassA : IOperation1, IOperation2, IMath
{
  public IStrategy CustomStrategy { get; set; }

  public void Operation1(int x, int y)
  {
    IOperation1 op1 = CustomStrategy as IOperation1;
    if (op1 != null)
    {
      op1.Operation1(x, y);
    }
    else
    {
      //Do ClassA's Operation1 logic here
    }
  }

  public void Operation2(string a, string b)
  {
    IOperation2 op2 = CustomStrategy as IOperation2;
    if (op2 != null)
    {
      op2.Operation2(a, b);
    }
    else
    {
      //Do ClassA's Operation2 logic here
    }
  }

  //
  // And so on ...
  //
}

This is not quite the same thing as you are asking, but it achieves a similar effect...

Why not define an interface for your operations. ClassA implements the interface. Your custom Strategies also implement the interface. ClassA internally creates the "default" implementation of the interface at startup (when ClassA is instantiated), but also has a property that allows the interface to be set. The interface might even allow the custom Strategy to specify which members of the interface that it actually implements:

interface IStrategy
{
  void Operation1(int x, int y);
  void Operation2(string a, string b);
}

class ClassA : IStrategy
{
  private IStrategy builtInStrategy = new BuiltInStrategy();

  public IStrategy CustomStrategy { get; set; }

  void Operation1(int x, int y);
  {
    if (CustomStrategy != null)
    {
      CustomStrategy.Operation1(x, y);
    }
    else
    {
      builtInStrategy.Operation1(x, y);
    }
  }

  void Operation2(string a, string b)
  {
    if (CustomStrategy != null)
    {
      CustomStrategy.Operation2(a, b);
    }
    else
    {
      builtInStrategy.Operation2(a, b);
    }
  }
}

You could specify as part of the IStrategy interface a way for the custom Strategy to indicate that it is not "overriding" a particular operation. Perhaps it could return a bool rather than a void or perhaps each operation could have an out bool parameter that is set to false if the custom Strategy has not overridden an operation.

Depending on how many operations can be overridden, you might even consider putting each operation its own interface. Operations could be grouped in an interface if it is not reasonable to implement one operation without also implementing some other operations.

interface IOperation1
{
  void Operation1(int x, int y);
}

interface IOperation2
{
  void Operation2(string a, string b);
}

interface IMath
{
  int Add(int i, int j);
  int Subtract(int i, int j);
  int Multiply(int i, int j);
  int Divide(int i, int j);
}

interface IStrategy
{
  //What operations should the Strategy have?
}

class ClassA : IOperation1, IOperation2, IMath
{
  public IStrategy CustomStrategy { get; set; }

  public void Operation1(int x, int y)
  {
    IOperation1 op1 = CustomStrategy as IOperation1;
    if (op1 != null)
    {
      op1.Operation1(x, y);
    }
    else
    {
      //Do ClassA's Operation1 logic here
    }
  }

  public void Operation2(string a, string b)
  {
    IOperation2 op2 = CustomStrategy as IOperation2;
    if (op2 != null)
    {
      op2.Operation2(a, b);
    }
    else
    {
      //Do ClassA's Operation2 logic here
    }
  }

  //
  // And so on ...
  //
}
千寻… 2024-10-08 02:43:53

您的第一个问题:该方法未标记为虚拟的。您不能重写非虚拟方法。

至于问题,这完全取决于ClassA是如何实例化和使用的。例如,如果您可以控制派生类型的实例化和使用,您可以在运行时创建派生类型。

Your first problem: the method is not marked virtual. You cannot override a non-virtual method.

As for the question, it all depends how ClassA is instantiated and used. You could for example create a derived type at runtime, if you can control the instantiation and usage of it.

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