C#: Inherit from Boolean? - c#

(how) can I Inherit from Boolean?
(Or make my class comparable to Boolean with '=' Operator)
class MyClass : Boolean
{
public MyClass()
{
this = true;
}
}
class Program
{
public Program()
{
MyClass myClass = new MyClass();
if(myClass == true)
//do something...
else
//do something else...
}
}

You can't. System.Boolean is a struct, and you can't derive from structs.
Now, why do you want to do so, exactly? What's the bigger purpose?
You could include an implicit conversion operator from your class to bool, but personally I wouldn't. I would almost always prefer to expose a property, so you'd write:
if (myValue.MyProperty)
... I think that keeps things clear. But if you could give us more of the real context, we may be able to give more concrete advice.

Simple example:
public class MyClass {
private bool isTrue = true;
public static bool operator ==(MyClass a, bool b)
{
if (a == null)
{
return false;
}
return a.isTrue == b;
}
public static bool operator !=(MyClass a, bool b)
{
return !(a == b);
}
}
somewhere in code you can compare your object with boolean value:
MyClass a = new MyClass();
if ( a == true ) { // it compares with a.isTrue property as defined in == operator overloading method
// ...
}

You can use an implicit conversion operator to have this code:
class MyClass {
public bool Value { get; set; }
public MyClass() {
Value = true;
}
public static implicit operator bool(MyClass m) {
return m != null && m.Value;
}
}
class Program {
public static void Main() {
var myClass = new MyClass();
if (myClass) { // MyClass can be treated like a Boolean
Console.WriteLine("myClass is true");
}
else {
Console.WriteLine("myClass is false");
}
}
}
It can be used as above:
if (myClass) ...
Or like this:
if (myClass == true) ...

while your example wouldnt work, you can do something similar for your own classes to test if one equals the values of another.
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.x == b.x && a.y == b.y && a.z == b.z;
}
public static bool operator !=(ThreeDPoint a, ThreeDPoint b)
{
return !(a == b);
}

You can ("or make my class comparable..."), by overriding the == operator. I presume Jon Skeet overlooked that part of the question.

If you want to be able to use your value in 'if' statements, define operator true and operator false (along with the & and | operators if you want to use && and ||.) (VB equivalents)
To answer more, I would have to know what you're trying to do (in other words, why not just use bool directly?)

Related

If (instance) / implicit boolean conversion on a custom class

I have the following class:
public class InterlockedBool
{
private int _value;
public bool Value
{
get { return _value > 0; }
set { System.Threading.Interlocked.Exchange(ref _value, value ? 1 : 0); }
}
public static bool operator ==(InterlockedBool obj1, bool obj2)
{
return obj1.Value.Equals(obj2);
}
public static bool operator !=(InterlockedBool obj1, bool obj2)
{
return !obj1.Value.Equals(obj2);
}
public override bool Equals(bool obj)
{
return this.Value.Equals(obj);
}
}
My question is: Can I check if Value is true, without == true? The operator override works, but can I also use it like so?
InterlockedBool ib = new InterlockedBool();
if (ib) { }
Instead of (this works, but normally I omit the == true in if statements.
if (ib == true) { }
And how do I assign it to a value without use .Value =?
Thanks for you help :)
You need to be able to convert your object to and from a boolean
Implicit Conversion
Your object to a boolean:
public static implicit operator bool(InterlockedBool obj)
{
return obj.Value;
}
Then a boolean to your object:
public static implicit operator InterlockedBool(bool obj)
{
return new InterlockedBool(obj);
}
Then you can test it:
InterlockedBool test1 = true;
if (test1)
{
//Do stuff
}
Explicit Conversion
If you want the users of this class to be aware that there is a conversion happening, you can force an explicit cast :
public static explicit operator bool(InterlockedBool obj)
{
return obj.Value;
}
public static explicit operator InterlockedBool(bool obj)
{
return new InterlockedBool(obj);
}
Then you must explicitly cast your objects:
InterlockedBool test1 = (InterlockedBool)true;
if ((bool)test1)
{
//Do stuff
}
EDIT (due to OP comment)
In the conversion from boolean to your object, I call a constructor that you did not mention, here is how I would build it:
public InterlockedBool(bool Value)
{
this.Value = Value;
}
Therefore the setting of the value is guranteed thread-safe
You can define an implicit conversion to bool :
public static implicit operator bool(InterlockedBool obj)
{
return obj.Value;
}

Testing for value equality between two interface instances in c#?

So I have an interface, lets call it IInterface.
public interface IInterface : IEquatable<IInterface>
{
string Name { get; set; }
int Number { get; }
Task<bool> Update();
}
Then I try and implement the interface in Implementation.
public bool Equals(IInterface other)
{
if (other == null) return false;
return (this.Name.Equals(other.Name) && this.Number.Equals(other.Number));
}
public override int GetHashCode()
{
return this.Number.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as IInterface ;
return other != null && Equals(other);
}
public static bool operator ==(Implementation left, IInterface right)
{
if (ReferenceEquals(left, right)) return true;
if (ReferenceEquals(left, null)) return false;
return left.Equals(right);
}
public static bool operator !=(Implementation left, IInterface right)
{
return !(left == right);
}
The problem I am running into is in a setter:
public IInterface MyIntf
{
get { return _myIntf; }
set
{
if (_myIntf == value) { return; }
_myIntf = value;
}
Intellisense is showing that the equality test there is testing the references only and treating both left and right as objects. I assume this is because there is no operator overload for ==(IInterface left, IInterface right). Of course, I cannot actually implement that function because == requires one of the sides to match the type of the implementing class. How does one properly make sure two interfaces can be checked for equality against each other?
Update
Got it, you cannot implement == for an interface. I will use Equals. Thanks everyone.
You should explicitly call Equals:
if (_myIntf != null && _myIntf.Equals(value)) { return; }
Implementing IEquatable<T> does not impact the == operator.
Use Equals instead of ==:
public IInterface MyIntf
{
get { return _myIntf; }
set
{
if (_myIntf.Equals(value)) { return; }
_myIntf = value;
}
}

Implicit (bool) and == operator override - handle if statements correctly

I have a custom class with implement both the == and the implicit for boolean operator.
Is this the correct way to handle all possible, if ==/!= statements and get the expected result?
Like this:
public class Foo
{
public bool Result { get; set; }
public static bool operator ==(bool #bool, Foo foo)
{
return Equals(foo, #bool);
}
public static bool operator !=(bool #bool, Foo foo)
{
return NotEquals(foo, #bool);
}
public static bool operator ==(Foo foo, bool #bool)
{
return Equals(foo, #bool);
}
public static bool operator !=(Foo foo, bool #bool)
{
return NotEquals(foo, #bool);
}
public static bool operator ==(Foo foo, Foo fooB)
{
return Equals(foo, fooB);
}
public static bool operator !=(Foo foo, Foo fooB)
{
return NotEquals(foo, fooB);
}
public static implicit operator bool(Foo foo)
{
try { return foo.Result; }
catch { return false; }
}
private static bool Equals(Foo foo, Foo fooB)
{
if (object.Equals(foo, null))
{
if (object.Equals(fooB, null))
return true;
return false;
}
if (object.Equals(fooB, null))
return false;
return foo.Result == fooB.Result;
}
private static bool NotEquals(Foo foo, Foo fooB)
{
if (object.Equals(foo, null))
{
if (object.Equals(fooB, null))
return false;
return true;
}
if (object.Equals(fooB, null))
return true;
return fooB.Result != foo.Result;
}
private static bool Equals(Foo foo, bool #bool)
{
if (object.Equals(foo, null))
return true;
return #bool == foo.Result;
}
private static bool NotEquals(Foo foo, bool #bool)
{
if (object.Equals(foo, null))
return false;
return #bool != foo.Result;
}
}
I am especially wondering about the fact that its seems you really need to implement overloads for either
if (new Foo() != true)
and
if (true != new Foo())
I think you've written too much code :-)
The following is sufficient:
public class Foo
{
public bool Result { get; set; }
public static implicit operator bool(Foo foo)
{
return !object.ReferenceEquals(foo, null) && foo.Result;
}
}
The compiler will then know how to implicitly convert variables of type Foo into bool. (And null will be converted to false).
So, when you write:
new Foo() == false
The compiler will use the implicit type converter to get a bool value from Foo and then use the standard equality operator for bool.
If we look at the IL that the compiler generates for that expression we find:
newobj instance void FooBool.Foo::.ctor() // new Foo()
call bool FooBool.Foo::op_Implicit(class FooBool.Foo) // implicit operator (Foo => bool)
ldc.i4.0 // false
ceq // equality operator (bool)
Here's a test:
static void Main(string[] args)
{
AssertTrue(new Foo() == false);
AssertTrue(false == new Foo());
AssertFalse(new Foo() != false);
AssertFalse(false != new Foo());
AssertTrue(new Foo { Result = true } == true);
AssertTrue(true == new Foo { Result = true });
AssertFalse(new Foo { Result = true } != true);
AssertFalse(true != new Foo { Result = true });
}
static void AssertTrue(bool value)
{
Console.WriteLine(value ? "ok" : "not ok");
}
static void AssertFalse(bool value)
{
Console.WriteLine(value ? "not ok" : "ok");
}
It prints ok for each test. So this simplified code should fulfill your needs if I understood them correctly.
UPDATE
To allow the equality operator to work for instances of Foo (which may be null):
public static bool operator ==(Foo a, Foo b)
{
if (object.ReferenceEquals(a, b))
{
return true;
}
else if (object.ReferenceEquals(a, null))
{
return !b.Result;
}
else if (object.ReferenceEquals(b, null))
{
return !a.Result;
}
else
{
return a.Result == b.Result;
}
}
You should then also implement the inequality operator:
public static bool operator !=(Foo a, Foo b)
{
return !(a == b);
}
And also override GetHashCode + Equals
public override int GetHashCode()
{
return this.Result ? 1 : 0;
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null))
{
return !this.Result;
}
Type t = obj.GetType();
if (t == typeof(Foo))
{
return this.Result == ((Foo)obj).Result;
}
else if (t == typeof(bool))
{
return this.Result == (bool)obj;
}
else
{
return false;
}
}
I think that you explicitly covered all the bases in your code; if needed you must consider the ordering of parameters for the operator so if you want your Equals function to be called for both ordering of parameters what you did is right.
However it looks a bit overkill in the case of comparing Foo to bool since you could simply rely on the implicit conversion. This would allow you to remove all operators between the two types as well as the Equals and NotEquals methods.
What's more, it would avoid some inconsistency in your code regarding the conversion of a null Foo to boolean. When you pass a null Foo to the Equals method it will return true whereas in the implicit conversion a null Foo will return false:
true == (Foo)null; //true
true == Convert.ToBoolean((Foo)null); //false
In closing, here is how i'd write the Foo class, i think it is sufficient:
public class Foo
{
public bool Result { get; set; }
public static bool operator ==(Foo foo, Foo fooB)
{
return Equals(foo, fooB);
}
public static bool operator !=(Foo foo, Foo fooB)
{
return NotEquals(foo, fooB);
}
public static implicit operator bool(Foo foo)
{
return foo == null ? false : foo.Result;
}
private static bool Equals(Foo foo, Foo fooB)
{
if (object.Equals(foo, null))
{
return object.Equals(fooB, null);
}
if (object.Equals(fooB, null))
return false;
return foo.Result == fooB.Result;
}
private static bool NotEquals(Foo foo, Foo fooB)
{
return !Equals(foo, fooB);
}
}

IEquatable Interface what to do when checking for null

I have implemented the IEquatable interface in a class with the following code.
public bool Equals(ClauseBE other)
{
if (this._id == other._id)
{
return true;
}
return false;
}
public override bool Equals(Object obj)
{
if (obj == null)
{
return base.Equals(obj);
}
if (!(obj is ClauseBE))
{
throw new InvalidCastException("The 'obj' argument is not a ClauseBE object.");
}
return Equals(obj as ClauseBE);
}
public override int GetHashCode()
{
return this._id.GetHashCode();
}
public static bool operator ==(ClauseBE a, ClauseBE b)
{
// cast to object so we call the overloaded Equals function which appropriately checks when b is null.
return a.Equals(b as object);
}
public static bool operator !=(ClauseBE a, ClauseBE b)
{
// cast to object so we call the overloaded Equals function which appropriately checks when b is null.
return !a.Equals(b as object);
}
This code work very well for most all cases. However, the following check throws an exception in the equality operator overload method because a is null and therefore does not have a Equals method.
if(this.Clause != null)
{
}
What is the standard way to solve this issue?
EDIT
I have gone to this, but it seems pretty cumbersome. I was hoping there was a more elegant way to accomplish this.
public static bool operator ==(ClauseBE a, ClauseBE b)
{
if (a as object == null && b as object == null)
{
return true;
}
if ((a as object == null && b as object != null)
|| (b as object == null && a as object != null))
{
return false;
}
// cast to object so we call the overloaded Equals function which appropriately checks when b is null.
return a.Equals(b as object);
}
public static bool operator !=(ClauseBE a, ClauseBE b)
{
if (a as object == null && b as object == null)
{
return false;
}
if((a as object == null && b as object != null)
|| (b as object == null && a as object != null))
{
return true;
}
// cast to object so we call the overloaded Equals function which appropriately checks when b is null.
return !a.Equals(b as object);
}
Solution
Thanks all. I got a lot of good tips from everyone, I really appreciate it. This is what I finally settled on, it's a lot more elegant than what I had started with. All code is the same except operator overloads.
public static bool operator ==(ClauseBE a, ClauseBE b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
{
return true;
}
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
{
return false;
}
return a.Equals(b);
}
public static bool operator !=(ClauseBE a, ClauseBE b)
{
return !(a == b);
}
I've always found it easier to write the static operator with null handling, and have the Equals override call the overloaded operator with "this" as one of the parameters.
From Guidelines for Overloading Equals() and Operator == (C# Programming Guide)
//add this code to class ThreeDPoint as defined previously
//
public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.x == b.x && a.y == b.y && a.z == b.z;
}
public static bool operator !=(ThreeDPoint a, ThreeDPoint b)
{
return !(a == b);
}
This is how ReSharper creates equality operators and implements IEquatable<T>, which I trust blindly, of course ;-)
public class ClauseBE : IEquatable<ClauseBE>
{
private int _id;
public bool Equals(ClauseBE other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return other._id == this._id;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != typeof(ClauseBE))
return false;
return Equals((ClauseBE)obj);
}
public override int GetHashCode()
{
return this._id.GetHashCode();
}
public static bool operator ==(ClauseBE left, ClauseBE right)
{
return Equals(left, right);
}
public static bool operator !=(ClauseBE left, ClauseBE right)
{
return !Equals(left, right);
}
}
Check for null and return false. Equals should always be false if one of the operands is null;
I think this is a bit less cumbersome than casting to Object before checking for null:
ReferenceEquals(a, null)
Other answers give good solutions to the general problem.
However, your own code can be simplified into a relatively simple solution ...
Firstly, at the start of your == operator you have this:
// First test
if (a as object == null && b as object == null)
{
return true;
}
This qualifies as "working too hard".
If ClauseBE is a reference type, then you only need to compare with null - the "as object" is redundant; equally, if ClauseBE is a value type, then it can never be null.
Assuming that ClauseBE is a reference type (the most likely case), then you can simplify to this - note that we use Object.Equals() to avoid infinite recursion and a stack blowout.
// First test
if (Object.Equals(a, null) && Object.Equals(b, null))
{
return true;
}
One useful shortcut is to use Object.ReferenceEquals() - which handles nulls for you.
So you could write this instead:
// First test
if (Object.ReferenceEquals(a, b))
{
return true;
}
with the bonus that this also handles the case where a and b are the same exact object.
Once you get past the Object.ReferenceEquals() test, you know that a and b are different.
So your next test:
// Second test
if ((a as object == null && b as object != null)
|| (b as object == null && a as object != null))
{
return false;
}
can be simplified - since you know that if a is null, b cannot be null, and so on.
// Second test
if (Object.Equals(a, null) || Object.Equals(b, null))
{
return false;
}
If this test fails, then you know that a and b are different, and that neither is null. A good time to call your overridden Equals().
// Use the implementation of Equals() for the rest
return a.Equals(b as object);
public class Foo : IEquatable<Foo>
{
public Int32 Id { get; set; }
public override Int32 GetHashCode()
{
return this.Id.GetHashCode();
}
public override Boolean Equals(Object obj)
{
return !Object.ReferenceEquals(obj as Foo, null)
&& (this.Id == ((Foo)obj).Id);
// Alternative casting to Object to use == operator.
return ((Object)(obj as Foo) != null) && (this.Id == ((Foo)obj).Id);
}
public static Boolean operator ==(Foo a, Foo b)
{
return Object.Equals(a, b);
}
public static Boolean operator !=(Foo a, Foo b)
{
return !Object.Equals(a, b);
}
public Boolean Equals(Foo other)
{
return Object.Equals(this, other);
}
}
I have used the following approach and it seemed to work well for me. Infact, Resharper suggests this approach.
public bool Equals(Foo pFoo)
{
if (pFoo == null)
return false;
return (pFoo.Id == Id);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, this))
return true;
return Equals(obj as Foo);
}
I prefer to perform all the comparison logic in the Equals(T) method, and leave the "if this or that is null, else ..." in operator overloads to the framework.
The only tricky thing about overriding operator overloads is that you can no longer use those operators in your Equals implementation, for example to compare with null. Instead, object.ReferenceEquals can be used to achieve the same effect.
Following the TwoDPoint example in the MSDN Guidelines for Overriding Equals() and Operator == article, this is the pattern I generate when implementing value equality for types:
public override bool Equals( object obj ) {
// Note: For value types, would use:
// return obj is TwoDPoint && this.Equals( (TwoDPoint)obj );
return this.Equals( obj as TwoDPoint );
}
public bool Equals( TwoDPoint other ) {
// Note: null check not needed for value types.
return !object.ReferenceEquals( other, null )
&& EqualityComparer<int>.Default.Equals( this.X, other.X )
&& EqualityComparer<int>.Default.Equals( this.Y, other.Y );
}
public static bool operator ==( TwoDPoint left, TwoDPoint right ) {
// System.Collections.Generic.EqualityComparer<T> will perform the null checks
// on the operands, and will call the Equals overload if necessary.
return EqualityComparer<TwoDPoint>.Default.Equals( left, right );
}
public static bool operator !=( TwoDPoint left, TwoDPoint right ) {
return !EqualityComparer<TwoDPoint>.Default.Equals( left, right );
}
The form above is the safest implementation, as it simply forwards the field equality checks to the framework and requires no knowledge of whether the fields overload the equality operators. It is perfectly fine to simplify this where you know the overload exists:
public bool Equals( TwoDPoint other ) {
return !object.ReferenceEquals( other, null )
&& this.X == other.X
&& this.Y == other.Y;
}
You can also replace the EqualityComparer<T> calls in the operator overloads with calls to the static object.Equals method when comparing reference types, or when boxing value types does not matter:
public static bool operator ==( TwoDPoint left, TwoDPoint right ) {
return object.Equals( left, right );
}
public static bool operator !=( TwoDPoint left, TwoDPoint right ) {
return !object.Equals( left, right );
}
See also What is the best algorithm for an overridden GetHashCode? for implementing GetHashCode.

Should I Overload == Operator?

How does the == operator really function in C#? If it used to compare objects of class A, will it try to match all of A's properties, or will it look for pointers to the same memory location (or maybe something else)?
Let's create a hypothetical example. I'm writing an application that utilizes the Twitter API, and it has a Tweet class, which has all the properties of a single tweet: text, sender, date&time, source, etc. If I want to compare objects of class Tweet for equivalence, can I just use:
Tweet a, b;
if (a == b)
{
//do something...
}
Will that check for equivalence of all the properties of the Tweet class between a and b?
If not, would the correct approach be to overload the == operator to explicitly check for equivalence of all the fields?
UPDATE: From the first two answers, am I right in assuming:
If the == operator or Equals method is not overloaded for a class, the == operator for the object class is used.
The == operator for the object class checks for equality in memory location.
I have to overload the == operator or the Equals method to accomplish this task.
In the overload, I have to check for equivalence in properties manually, so there is no way to do it semi-automatically, say, in a loop, right?
UPDATE #2: Yuriy made a comment that it is possible to do check for equivalence in properties in the == operator with reflection. How can this be done? Could you give me some sample code? Thanks!
For reference types, the default implementations of both the == operator and the Equals() method will simply check that both objects have the same reference, and are therefore the same instance.
If you want to check the contents of two different objects are equal then you must write the code to do it yourself, one way or another. It would be possible to do with reflection (the MbUnit test framework does something along these lines) but with a heavy performance penalty and a good chance that it wouldn't do quite what you expected anyway, and you should implement == or Equals and GetHashCode by hand.
MSDN has a good example of how to do it:
public override bool Equals(object o)
{
try
{
return (bool) (this == (DBBool) o);
}
catch
{
return false;
}
}
Then you overload the == and !=:
// Equality operator. Returns dbNull if either operand is dbNull,
// otherwise returns dbTrue or dbFalse:
public static DBBool operator ==(DBBool x, DBBool y)
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value == y.value? dbTrue: dbFalse;
}
// Inequality operator. Returns dbNull if either operand is
// dbNull, otherwise returns dbTrue or dbFalse:
public static DBBool operator !=(DBBool x, DBBool y)
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value != y.value? dbTrue: dbFalse;
}
And don't forget to overload the GetHash method.
Edit:
I wrote the following quick sample for using reflection in a compare. This would have to be much more comprehensive, I might try doing a blog on it if people want me to:
public class TestEquals
{
private int _x;
public TestEquals(int x)
{
this._x = x;
}
public override bool Equals(object obj)
{
TestEquals te = (TestEquals)obj;
if (te == null) return false;
foreach (var field in typeof(TestEquals)
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (!field.GetValue(this).Equals(field.GetValue(te)))
return false;
}
return true;
}
}
The proper approach is the overload the equals method of the Tweet class in addition to the == operator, as described here.
Will that check for equivalence of all the properties of the Tweet class between a and b?
No
If not, would the correct approach be to overload the == operator to explicitly check for equivalence of all the fields?
You can either overload the == operator, or overload the Equals function.
Edit
#Yuriy gave a good example for compating all the non public variables. Since i also wrote an example, here it is (mine compares properties)
class TwitterItem
{
private string myValue = "default value";
public string Value1
{
get { return myValue; }
set { myValue = value; }
}
public string Value2
{
get { return myValue; }
set { myValue = value; }
}
public string Value3
{
get { return myValue; }
set { myValue = value; }
}
public override bool Equals(object obj)
{
if (base.Equals(obj)) return true;
Type type = typeof(TwitterItem);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
if (false == property.GetValue(this, null).Equals(property.GetValue(obj, null)))
return false;
}
return true;
}
}
You can compare the properties using reflection:
var a = new Entity() { Name = "test", ID = "1" };
var b = new Entity() { Name = "test", ID = "1" };
var c = new Entity() { Name = "test", ID = "2" };
System.Diagnostics.Debug.WriteLine(a.Equals(b));//Returns true
System.Diagnostics.Debug.WriteLine(a.Equals(c));//Returns false
public class Entity
{
public string Name { get; set; }
public string ID { get; set; }
public override bool Equals(object obj)
{
var t = obj.GetType();
foreach (var p in t.GetProperties())
{
if (t.GetProperty(p.Name).GetValue(obj, null) != t.GetProperty(p.Name).GetValue(this, null))
return false;
}
return true;
}
}

Categories