开发者

"override method at runtime" 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# ?


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.


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 ...
  //
}


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.


This is not possible, because i can't extend ClassA because it is in the Core dll delivered with the application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜