LINQ to Reflection反射机制
2010-12-02 07:06:56 来源:WEB开发网核心提示:1.如何获取一个属性,并进行取值、赋值?using Sparrow.Reflection; [TestMethod] public void set_property_value() { var obj = new MockObject { Name = "dayi", Blog = &
1.如何获取一个属性,并进行取值、赋值?
- using Sparrow.Reflection;
- [TestMethod]
- public void set_property_value()
- {
- var obj = new MockObject { Name = "dayi", Blog = "http://walkingboy.cnblogs.com", Location = "XiaMen" };
- var property = obj.Metadata().Properties.Where(i => i.Name == "Location").FirstOrDefault();
- var changedLocation = "Xiamen,China";
- //get value
- //var value = property.GetValue(obj);
- property.SetValue(obj, changedLocation);
- Assert.AreEqual(changedLocation, obj.Location);
- }
2.如果获取一个字段的值?
- using Sparrow.Reflection;
- [TestMethod]
- public void get_field_value()
- {
- var obj = new MockObject();
- var field = obj.Metadata().Fields.Where(i => i.Name == "Country").FirstOrDefault();
- Assert.AreEqual("China", field.GetValue(obj));
- }
3.如何获取一个自定义CustomAttribute?
- using Sparrow.Reflection;
- [TestMethod]
- public void get_custom_attribute_data() {
- var obj = new MockObject { Name = "dayi", Blog = "http://walkingboy.cnblogs.com", Location = "XiaMen" };
- var attribute = obj.Metadata().Properties
- .Where(i => i.Name == "Name")
- .SelectMany(i => i.Attributes)
- .Select(i=>i.Attribute)
- .OfType<MockAttribute>()
- .FirstOrDefault();
- Assert.AreEqual("this is the name", attribute.Name);
- }
4.如何调用一个指定名称的Method?
- using Sparrow.Reflection;
- [TestMethod]
- public void invoke_method()
- {
- var obj = new MockObject();
- var method = obj.Metadata().Methods.Where(i => i.Name == "SayHello").FirstOrDefault();
- Assert.AreEqual("Hi,world",method.Invoke(obj,new []{"world"}));
- }.Proxy()
Tags:LINQ to Reflection
编辑录入:爽爽 [复制链接] [打 印][]
- ››LINQ to Reflection反射机制
- ››TOscilloscope 仿Windows任务管理器CPU使用记录组...
- ››linq 查询分页实例
- ››tomcat不支持TCP/IP6协议
- ››tomcat 下的 url 大小写问题
- ››tomcat6.0.28 内存溢出PermGen Space
- ››Tomcat 系统架构与设计模式,第 2 部分: 设计模式...
- ››Tomcat 系统架构与设计模式,第 1 部分: 工作原理...
- ››TOMCAT和IIS整合
- ››Tomcat性能调优方案
- ››Tomcat6 下 MySQL 5.1 数据源配制
- ››Tomcat启动分析server.xml
更多精彩
赞助商链接