1

Here is something that looks like the classes I'm currently working with.

public class SomeClass
{

    public PropertyAClass A { get; set;}
    public PropertyBClass B { get; set;}

    public SomeClass (PropertyAClass A, PropertyBClass B)
    {
        this.A = A;
        this.B = B;
    }

    public static SomeClass operator +(SomeClass a, SomeClass b)
    {
        return new SomeClass(a.A + b.A, a.b + b.B);
    }
}

public class DerivedClass : SomeClass
{
    public DerivedClass (PropertyADerivedClass A, PropertyBDerivedClass B) : base(A,B)
    {
    }

    //Operation is the same but I have to repeat the method
    public static DerivedClass operator +(DerivedClass a, DerivedClass b)
    {
        return new DerivedClass(a.A + b.A, a.b + b.B);
    }
}

Is there some smart way that would make it possible not to repeat operator + in derived class by applying it to the base class and still getting the derived class as output ?

TTT
  • 1,848
  • 2
  • 30
  • 60
  • 2
    You're trying to figure out how to replace one very simple line of code. Even if you could, anything you would attempt to do is certain to be *dramatically* more complicated. – Servy Sep 02 '16 at 13:32
  • This is an example. In the actual code, the line of code may be less "simple". – TTT Sep 02 '16 at 13:37
  • If this isn't representative, then a solution to this problem is unlikely to help you. How you might go about avoiding repeating that verbose creation work is going to be highly dependent on what that creation work is. – Servy Sep 02 '16 at 13:39
  • You cannot easily do that in C#. Thus it impossible to gave you most appropriate alternatives from a simplified example. In simple cases, it might be possible to use something like `Func addFun` and an extra base class `SomeBaseWithFuncs` as the base class and then properly hook functions... – Phil1970 Sep 02 '16 at 16:40

1 Answers1

0

I do not have C# compiler rigth now but I tried wrote something by heart. Try to use Generic Type Param, something like that:

using System;
using System.Reflection;

public class SomeClass<T>
{

    public PropertyAClass A { get; set;}
    public PropertyBClass B { get; set;}

    public SomeClass (PropertyAClass A, PropertyBClass B)
    {
        this.A = A;
        this.B = B;
    }

    public static T operator +(T a, T b)
    {
        return new T(a.A + b.A, a.b + b.B);
    }
}

public class DerivedClass : SomeClass<DerivedClass>
{
    public DerivedClass (PropertyADerivedClass A, PropertyBDerivedClass B) : base(A,B)
    {
    }
}

Maybe it help:

Pass An Instantiated System.Type as a Type Parameter for a Generic Class

http://www.dotnetperls.com/generic

https://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

Community
  • 1
  • 1
Vitorlui
  • 760
  • 1
  • 11
  • 26
  • I think I already tried this, but for some reason I discard it. I may try it again. – TTT Sep 02 '16 at 13:40
  • Nice, good luck. http://stackoverflow.com/questions/1420581/inheritance-on-a-constrained-generic-type-parameter – Vitorlui Sep 02 '16 at 14:00
  • 1
    That code would not compile... C# generics are very limited and won't easily allow you to do something like this. For one thing, the compiler does not know that `T` as member `A` and `B` and even less their types. For example, the code would obviously not works with `SomeClass`. – Phil1970 Sep 02 '16 at 16:28