class Program
{
static void Main(string[] args)
{
Func<int, int> a = x => x + 1;//写一个方法给变量a
Console.WriteLine(a(6));//调用此方法
Program p = new Program();
var ps = p.test(new Parent(1), x => x.p + 1);//调用泛型函数
foreach (var item in ps)
{
Console.WriteLine(item.p);
}
var obj = new { dcid = "test", name = "mytest", nodeText = 89 };//匿名对象
string dcid = obj.GetType().GetProperty("dcid").GetValue(obj).ToString();
string name = obj.GetType().GetProperty("name").GetValue(obj).ToString();
string nodeText = obj.GetType().GetProperty("nodeText").GetValue(obj).GetType().ToString();
Console.WriteLine(dcid);
Console.WriteLine(name);
Console.WriteLine(nodeText);
Console.ReadKey();
}
public T1[] test<T1>(T1 x, Func<T1, int> k) where T1 : new()//当传入x的时候,已经能检测出T1为Parent了.当然,在此函数里面是不确定的
{
List<T1> ts = new List<T1>();
int count = k(x);
for (int i = 0; i < count; i++)
{
ts.Add(new T1());//因为有where new()的修饰,不然不允许这样操作的
}
return ts.ToArray();
}
}
class Parent
{
public int p = 20;
public Parent(int value)
{
this.p = value;
}
public Parent()//必须有这个,不然做为T的时候不能被new了
{
}
}
网友评论