主要是实现将类里面 的属性和对应的值转换为字典的键和值。

public class RDfsedfw
{
/// <summary>
/// 将匿名类转换为字典
/// </summary>
/// <returns></returns>
public Dictionary<string,object> ClassToDic()
{
var data = new
{
action = "GetProperties",
ip ="192.168.1.150",
encryp = ,
date = DateTime.Now,
model = "cash",
sn = "",
vs=""
};
var d = data.GetType().GetProperties()//这一步获取匿名类的公共属性,返回一个数组
.OrderBy(q => q.Name)//这一步排序,需要引入System.Linq,当然可以省略
.Where(q => q.Name!="vs")//这一步筛选,也可以省略
.ToDictionary(q => q.Name, q => q.GetValue(data));//这一步将数组转换为字典
return d;
}
public Dictionary<string,object> ClassToDic1()
{
DataModel dm1 = new DataModel()
{
Str = "abc123",
Date = DateTime.Now,
Chensf = ,
Dic = new Dictionary<string, string>() { { "ID", "" }, { "name", "mike" } },
Gname = "fffffs"//这个字段由于不是属性,是不能被GetProperties转换为数组元素的
};
var d = dm1.GetType().GetProperties()
.ToDictionary(q=>q.Name,q=>q.GetValue(dm1));
return d;
}
}
public class DataModel
{
public string Str { set; get; }
public DateTime Date { set; get; }
public int Chensf { set; get; }
public Dictionary<string ,string> Dic { set; get; } public string Gname;
}

C#将类对象转换为字典的更多相关文章

  1. python-类对象以字典模式操作

    #类对象以字典模式操作 class Person: def __init__(self): self.cache={} def __setitem__(self, key, value): #增加或修 ...

  2. python]用eval强制将字符串转换为字典变量时候出错:NameError: name 'null' is not defined[python]用eval函数 字符串转dict

    本博客已搬家至个人网站 在路上 - On the way 下面的 技术 分类. 你可以通过点击 更新帖子 [已解决]Python中,用eval强制将字符串转换为字典变量时候出错:NameError: ...

  3. 福利->KVC+Runtime获取类/对象的属性/成员变量/方法/协议并实现字典转模型

    我们知道,KVC+Runtime可以做非常多的事情.有了这个,我们可以实现很多的效果. 这里来个福利,利用KVC+Runtime获取类/对象的所有成员变量.属性.方法及协议: 并利用它来实现字典转模型 ...

  4. [转载]python中将普通对象作为 字典类(dict) 使用

    目前我知道的有两种方法: 1 定义的类继承dict类 例如 class A(dict): pass a = A() a['name'] = 12 2 给自定义的类添加 __setitem__() __ ...

  5. 将实体类、匿名对象转换为SqlParameter列表

    /// <summary> /// <remarks> /// <para>将实体类/匿名对象转换为SqlParameter列表</para> /// ...

  6. 利用JSON将Map转换为类对象

    Map类型做为一种常见的Java类型,经常在开发过程中使用,笔者最近遇到要将Map对象做为一种通用的参数变量,下传到多个业务类方法中,然后在各个业务类方法中将Map转换为指定类对象的情况.如何将Map ...

  7. RestTemplate将响应数据转换为具有泛型的类对象

    前言: 重要,RestTemplate在SpringBoot项目里即便通过HttpMessageConverters添加了Fastjson且优先级比jackson要高也不会在RestTemplate里 ...

  8. 微信开发,对象转换为xml时候引用XStream这个类报错处理方案

    报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved. /**  * 扩展XStream 支持CDATA  */ privat ...

  9. 什么是“类数组对象”,在jquer中怎样将类数组对象转换为数组对象

    类数组对象的定义: 所谓"类数组对象"就是一个常规的Object对象,如$("div")但它和数组对象非常相似:具备length属性, 并以0.1.2.3……等 ...

随机推荐

  1. C++字符串读入

    int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } int main() { ...

  2. HDU1166 线段树裸题 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. Google Protocol Buffer 的使用(二)

    一.protobuf应用场景 protobuf 在Java中的应用场景可以是序列化和反序列化,流可以通过文件或者通过网络TCP/UDP等方式传输.新建一个.proto文件 syntax = " ...

  4. Rooks-LightOj1005(规律)

    A rook is a piece used in the game of chess which is played on a board of square grids. A rook can o ...

  5. c++ stl 使用汇总(string,vector,map,set)

    1.string 1>substr(),截取字串的方法.返回一个从指定位置开始,并具有指定长度的子字符串.参数 start(必选),所需的子字符串的起始位置.字符串中第一个字符的索引为 0.le ...

  6. linux 虚拟网卡

    linux中可以通过一个物理网卡,模拟出多个虚拟网卡,并在网卡中配置ip. 下面做一个实验. 实验描述: 我们有server A (ip 10.79.148.205),server B (10.79. ...

  7. vi,vim的基本使用方法

    "i”插入 "/" 查找 "wq"保存退出 "q!"不保存退出

  8. antd 的 Table 遇到的 bug

    1.报错情况 /* autoprefixer: off */ filter: progid\:DXImageTransform\.Microsoft\.Blur(PixelRadius\=1, Mak ...

  9. ABAP学习之旅——多种方式建立模块化功能

    在ABAP中.有多种方法能够建立模块化的功能. 以下依次对其种三种进行介绍. 一.            使用子程序(Subroutine) 1.      基本的语法: FORM subname. ...

  10. 99_leetcode_Best Time to Buy and sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...