2

Let's say i have a class like that:

class Test { string asd; int dsa; } 

Later, I create instance of this class, then later it may change values for data members.
I need a simple compare method without comparing all class members.
Something like that:

Test t = new Test(); t.asd = "asd"; SOMETHING SMTH = GetSOMETHINGof(t); a.dsa = 3; if (GetSOMETHINGof(t) != SMTH) //object modified 

Maybe someone know built-in things that can be used for that? I know i can implement Equal and etc, but that's not what i want. There is too much classes and a lot of members.
I use C# and .net 4.0

7
  • could you use private variables, public properties and implement the propertychanged event on those properties? that way you can subscribe to the event and have it fire when something changes Commented Sep 14, 2012 at 6:10
  • Maybe this can help you : stackoverflow.com/questions/2363801/… Commented Sep 14, 2012 at 6:10
  • there is a lot of changes in source code if switch to properties. I can't pass properties as ref, out and etc. I need something more cool :D Commented Sep 14, 2012 at 6:15
  • or this stackoverflow.com/questions/9624318/… Commented Sep 14, 2012 at 6:17
  • This will be helpful stackoverflow.com/questions/2502395/… Commented Sep 14, 2012 at 6:18

2 Answers 2

1

Compare .NET Objects 1.4.2.0 by Greg Finzer http://www.nuget.org/packages/CompareNETObjects

Sign up to request clarification or add additional context in comments.

Comments

1

Add extra property to keep state of the object:

public class A { public bool HasChanged { get; set; } object _Value; public object Value { get { return _Value; } set { HasChanged = value != _Value; _Value = value; } } public A(object _value) { _Value = _value; HasChanged = false; } } 

and use it:

 A a = new A(5); Console.WriteLine(a.HasChanged); //false a.Value = 6; Console.WriteLine(a.HasChanged); //true a.HasChanged = false; Console.WriteLine(a.HasChanged); //false 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.