Is it possible to extend function from library in such case?
I have a .Net library. say with 2 public functions. say one is Summmator
which takes 2 arguments and does A+B
. Another function simpleMultiplicator
which takes 2 arguments: a number to multiplicate and count of times it should be multiplicated. A simpleMultiplicator
function when called uses Summmator
function. So it is my开发者_Go百科 library. It is compiled into dll for .net4. In my programm I want to extend or modify Summator function so that when I call simpleMultiplicator it would use my modification of original Summator function. Is it possible in .net4, how to do it?
(C#, visual-C++)
It depends on how you design your classes. You state that your library exports two public functions, but they need to be defined on a class either as static or instance method, so you can make use of object-orientated principles like inheritance or polymorphism to achieve what you want.
Here is an example using inheritance:
namespace MyLibrary
{
public class MyMath
{
// Be aware of the virtual keyword which enables overriding the method
public virtual int Summmator(int a, int b)
{
return a + b;
}
public int SimpleMultiplicator(int a, int b)
{
int result = 0;
for (int i = 0; i < b; i++)
{
result = Summmator(result, a);
}
}
}
}
namespace MyProgram
{
using MyLibrary;
public class MyExtendedMath : MyMath
{
public override int Summmator(int a, int b)
{
return a + 2 * b;
}
}
public static class Program
{
public static void Main()
{
MyMath math = new MyExtendedMath();
int result = math.SimpleMultiplicator(2, 3);
Console.WriteLine(result);
}
}
}
Another way is to use polymorphism:
namespace MyLibrary
{
public interface ISummmator
{
int Summmator(int a, int b);
}
public class Summmator : ISummator
{
public int Summmator(int a, int b)
{
return a + b;
}
}
public static class MyMath
{
public static int SimpleMultiplicator(int a, int b, ISummmator summmator)
{
int result = 0;
for (int i = 0; i < b; i++)
{
result = summmator.Summmator(result, a);
}
}
}
}
namespace MyProgram
{
using MyLibrary;
public class MySummmator : ISummmator
{
public int Summmator(int a, int b)
{
return a + 2 * b;
}
}
public static class Program
{
public static void Main()
{
int result = MyMath.SimpleMultiplicator(2, 3, new MySummmator());
Console.WriteLine(result);
}
}
}
Best Regards, Oliver Hanappi
First, I'd objectify these things. So you have a Summator which provides a sum function, and a SimpleMultiplicator which provides a multiply function. Then, you add the Summator to the SimpleMultiplicator in the default case. For example,
class SimpleMultiplicator
{
public SimpleMultiplicator()
{
this.summator = new Summator();
}
public int Mult(int a, int b)
{
// ...
this.summator.sum(something, somethingelse);
// etc
}
Then you create another constructor where you can override the default Summator:
public SimpleMultiplicator(Summator summator)
{
this.summator = summator;
}
So if you want to change the summation function, you create a new Summator-based class, override its sum() method and pass it on to the SimpleMultiplicator constructur.
精彩评论