public void CustomDynamicHasProperty() { // not great rhs has to be a specific type of class that derives from DynamicObject dynamic a = new CustomDynamic(); Assert.DoesNotThrow(() => a.Test()); }
static void Main(string[] args) { ClsLevel1 levelRoot = new ClsLevel1(); if (levelRoot?.ClsLevel2?.ClsLevel3 != null) { // will enter here you you do not comment the content of the ClsLevel1 constructor } dynamic dinRoot = new CustomDynamic(); dynamic DinLevel1 = new CustomDynamic(); dynamic DinLevel2 = new CustomDynamic(); dynamic DinLevel3 = new CustomDynamic(); dinRoot.DinLevel1 = DinLevel1; dinRoot.DinLevel1.DinLevel2 = DinLevel2; // dinRoot.DinLevel1.DinLevel2.DinLevel3 = DinLevel3; // You can comment this line to test dynamic foo = new CustomDynamic(); if (foo.Boo?.Lol ?? true) { Console.WriteLine("It works!"); } if (dinRoot?.DinLevel1?.DinLevel2?.DinLevel3 == null) { Console.WriteLine("There is something null!"); } else { Console.WriteLine("Nothing null in the chain!"); } //Console.ReadLine(); //if (!dinRoot.IsNull(p => p.DinLevel1.DinLevel2)) //{ // //Nothing is null //} if (dinRoot?.DinLevel1?.DinLevel2?.DinLevel3 != null) { // Obviously it will raise an exception because the DinLevel3 does not exists } }
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); }