Is it possible in C# to compare two objects of unknown types, (including both reference and value types) using their type comparators if they exist?
The goal is to write a function that would have a signature like this:
public bool Compare(object a, object b)
{
// compare logic goes here
}
Which would return
Compare(100d, 100d) == true
Compare(100f, 100f) == true
Compare("hello", "hello") == true
Compare(null, null) == true
Compare(100d, 101d) == false
Compare(100f, null) == false
// Use type comparators where possible, i.e.:
Compare(new DateTime(2010, 12, 01), new DateTime(2010, 12, 01)) == true
Compare(new DateTime(2010, 12, 01), new DateTime(2010, 12, 02)) == false
Compare(new DateTime(2010, 12, 01), null) == false
Is there a generic approach to solving this problem that would work for any type of object?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…