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 ...
随机推荐
- 基于FPGA的图像镜像
图像镜像,一种较为常见的图像处理操作,分为水平镜像.垂直镜像.对角镜像.水平镜像即处理后的图像与原图像关于垂直线对称,垂直镜像为处理后的图像与 原图像关于水平线对称,对角镜像则关于对角线对称. 关于低 ...
- SpringMVC架构(一)
SpringMVC架构 1.1Spring web mvc介绍 Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来 ...
- Greenplum安装总结
Greenplum安装总结 一.环境说明 服务器centos7 4台,一台Master节点,三台Segment节点: mdw 192.168.43.21 (master节点) sdw1 192.168 ...
- python + mysql 实现创建数据表
import pymysql"""1.连接本地数据库2.建立游标3.创建表4.插入表数据.查询表数据.更新表数据.删除表数据"""def c ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- 分布式ID生成器(CosId)的设计与实现
分布式ID生成器(CosId)设计与实现 CosId 简介 CosId 旨在提供通用.灵活.高性能的分布式 ID 生成器. 目前提供了俩类 ID 生成器: SnowflakeId : 单机 TPS 性 ...
- C#计算复利方法
复利即是指利滚知利 如存入1000,年利息回0.003,存了答10年,则调用fl(0.003,1000,10); double fl(double rate,double cash,int times ...
- Python基础之读写xml总结
参考文章:https://blog.csdn.net/weixin_42749767/article/details/82770563 先介绍xml.dom.minidom包,有一个读写的例子 rea ...
- Python小白的数学建模课-15.图论基本概念
图论中所说的图,不是图形图像或地图,而是指由顶点和边所构成的图形结构. 图论不仅与拓扑学.计算机数据结构和算法密切相关,而且正在成为机器学习的关键技术. 本系列结合数学建模的应用需求,来介绍 Netw ...
- sql注入漏洞笔记随笔
sql注入是从1998年出现的,是一个十分常见的漏洞,它是OWASP top10的第一名(注入) 在了解sql注入之前,我们需要先了解web框架 webapp:web网站,这种方式它采用的是B/S架构 ...