static void Main(string[] args) { // create an object of MyClass class MyClass myObj = new MyClass(); // create AProperty that assigns string to MyClass objects AProperty<MyClass, string> myAProp = new AProperty<MyClass, string>(null); myAProp.SetProperty(myObj, "Hello World"); // try to do garbage collection, // the myObj should not be collected at this point // since the main program has a strong reference to it. GC.Collect(); // the property should not be collected at this point // since the reference to the object still exists in the // Main program. string thePropValue = myAProp.GetProperty(myObj); Console.WriteLine("The AProp Value is " + thePropValue); // set the only 'strong' reference to myObj to null myObj = null; // after the only 'strong' reference to myObj // was set to null, the call to 'GC.Collect()' should // collect the object not-withstanding the fact // that it is still weakly refenced from within myAProp object. GC.Collect(); // destructor should be called before sleep or in the beginning of sleep; Console.WriteLine("before sleep"); Thread.Sleep(3000); GC.Collect(); // if you put a break point at the next line, // and expand the internals of myAProp object, // you'll see that it has no objects within it // (the weak reference key has been removed) Console.WriteLine("After sleep"); }
static void Main(string[] args) { #region Plain C# to Plain C# property binding Console.WriteLine("\n\nTesting binding from plain property to another plain property\n"); // initialize test objects MyTestDataClass sourceObj = new MyTestDataClass { MyStringProp = "Hello World" }; MyTestDataClass targetObj = new MyTestDataClass(); OneWayPropertyBindingWithPath<string, string> plainToPlainPropBinding = new OneWayPropertyBindingWithPath<string, string>(); plainToPlainPropBinding.SourceObj = sourceObj; plainToPlainPropBinding.SourcePPath = new BindingPathLink<string>("MyStringProp"); plainToPlainPropBinding.TargetObj = targetObj; plainToPlainPropBinding.TargetPPath = new BindingPathLink<string>("MyStringProp"); // bind the two properties. plainToPlainPropBinding.Bind(); // verify that the binding changed the target property // to be the same as the source property Console.Write("Testing target property change after binding operation: "); Console.WriteLine(targetObj.MyStringProp); // should print Hello World; // let us change the source property and verify that target property also changes sourceObj.MyStringProp = "Hi World"; Console.Write("Testing target property change after the source property change: "); Console.WriteLine(targetObj.MyStringProp); // should print Hi World; #endregion Plain C# to Plain C# property binding #region AProperty to AProperty binding Console.WriteLine("\n\nTesting binding from plain property to another plain property\n"); AProperty<object, string> myAProperty = new AProperty<object, string>(); // reinitialize test objects sourceObj = new MyTestDataClass(); targetObj = new MyTestDataClass(); // set AProperty on the source object before the binding myAProperty.SetProperty(sourceObj, "Hello World"); OneWayPropertyBinding<string, string> aPropToAPropBinding = new OneWayPropertyBinding<string, string>(); aPropToAPropBinding.SourceObj = sourceObj; aPropToAPropBinding.SourcePPath = new BindingPathLink<string>(myAProperty); aPropToAPropBinding.TargetObj = targetObj; aPropToAPropBinding.TargetPPath = new BindingPathLink<string>(myAProperty); aPropToAPropBinding.Bind(); Console.Write("Testing target property change after binding operation: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); // change the source property myAProperty.SetProperty(sourceObj, "Hi World"); Console.Write("Testing target property change after the source property change: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); #endregion AProperty to AProperty binding #region plain property to AProperty binding Console.WriteLine("\n\nTesting binding from plain property to AProp\n"); // reinitialize test objects sourceObj = new MyTestDataClass { MyStringProp = "Hello World" }; targetObj = new MyTestDataClass(); OneWayPropertyBinding<string, string> plainToAPropBinding = new OneWayPropertyBinding<string, string>(); plainToAPropBinding.SourcePPath = new BindingPathLink<string>("MyStringProp") { DefaultValue = "blablabla" }; plainToAPropBinding.TargetObj = targetObj; plainToAPropBinding.TargetPPath = new BindingPathLink<string>(myAProperty); plainToAPropBinding.Bind(); Console.Write("Testing target property change after binding operation without source object (default value will be used): "); Console.WriteLine(myAProperty.GetProperty(targetObj)); plainToAPropBinding.SourceObj = sourceObj; Console.Write("Testing target property change after binding operation with source object: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); sourceObj.MyStringProp = "Hi World"; Console.Write("Testing target property change after the source property change: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); #endregion plain property to AProperty binding #region AProperty to plain property binding Console.WriteLine("\n\nTesting binding from AProp to plain property\n"); // reinitialize test objects sourceObj = new MyTestDataClass(); targetObj = new MyTestDataClass(); myAProperty.SetProperty(sourceObj, "Hello World"); OneWayPropertyBinding<string, string> aPropToPlainBinding = new OneWayPropertyBinding<string, string>(); aPropToPlainBinding.SourceObj = sourceObj; aPropToPlainBinding.SourcePPath = new BindingPathLink<string>(myAProperty); aPropToPlainBinding.TargetObj = targetObj; aPropToPlainBinding.TargetPPath = new BindingPathLink<string>("MyStringProp"); aPropToPlainBinding.Bind(); Console.Write("Testing target property change after binding operation: "); Console.WriteLine(targetObj.MyStringProp); myAProperty.SetProperty(sourceObj, "Hi World"); Console.Write("Testing target property change after the source property change: "); Console.WriteLine(targetObj.MyStringProp); #endregion AProperty to plain property binding }