private CustomDynamic GetTestDynamic() { dynamic dinRoot = new CustomDynamic();// ExpandoObject(); dynamic DinLevel1 = new CustomDynamic(); dynamic DinLevel2 = new CustomDynamic(); dynamic DinLevel3 = new CustomDynamic(); dinRoot.ContainsProperty("prop"); dinRoot.DinLevel1 = DinLevel1; dinRoot.DinLevel1.DinLevel2 = DinLevel2; //dinRoot.DinLevel1.DinLevel2.DinLevel3 = DinLevel3; // You can comment this line to test return(dinRoot); }
public void MyExpando_NotNullExpando() { dynamic dinRoot = null; if (dinRoot == null) { Console.WriteLine("Its Nulll!"); } dinRoot = new CustomDynamic();// ExpandoObject(); if (dinRoot != null) { Console.WriteLine("Its Not Null!"); } dynamic DinLevel1 = new CustomDynamic(); if (dinRoot.DinLevel1 == null) { Console.WriteLine("Its Null!"); } dinRoot.DinLevel1 = DinLevel1; if (dinRoot.DinLevel1 != null) { Console.WriteLine("Its Not Null!"); } if (dinRoot.DinLevel1.SomeNull == null) { Console.WriteLine("Its Null works!"); } if (dinRoot.DinLevel1?.SomeNull == null) { Console.WriteLine("Its Null works!"); } dynamic DinLevel2 = new CustomDynamic(); dynamic DinLevel3 = new CustomDynamic(); dinRoot.DinLevel1.DinLevel2 = DinLevel2; if (dinRoot.DinLevel1.DinLevel2.DinLevel3 == null) { Console.WriteLine("Its Null works!"); } //Should raise and exception because DinLevel3 is null right now //if (dinRoot.DinLevel1.DinLevel2.DinLevel3.SomeNull == null) //{ // Console.WriteLine("Its Null works!"); //} if (dinRoot.DinLevel1?.SomeNull == null) { Console.WriteLine("Its Null works!"); } if (dinRoot.DinLevel1?.DinLevel2?.DinLevel3 == null) { Console.WriteLine("Its Null works!"); } dinRoot.DinLevel1.DinLevel2.DinLevel3 = DinLevel3; // You can comment this line to test if (dinRoot.DinLevel1?.DinLevel2?.DinLevel3 != null) { Console.WriteLine("Its Not works!"); } if (dinRoot.DinLevel1?.DinLevel2?.DinLevel3.SomeNull == null) { Console.WriteLine("Its Null works!"); } DinLevel3.SomeList = new List <string>() { "Item1", "Item2" }; if (dinRoot.DinLevel1?.DinLevel2?.DinLevel3?.SomeList != null) { Console.WriteLine("Its Null works!"); } if (dinRoot.DinLevel1?.DinLevel2?.DinLevel3?.SomeList[0] != null) { Console.WriteLine("Its Null works!"); } dinRoot.NovaProp = "sdsd"; dinRoot.ContainsProperty("NovaProp", true); dinRoot.ContainsProperty("DinLevel3", true); dinRoot.ContainsProperty("SomeList", true); CustomDynamic.ContainsProperty(dinRoot, "DinLevel3", false); CustomDynamic.ContainsProperty(dinRoot, "DinLevel3", true); dinRoot.RandomProp = "Some random prop"; CustomDynamic.ContainsProperty(dinRoot, "RandomProp", false); CustomDynamic.ContainsProperty(dinRoot, "RandomProp", true); }