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 ...
随机推荐
- 前端-Vue基础1
Vue核心思想:只要改变数据,页面就会发生改变 1.引入vue 1.下载vue.js 2.在script标签的src属性中,引入vue.js <script src="js/vue.j ...
- java集合(4)-Set集合
Set集合,类似于一个罐子,程序可以把多个对象"丢进"Set集合,而Set集合通常不能记住每个元素的添加顺序.Set集合与Collection基本相同,没有提供任何额外的方法.实际 ...
- Pytest单元测试框架之setup/teardown模块示例操作
"""模块级(setup_module/teardown_module)开始于模块始末,全局的函数级(setup_function/teardown_function)只 ...
- 前端之html基础演示
1.本地服务:下载淘宝镜像node.js :https://npm.taobao.org/mirrors/npm :本次下载的版本是 v10.0.0 2.下载成功后,到cmd窗口输入 node -v, ...
- github在不同电脑上协同开发
当我换了电脑后,开发自己的github项目遇到了一些问题. 首先,git clone 'repository url'拉取下来项目,开始开发项目发.修改了一些文件后,当要git commit, git ...
- 带标签的for循环
for循环可以加标签,使用break或者continue时,若存在多层嵌套循环可指定标签的for循环 public class ForLabel { public static void main(S ...
- 如何用C++自己实现mysql数据库的连接池?
为什么是mysql? 现在几乎所有的后台应用都要用到数据库,什么关系型的.非关系型的:正当关系的,不正当关系的:主流的和非主流的, 大到Oracle,小到sqlite,以及包括现在逐渐流行的基于物联网 ...
- 构建前端第8篇之---Webstom搭建ES6运行环境
张艳涛 写于2021-1-22 一.在有webstorm和node.js前提下,安装全局的babel npm install babel-cli babel-eslint -g 二.在terminal ...
- php 随机生成字符串
private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK ...
- azure bash: kubectl: command not found
[root@***]# az aks install-cliDownloading client to "/usr/local/bin/kubectl" from "ht ...