-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
123 lines (99 loc) · 4.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
namespace DynamicTypes
{
/// <summary>
/// https://github.com/IronLanguages/dlr
/// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic
/// https://channel9.msdn.com/blogs/matthijs/c-40-and-beyond-by-anders-hejlsberg
/// https://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/
/// </summary>
class Program
{
public dynamic Property { get; set; }
static void Main(string[] args)
{
Person person01 = new Person("İbrahim", "ATAY"); // clr
Console.WriteLine($"CLR Type of Person - Type:{person01.GetType()}");
// Person person02 = new ExpandoObject(); // give the compilation error because Person is clr object
dynamic person03 = new ExpandoObject(); // dlr object
person03.FirstName = "İbrahim";
person03.LastName = "ATAY";
Console.WriteLine($"Dynamic Type of Person - Type:{person03.GetType()}");
var person04 = new Person("İbrahim", "ATAY");
// give the compilation error because var variable is not dynamic type.
// var declarations are resolved at compile-time.
// https://stackoverflow.com/questions/3442821/dynamic-vs-object-type
// https://stackoverflow.com/questions/961581/whats-the-difference-between-dynamic-c-4-and-var
//var person05 = new ExpandoObject();
//person05.FirstName = "İbrahim";
//person05.LastName = "ATAY";
dynamic person06 = new ExpandoObject();
person06.FirstName = "İbrahim";
person06.LastName = "ATAY";
var person07 = person06; // but do not access property
//person07.FirstName //compilation error
dynamic factory = Activator.CreateInstance(typeof(Factory));
Console.WriteLine($"Factory is {factory.GetFactoryName()}");
// using Dynamic type with extension methods
dynamic today = DateTime.Today;
// give the runtime exception
//Console.WriteLine(today.ToUKFormat());
//Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.DateTime' does not contain a definition for 'ToUKFormat''
Console.WriteLine(DateTimeExtensions.ToUKFormat(today));
// List & Dictionary
List<dynamic> person08 = new List<dynamic>()
{
new Person("İbrahim", "ATAY"),
new Person("Gizem", "ATAY")
};
Dictionary<string, dynamic> dictionary = new Dictionary<string, dynamic>()
{
{"IA", new Person("İbrahim", "ATAY")},
{"GA", new Person("Gizem", "ATAY")}
};
if (IsDictionary(dictionary))
{
Console.WriteLine("dictionary is dynamic dictionary");
}
}
public static void Sum(int val01) { }
public static void Sum(dynamic val01) { }
public static void Sum(int val01, dynamic val02) { }
public static void Sum(dynamic val01, dynamic val02) { }
public dynamic Foo()=> new ExpandoObject();
//public var Foo() => new ExpandoObject(); // compilation error
public static dynamic Bar() => 1;
public object Baz() => new ExpandoObject(); // return by dlr object
public static bool IsDictionary(object o)
{
if (o == null) return false;
return o is IDictionary &&
o.GetType().IsGenericType &&
o.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(Dictionary<,>));
}
}
class Person
{
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName{get; set; }
}
class Factory
{
public string GetFactoryName() => "Big Factory!";
}
static class DateTimeExtensions
{
public static string ToUKFormat(this DateTime dateTime)
{
return dateTime.ToString("dd/MM/yyyy");
}
}
}