笔记—利用反射给对象赋值

模型类:TestModel

public class TestModel

{

private string _name;

private int _id;

public TestModel()

{}

public string Name

{

get { return _name; }

set { _name = value; }

}

public int Id

{

get { return _id; }

set { _id = value; }

}

}

 

逻辑类:TestGetValue

public class TestGetValue

{

public TestGetValue()

{}

public string GetValue(TestModel tm)

{

return tm.Name + " In TestGetValue ";

}

}

 

页面:TestReflection.aspx.cs

void Rel()

{

 

Type t = typeof(TestModel);

TestModel tm = new TestModel();

t.GetProperty("Name").SetValue(tm, "BackStreetBoy", null);

 

TestGetValue tg = new TestGetValue();

Response.Write(tg.GetValue(tm));

}

 

输出结果:

    BackStreetBoy In TestGetValue

设值成功。窃喜…

posted @ 2008-09-04 12:32  wlhc_Jing  阅读(399)  评论(0编辑  收藏  举报