I want create a Func that is a two-argument encapsulation of the base.Equals method for some derived class. One argument of the Func is the instance in/on which base.Equals is called and the other argument of the Func is passed into base.Equals. I cannot place requirements on the base class, but I can place some on the derived class. For example, I can require the derived class to expose base.Equals.
This would be easy if the keyword base could be used in like the keyword this, but it doesn't. See my failed attempt below.
using System; namespace MyNamespace { class BaseClass { } class DerivedClass : BaseClass, IEquatable<DerivedClass> { Func<DerivedClass, DerivedClass, bool> baseEquals; DerivedClass() { DerivedClass referenceToThis = this; BaseClass referenceToBase = referenceToThis.base; // doesn't compile this.baseEquals = (x, y) => x.Equals(y); } public bool Equals(DerivedClass that) => this.baseEquals(this, that); } } Based on my current understanding of C#, I think what want to do is not allowed.
Is there C# that has the same semantics as my example but is valid?
If not, how close can one get to this example? That is, to what extent can the
base.Equalsmethod be encapsulated?
Edit to add an example
using System; using FluentEquality.Common.EqualityCompareres; namespace MyNamespace { class MyProgram { static void Main(string[] args) { var instance0 = new LeafClass(0); var instance1 = new LeafClass(1); var baseEqualsMethod = SomeMagicalMethodYetToBeDefined(instance0.BaseEqualsMethod, instance0.BaseGetHashCodeMethod); equalityComparer.Equals(instance0, instance1); // should output: BaseClass 0 equalityComparer.Equals(instance1, instance0); // should output: BaseClass 1 Console.ReadKey(); } } class BaseClass { protected int Id; public BaseClass(int id) { this.Id = id; } public override bool Equals(object obj) { Console.WriteLine(nameof(BaseClass) + " " + Id); return false; } public override int GetHashCode() => base.GetHashCode(); } class DerivedClass : BaseClass { public DerivedClass(int id) : base(id) { } public override bool Equals(object obj) { Console.WriteLine(nameof(DerivedClass) + " " + Id); return false; } public override int GetHashCode() => base.GetHashCode(); public Func<object, bool> BaseEqualsMethod => base.Equals; public Func<int> BaseGetHashCodeMethod => base.GetHashCode; } class LeafClass : DerivedClass { public LeafClass(int id) : base(id) { } public override bool Equals(object obj) { Console.WriteLine(nameof(LeafClass) + " " + Id); return false; } public override int GetHashCode() => base.GetHashCode(); } }
base.Equals, without knowing the type. Could you explain the broad solution you're looking for? I'm sure there's a better way to approach the issue. Forgetting thatEqualsis a method onobject, what happens if they pass youBaseClass, where there is nobase.Equals?base.Equals. I only did that to create a minimum working example. I want to create an implantation of IEqualityComparer that uses the base Equals and GeyHashCode methods for a type. Your last question raised a good point in that whatever I build will also need to work for the typeobject, which has no base.Func.baseEqualsMethodandequalityComparerto be the same thing?