ExpandoObject :
动态的增删一个对象的属性,在低层库(比如ORM)中非常实用。
因为ExpandoObject实现了IDictionay<string, object>接口,常见的一种使用方法是,把expando转成dictionary,动态添加属性名和值[key,value],expando就达到了动态属性的目的。  演示样例代码(using System.Dynamic):

dynamic expando = new ExpandoObject();

	expando.A = "a";
expando.B = 1; // A,a
// B,1
IDictionary<string, object> dict = expando as IDictionary<string, object>;
foreach(var e in dict){
Console.WriteLine(e.Key + ","+ e.Value);
} dict.Add("C", "c");
// c
Console.WriteLine(expando.C);

DynamicObject 类:
当须要track一个对象的属性被get或set时。这个类是非常好的候选。 (通常出如今AOP的设计中,假设不打算使用AOP框架比如POSTSHARP的话)。演示样例代码:

void Main()
{
dynamic obj = new MyDynaObject();
obj.A = "a";
obj.B = 1;
var a = obj.A;
// should print :
// tring to set member : A, with value : a
// tring to set member : B, with value : 1
// tring to get member : a
} public class MyDynaObject : DynamicObject
{
// The inner dictionary.
Dictionary<string, object> dictionary
= new Dictionary<string, object>(); // This property returns the number of elements
// in the inner dictionary.
public int Count
{
get
{
return dictionary.Count;
}
} // If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{ // Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower(); Console.WriteLine("tring to get member : {0}", name); // If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
} // If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value; Console.WriteLine("tring to set member : {0}, with value : {1}", binder.Name, value); // You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
} // Define other methods and classes

DLR之 ExpandoObject和DynamicObject的使用演示样例的更多相关文章

  1. JDBC连接MySQL数据库及演示样例

    JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

  2. java 覆盖hashCode()深入探讨 代码演示样例

    java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...

  3. 模式识别 - 处理多演示样例学习(MIL)特征(matlab)

    处理多演示样例学习(MIL)特征(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习( ...

  4. java并行调度框架封装及演示样例

    參考资料:  阿里巴巴开源项目 CobarClient  源代码实现. 分享作者:闫建忠 分享时间:2014年5月7日 ---------------------------------------- ...

  5. Java连接redis的使用演示样例

    Java连接redis的使用演示样例 Redis是开源的key-value存储工具,redis通经常使用来存储结构化的数据,由于redis的key能够包括String.hash.listset和sor ...

  6. Introspector(内省)简单演示样例 与 简单应用

    简单演示样例: package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; ...

  7. libcurl使用演示样例

    简要说明:C++使用libcurl訪问"www.baidu.com".获取返回码和打印出http文件 /* * @ libcurl使用演示样例 * @ 2014.04.29 * @ ...

  8. 构造Scala开发环境并创建ApiDemos演示样例项目

    从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...

  9. OpenCV LDA(Linnear Discriminant analysis)类的使用---OpenCV LDA演示样例

    1.OpenCV中LDA类的声明 //contrib.hpp class CV_EXPORTS LDA { public: // Initializes a LDA with num_componen ...

随机推荐

  1. Django框架基础知识03-模板变量及模板过滤器

    模板变量及模板过滤器. 1.模板路径的查找 -查找顺序 1.尝试,在app目录下存放模板. -两种方案 1.app项目文件夹下存放. 2.templates文件夹下分类存放. 首先查找项目settin ...

  2. python re 正则表达式

    元字符和其含义 . 匹配除换行符以外的任意字符 \ 转义字符,使后一个字符改变原来的意思 \w 匹配字母.数字.下划线:[A-Za-z0-9_] \W 匹配特殊字符:[^A-Za-z0-9_] \s ...

  3. 【51nod 1791】 合法括号子段

    有一个括号序列,现在要计算一下它有多少非空子段是合法括号序列. 合法括号序列的定义是: 1.空序列是合法括号序列. 2.如果S是合法括号序列,那么(S)是合法括号序列. 3.如果A和B都是合法括号序列 ...

  4. luogu2770 航空路线问题

    前置技能:HDU3376 Matrix Again 所以看到这个题,我们也会想着用最大费用最大流解决,因为从起点飞到终点再飞回来,就等于从起点飞两次到终点且这两次飞行除了起点终点之外没有访问超过一次的 ...

  5. STM32F407 STLINK 在线调试 个人笔记

    配置的部分请看本博客STM32分类下的环境配置篇目 开始调试 一些按键 查看寄存器 查看变量值 选中变量,右键,add to watch

  6. pip安装requests库失败

    pip install 安装第三方插件是出现Could not fetch URL https://pypi.python.org/simple/pool/: There was a problem ...

  7. 钓鱼(洛谷 P1717)

    题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请他吃一次“年年大丰收”,为了表示诚意,他还决定亲自去钓鱼,但是,因为还要准备2013NO ...

  8. 背包!背包!HDU 2602 Bone Collector + HDU 1114 Piggy-Bank + HDU 2191 512

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 第一题 01背包问题 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  9. [NOIP1999] 提高组 洛谷P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  10. 【Educational Codeforces Round 48】

    A:https://www.cnblogs.com/myx12345/p/9843001.html B:https://www.cnblogs.com/myx12345/p/9843021.html ...