public void GetTokens_DynamicExample_ReturnsDynamicObject() { dynamic input = new DynamicExample(); input.foo = "hello world"; input.number = 42; input.boolean = false; input.@null = null; var expected = new[] { ModelGrammar.TokenObjectBegin("DynamicExample"), ModelGrammar.TokenProperty("foo"), ModelGrammar.TokenPrimitive("hello world"), ModelGrammar.TokenProperty("number"), ModelGrammar.TokenPrimitive(42), ModelGrammar.TokenProperty("boolean"), ModelGrammar.TokenPrimitive(false), ModelGrammar.TokenProperty("null"), ModelGrammar.TokenPrimitive(null), ModelGrammar.TokenObjectEnd }; var walker = new ModelWalker(new DataWriterSettings()); var actual = walker.GetTokens(input).ToArray(); Assert.Equal(expected, actual, false); }
public void Analyze_DynamicExample_ReturnsDynamicObject() { var input = new[] { ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("foo"), ModelGrammar.TokenPrimitive("hello world"), ModelGrammar.TokenProperty("number"), ModelGrammar.TokenPrimitive(42), ModelGrammar.TokenProperty("boolean"), ModelGrammar.TokenPrimitive(false), ModelGrammar.TokenProperty("null"), ModelGrammar.TokenPrimitive(null), ModelGrammar.TokenObjectEnd }; dynamic expected = new DynamicExample(); expected.foo = "hello world"; expected.number = 42; expected.boolean = false; expected.@null = null; var analyzer = new ModelAnalyzer(new DataReaderSettings()); var actual = analyzer.Analyze <DynamicExample>(input).Single(); Assert.Equal(expected.Values, actual.Values, false); }
public void Analyze_DynamicExample_ReturnsDynamicObject() { var input = new[] { ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("foo"), ModelGrammar.TokenPrimitive("hello world"), ModelGrammar.TokenProperty("number"), ModelGrammar.TokenPrimitive(42), ModelGrammar.TokenProperty("boolean"), ModelGrammar.TokenPrimitive(false), ModelGrammar.TokenProperty("null"), ModelGrammar.TokenPrimitive(null), ModelGrammar.TokenObjectEnd }; dynamic expected = new DynamicExample(); expected.foo = "hello world"; expected.number = 42; expected.boolean = false; expected.@null = null; var analyzer = new ModelAnalyzer(new DataReaderSettings()); var actual = analyzer.Analyze<DynamicExample>(input).Single(); Assert.Equal(expected.Values, actual.Values, false); }
public void RunDynamicExamples() { // -------------------------------------------------------------------------------------------- // - The dynamic keyword https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic // // -------------------------------------------------------------------------------------------- // Using for different types Console.Write("[dynamic] Dynamic variable = "); dynamic value = null; Console.Write("{0}, ", value != null ? value.ToString() : "(null)"); value = "I can be everything"; Console.Write("{0}, ", value != null ? value.ToString() : "(null)"); value = 123; Console.Write("{0}, ", value != null ? value.ToString() : "(null)"); value = DateTime.Today; Console.Write("{0} ", value != null ? value.ToString() : "(null)"); Console.WriteLine(); // -------------------------------------------------------------------------------------------- // Using for complex types and calls to their methods dynamic temp = new DynamicExample(); // ----------------------------------------- // No IntelliSense for Add() though Console.WriteLine("[dynamic.MethodCall] Int = {0}, string = {1}, DateTime = {2}", temp.Add(3, 7), temp.Add("Foo", "Bar"), temp.Add(new DateTime(2010, 1, 1), TimeSpan.FromDays(30))); // ----------------------------------------- // Lexical checks happen at runtime try { temp.Sub(5, 3); } catch (RuntimeBinderException ex) { Console.WriteLine("[dynamic.MissingMethodCall] Exception = {0}", ex.Message); } // ----------------------------------------- // Some extensive dynamic example Console.WriteLine("[dynamic.MultiMethod] Int = {0}, string = {1}, DateTime = {2}", temp.Conversion(123), temp.Conversion("FooBar"), temp.Conversion(new DateTime(2010, 1, 1))); // -------------------------------------------------------------------------------------------- // Performance // Testing the performance of normal, boxed and dynamic assignment and operations. // Another downside of dynamic is the complied result when viewing for example in ILSpy. // Just try. // ----------------------------------------- // Normal add and assignment int intValue = 123; int intValue2 = 345; int intTimes = 100000000; DateTime intCurrent = DateTime.UtcNow; for (int i = 0; i < intTimes; ++i) { intValue = intValue2 + intValue; } TimeSpan intResult = DateTime.UtcNow - intCurrent; // ----------------------------------------- // Boxed add and assignment object boxValue = 123; object boxValue2 = boxValue; int boxTimes = 100000000; DateTime boxCurrent = DateTime.UtcNow; for (int i = 0; i < boxTimes; ++i) { boxValue2 = (int)boxValue2 + (int)boxValue; } TimeSpan boxResult = DateTime.UtcNow - boxCurrent; // ----------------------------------------- // Dynamic add and assignment dynamic dynamicValue = 123; dynamic dynamicValue2 = boxValue; int dynamicTimes = 100000000; DateTime dynamicCurrent = DateTime.UtcNow; for (int i = 0; i < dynamicTimes; ++i) { dynamicValue2 = dynamicValue2 + dynamicValue; } TimeSpan dynamicResult = DateTime.UtcNow - dynamicCurrent; // ----------------------------------------- // Display results Console.WriteLine("[dynamic.Performance] Normal = {0:0.000}, Boxing = {1:0.000}, Dynamic = {2:0.000}", intResult.TotalSeconds, boxResult.TotalSeconds, dynamicResult.TotalSeconds); }