object result = method.Invoke(obj, new object[] { arg1, arg2 });:动态调用一个方法,并传递参数。
属性(Property):
public string Name { get; set; }:定义一个自动实现的属性。
private int age; public int Age { get { return age; } set { if (value >= 0 && value <= 150) age = value; else throw new ArgumentException("Age must be between 0 and 150."); } }:定义一个具有验证功能的属性。
索引器(Indexer):
public int this[int index] { get { return array[index]; } set { array[index] = value; } }:定义一个基于数组的索引器。
public string this[string key] { get { return dictionary[key]; } set { dictionary[key] = value; } }:定义一个基于字典的索引器。
委托(Delegate):
public delegate void MyDelegate(int x, int y);:定义一个委托类型。
MyDelegate del = (x, y) => Console.WriteLine(x + y);:使用Lambda表达式创建一个委托实例。