c# 反射获取属性值 TypeUtils
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text; namespace xxx.Common
{
static class TypeUtilities
{
public static List<T> GetAllPublicConstantValues<T>(this Type type)
{
return type
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(T))
.Select(x => (T)x.GetRawConstantValue())
.ToList();
}
public static T[] Concat<T>(this T[] x, T[] y)
{
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
int oldLen = x.Length;
Array.Resize<T>(ref x, x.Length + y.Length);
Array.Copy(y, , x, oldLen, y.Length);
return x;
}
public static Object GetPropValue(this Object obj, String name)
{
foreach (String part in name.Split('.'))
{
if (obj == null) { return null; } Type type = obj.GetType();
PropertyInfo info = type.GetProperty(part);
if (info == null) { return null; } obj = info.GetValue(obj, null);
}
return obj;
} public static T GetPropValue<T>(this Object obj, String name)
{
Object retval = GetPropValue(obj, name);
if (retval == null) { return default(T); } // throws InvalidCastException if types are incompatible
return (T)retval;
}
public static void SetPropValue<T>(this object obj,string property, string value)
{
if (obj == null) { return ; }
Type type = obj.GetType();
type.GetProperty(property).SetValue(obj, value, null);
}
public static bool IsPositiveInteger(this string str)
{
int num;
if(Int32.TryParse(str,out num))
{
return num > ;
}
return false;
}
public static bool IsPositiveIntegerOrZero(this string str)
{
int num;
if (Int32.TryParse(str, out num))
{
return num >= ;
}
return false;
}
public static bool IsNegaitiveInteger(this string str)
{
int num;
if (Int32.TryParse(str, out num))
{
return num < ;
}
return false;
}
public static bool IsZero(this string str)
{
int num;
if (Int32.TryParse(str, out num))
{
return num == ;
}
return false;
}
}
}
c# 反射获取属性值 TypeUtils的更多相关文章
- C# 反射获取属性值、名称、类型以及集合的属性值、类型名称
实体类 class Product { public string Id { get; set; } public string Name { get; set; } public List<P ...
- C#反射获取属性值和设置属性值
/// /// 获取类中的属性值 /// public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj ...
- java 反射获取属性值 方法
public static void main(String[] args) throws SecurityException, ClassNotFoundException, IllegalArgu ...
- 反射获取属性DisplayName特性名字以及属性值
/// <summary> /// 反射获取所有DisplayName标记值 /// </summary> /// <typeparam name="T&quo ...
- storm源码之巧用java反射反序列化clojure的defrecord获取属性值
[原创]storm源码之巧用java反射反序列化clojure的defrecord获取属性值 [原创]storm源码之巧用java反射反序列化clojure的defrecord获取属性值 storm源 ...
- java 反射机制--根据属性名获取属性值
1.考虑安全访问范围内的属性,没有权限访问到的属性不读取 /** * 根据属性名获取属性值 * * @param fieldName * @param object * @return */ priv ...
- HtmlAgilityPack中使用xpath获取属性值
HtmlAgilityPack介绍 HtmlAgilityPack是一个专门用来解析Html的库,它可以使用xml的方式来解析html. 有人说了,html本身不就是xml?是的,html就是xml, ...
- 【原】storm源码之巧用java反射反序列化clojure的defrecord获取属性值
storm源码是clojure.java.python的混合体.在解决storm-0.8.2的nimbus单点问题的过程中需要从zookeeper上读取目前storm集群中正在运行的assignmen ...
- C#反射设置属性值和获取属性值
/// /// 获取类中的属性值 /// /// /// /// public string GetModelValue(string FieldName, object obj) { try { T ...
随机推荐
- 3D Computer Grapihcs Using OpenGL - 09 Enable Depth Test
启用Depth Test OpenGL是个3D绘图API,也就是说不只有xy坐标轴,还有第三个坐标轴z,z轴的方向是垂直于屏幕,指向屏幕内. 靠近人眼的方向是负方向,标准化设备坐标的最小值是-1, 最 ...
- Solr集群环境搭建
一.准备工作 首先保证已经安装JDK工具包: [root@localhost opt]# java -version java version "1.8.0_144" Java(T ...
- 力扣60——第k个排列
原题 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: 1. "123" 2. &qu ...
- 线段树板子1(洛谷P3372)
传送 一道线段树板子(最简单的) 似乎之前在培训里写过线段树的样子?不记得了 何为线段树? 一般就是长成这样的树,树上的每个节点代表一个区间.线段树一般用于区间修改,区间查询的问题. 我们如何种写一棵 ...
- duration of lease 1 0.5 0.875 DHCP 租借时间 续租时间 重新绑定时间
w
- DRF 组件
DRF组件中的认证 授权 频率限制 分页 注册器 url控件
- 使用wxpy模块了解微信好友
网上看了一篇python文章,内容简单有趣,正好可以练习一下.原文连接:http://mp.weixin.qq.com/s/oI2pH8uvq4kwYqc4kLMjuA 一.环境:Windows+py ...
- sqlserver通过select查询出连续的日历临时表
首先我们需要用到这个 select * FROM master..spt_values n WHERE n.type = 'p' AND n.number <= 7 里面分几个列,我们需要连续的 ...
- python 正则表达式 re.split
内置函数split与re库中的split,有很多相似处 #!use/bin/python #coding:utf-8 import re str= "https://i.cnb1logs.c ...
- 前端004/React常用UI组件
每天进步一点点〜 Ant Design of React //蚂蚁金服设计平台.需要应用何种类型组件可参考API React + mobx + nornj 开发模式文件说明: [1].A.t.html ...