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. 【MySQL】索引和锁

    前言 本文摘自数据库两大神器[索引和锁] 声明:如果没有说明具体的数据库和存储引擎,默认指的是MySQL中的InnoDB存储引擎 索引 在之前,我对索引有以下的认知: 索引可以加快数据库的检索速度 表 ...

  2. python基础(一)—— 核心数据类型

    Hello World程序 [root@mysql ~]# python3 Python 3.6.5 (default, Jul  8 2018, 11:41:23) [GCC 4.4.7 20120 ...

  3. windows本机域名配置

    路径: C:\Windows\System32\drivers\etc打开hosts文件如下: # Copyright (c) - Microsoft Corp. # # This is a samp ...

  4. angularJS transclude

    参考来源:彻底弄懂AngularJS中的transclusion 对以上文章进行摘录.总结和测试记录 在使用指令的时候,如果想要使用指令中的子元素,那么你就要用transclusion. 指令的DDO ...

  5. scrapy yield 回调函数不执行解决方案

    yield Request(url=parse.urljoin(response.url, p_url),callback=self.parse_detail) 回调函数不执行: 加上: dont_f ...

  6. 剑指Offer(书):矩阵中的路径

    题目: * 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.* 路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.* 如果一条路径经 ...

  7. 【pwnable】asm之write up

    首先查看源代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <s ...

  8. 【UVA 11181】(条件概率)

    题链:https://cn.vjudge.net/problem/UVA-11181 题意 n个人去了超市,已知每个人买东西的概率为p[i],在已知有r个人买了东西的情况下,求实际上每个人买东西的概率 ...

  9. 大数据学习——flume安装部署

    1.Flume的安装非常简单,只需要解压即可,当然,前提是已有hadoop环境 上传安装包到数据源所在节点上 然后解压  tar -zxvf apache-flume-1.6.0-bin.tar.gz ...

  10. 大数据学习——mapreduce案例join算法

    需求: 用mapreduce实现select order.orderid,order.pdtid,pdts.pdt_name,oder.amount from orderjoin pdtson ord ...