ฉันกำลังเขียน wrapper สำหรับองค์ประกอบ XML ที่ช่วยให้นักพัฒนาสามารถวิเคราะห์แอตทริบิวต์จาก XML ได้อย่างง่ายดาย เสื้อคลุมไม่มีสถานะอื่นนอกจากวัตถุที่ถูกห่อ
ฉันกำลังพิจารณาการติดตั้งต่อไปนี้ (ทำให้ง่ายขึ้นสำหรับตัวอย่างนี้) ซึ่งรวมถึงโอเวอร์โหลดสำหรับ==โอเปอเรเตอร์
class XmlWrapper
{
    protected readonly XElement _element;
    public XmlWrapper(XElement element)
    {
        _element = element;
    }
    public string NameAttribute
    {
        get
        {
            //Get the value of the name attribute
        }
        set
        {
            //Set the value of the name attribute
        }
    }
    public override bool Equals(object other)
    {
        var o = other as XmlWrapper;
        if (o == null) return false;
        return _element.Equals(o._element);
    }
    public override int GetHashCode()
    {
        return _element.GetHashCode();
    }
    static public bool operator == (XmlWrapper lhs, XmlWrapper rhs)
    {
        if (ReferenceEquals(lhs, null) && ReferenceEquals(rhs, null)) return true;
        if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) return false;
        return lhs._element == rhs._element;
    }
    static public bool operator != (XmlWrapper lhs, XmlWrapper rhs)
    {
        return !(lhs == rhs);
    }
}
ตามที่ฉันเข้าใจสำนวน c # ==ผู้ปฏิบัติงานมีไว้สำหรับความเท่าเทียมในการอ้างอิงในขณะที่Equals()วิธีการสำหรับความเท่าเทียมกันของค่า แต่ในกรณีนี้ "ค่า" เป็นเพียงการอ้างอิงถึงวัตถุที่ถูกห่อ ดังนั้นฉันจึงไม่ชัดเจนว่าสิ่งที่เป็นธรรมดาหรือสำนวนสำหรับ c #
ตัวอย่างเช่นในรหัสนี้ ...
var underlyingElement = new XElement("Foo");
var a = new XmlWrapper(underlyingElement);
var b = new XmlWrapper(underlyingElement);
a.NameAttribute = "Hello";
b.NameAttribute = "World";
if (a == b)
{
    Console.WriteLine("The wrappers a and b are the same.");
}เอาท์พุทของโปรแกรม .... "ตัวห่อ a และ b เหมือนกัน"? หรือว่าจะแปลกคือละเมิดหลักความประหลาดใจอย่างน้อย ?
Equalsฉันไม่เคยลบล้าง==( แต่ไม่เคยวิธีอื่น ๆ ) ขี้เกียจเป็นสำนวนหรือเปล่า หากฉันได้รับพฤติกรรมที่แตกต่างโดยไม่มีนักแสดงที่ชัดเจนว่าละเมิดความประหลาดใจน้อยที่สุด