C#基础知识---动态为类型添加属性
一、概述
通常情况下,我们是事先在类型中定义好属性的,但有时候,我们需要动态为一个类型添加某些属性,这个时候,我们就需要使用DynamicObject类型了。
二、Demo
using System;
using System.Collections.Generic;
using System.Dynamic; namespace ConsoleDynamicModel
{
public class DynamicModel : DynamicObject
{
private string propertyName;
public string PropertyName
{
get { return propertyName; }
set { propertyName = value; }
} // The inner dictionary.
Dictionary<string, object> dicProperty
= new Dictionary<string, object>();
public Dictionary<string, object> DicProperty
{
get
{
return dicProperty;
}
} // This property returns the number of elements
// in the inner dictionary.
public int Count
{
get
{
return dicProperty.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; // If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dicProperty.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.
if (binder.Name == "Property")
{
dicProperty[PropertyName] = value;
}
else
{
dicProperty[binder.Name] = value;
} // You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("动态为类型添加属性");
dynamic dynamicModel = new DynamicModel();
List<string> myList = new List<string>();
myList.Add("Name");
myList.Add("Age");
myList.Add("Hobby"); List<string> myValueList = new List<string>();
myValueList.Add("Mary");
myValueList.Add("18");
myValueList.Add("Dance"); for (int i = 0; i < myList.Count; i++)
{
string myAttr = myList[i];
dynamicModel.PropertyName = myAttr;
dynamicModel.Property = myValueList[i];
} Console.WriteLine($"Name: {dynamicModel.Name}");
Console.WriteLine($"Age: {dynamicModel.Age}");
Console.WriteLine($"Hobby: {dynamicModel.Hobby}");
}
}
}
C#基础知识---动态为类型添加属性的更多相关文章
- java 基础知识二 基本类型与运算符
		
java 基础知识二 基本类型与运算符 1.标识符 定义:为类.方法.变量起的名称 由大小写字母.数字.下划线(_)和美元符号($)组成,同时不能以数字开头 2.关键字 java语言保留特殊含义或者 ...
 - javascript的基础知识及面向对象和原型属性
		
自己总结一下javascript的基础知识,希望对大家有用,也希望大家来拍砖,毕竟是个人的理解啊 1.1 类型检查:typeof(验证数据类型是:string) var num = 123; cons ...
 - JS基础知识(基本类型 引用类型)
		
1,js中的 基本类型 引用类型 javascript中有两种变量类型:基本类型和引用类型,基本类型包括:Number.String.Undefined.Null.Boolean这五种,而引用类型 ...
 - Js基础知识2-对象、对象属性全解
		
Object对象 Object对象包含如下属性和方法,也就意味着一切对象(函数也是对象)都包含如下方法. 每种方法和属性在不同的对象中有不同的作用,并不是每种对象都有使用每个方法的必要. 下面是Obj ...
 - JAVA“动态”为类添加属性
		
部分参考:http://www.cnblogs.com/zy2009/p/6725843.html pom.xml中添加: <dependency> <groupId>comm ...
 - runTime动态给类添加属性
		
#项目中需要给系统类添加属性 #需要注意的地方就是.m中 set 和 get ,get方法中方法名和添加的属性名一致,set中可以用驼峰 #import <UIKit/UIKit.h> ...
 - python基础知识4--数据类型与变量
		
阅读目录 一.变量 二.数据类型 2.1 什么是数据类型及数据类型分类 2.2 标准数据类型: 2.2.1 数字 2.2.1.1 整型: 2.2.1.2 长整型long: 2.2.1.3 布尔bool ...
 - .NET基础知识(01)-值类型与引用类型
		
常见面试题目: 1. 值类型和引用类型的区别? 2. 结构和类的区别? 3. delegate是引用类型还是值类型?enum.int[]和string呢? 4. 堆和栈的区别? 5. 什么情况下会在堆 ...
 - C#基础知识之Dynamic类型
		
Dynamic类型是C#4.0中引入的新类型,它允许其操作掠过编译器类型检查,而在运行时处理. 编程语言有时可以划分为静态类型化语言和动态类型化语言.C#和Java经常被认为是静态化类型的语言,而Py ...
 
随机推荐
- C语言:GB2312编码和GBK编码,将中文存储到计算机
			
计算机是一种改变世界的发明,很快就从美国传到了全球各地,得到了所有国家的认可,成为了一种不可替代的工具.计算机在广泛流行的过程中遇到的一个棘手问题就是字符编码,计算机是美国人发明的,它使用的是 ASC ...
 - C语言:最大公约数
			
//求最大公约数 #include <stdio.h> main() { int m,n,i,k; scanf("%d,%d",&m,&n); k=m& ...
 - Python之一行代码将网址URL转换成动态彩色二维码
			
先在 pycharm 安装 myqr.或者,Python3 必装,然后命令行 pip install myqr 也可. 将我的微信公众号网址:http://weixin.qq.com/r/hRMQC ...
 - Spring总结之事务
			
Spring事务 1)定义 事务是指多个操作单元组成的集合,多个操作单元是整体不可分割的,要么都成功,要么都不成功.必须遵守四个原则(ACID) ●原子性(Atomicity):即事务是不可分割的最小 ...
 - python -- 模块与类库
			
一.模块 模块(Module)是由一组类.函数和变量组成的,模块文件的扩展名是.py或.pyc 在使用模块之前,需要先使用import语句导入这个模块. 语法格式如下: import 模块名 from ...
 - CH1809 匹配统计 题解
			
看了好久才懂,我好菜啊-- 题意:给两个字符串 \(a\) 与 \(b\),对于 \(q\) 次询问,每次询问给出一个 \(x\),求存在多少个位置使得 \(a\) 从该位置开始的后缀子串与 \(b\ ...
 - python 连接远程服务器,修改时间
			
import paramiko from datetime import date, timedelta def set_time(hostname): ssh = paramiko.SSHClien ...
 - odoo里的开发案例
			
1.模块命名[驼峰命名方法] res开头的是:resources 常见模型:res.users, res.company, res.partner, res.config.setti ...
 - 创建函数,传递一个数字n,返回斐波那契数列的第n的值。
			
斐波那契数列 第1项和第2项的值是1,从第3项开始,每项的值是前两项相加的和 1 1 2 3 5 8 13 21...... 法1: function fn(n) ...
 - C# 事件与继承
			
在窗体编程过程中,常常会封装一个基类,包含未来业务中常用的属性.方法.委托.事件等,但是事件作为一个特殊的委托,只能在声明类中调用,派生类都不可以调用,所以在基类中必须实现一个虚函数,实现事件的调用, ...