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 ...
随机推荐
- 「AGC020F」 Arcs on a Circle
「AGC020F」 Arcs on a Circle Link 这个题非常 Amazing 啊.果然AtCoder全是智商题 首先你可以注意到数据范围真的是小得离谱,让你想要爆搜. 然后你发现不可做, ...
- 团队开发day05
在进行网络间的通信中,需要开启线程来实现网络连接, 但是在activity中无法拿到获取到的数据,数据只能在网络线程中查看 解决:通过Handler在线程之间进行通信,传递获取到的信息 Handler ...
- 家庭账本开发day10
系统的增删改查基本功能完成,进行业务流程完整测试.完善相关功能,编写搜索 功能,通过日期,类型等关键字进行搜索,对搜索到的数据表格完成重载 // 监听搜索操作 form.on('subm ...
- SpringBoot自动装配-自定义Start
SpringBoot自动装配 在没有使用SpringBoot之前,使用ssm时配置redis需要在XML中配置端口号,地址,账号密码,连接池等等,而使用了SpringBoot后只需要在applicat ...
- .net core番外第2篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入
本篇文章接前一篇,建议可以先看前篇文章,再看本文,会有更好的效果. 前一篇跳转链接:https://www.cnblogs.com/weskynet/p/15046999.html 正文: Autof ...
- 极致简洁的微前端框架-京东MicroApp开源了
前言 MicroApp是一款基于类WebComponent进行渲染的微前端框架,不同于目前流行的开源框架,它从组件化的思维实现微前端,旨在降低上手难度.提升工作效率.它是目前市面上接入微前端成本最低的 ...
- Gos Log每次查询响应后自动清理临时文件,优化磁盘空间
客户端清理 logc/controllers/file/file.go 压缩后清理原始文件 //压缩成功后 删除原文件 os.Remove(src) 返回后清理压缩文件 defer func() { ...
- Python基础之PyQt5关闭界面
想让执行完程序后自动关闭窗口,而不用点击右上角叉叉的方法是self.close(),具体应用还是以treewidget为例. 前面我们写了一个treewidget的界面,并且实现了界面代码分离,具体实 ...
- 第五篇--VS2017如何生成Dll文件
参考资料: https://blog.csdn.net/qq_34097715/article/details/79540933 https://www.cnblogs.com/RascallySna ...
- OVERLAPPED 结构
typedef struct _OVERLAPPED { ULONG_PTR Internal; ULONG_PTR InternalHigh; union { struct { DWORD Offs ...