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 ...
随机推荐
- linux 下 一步一步安装odb
Introduction This guide presents step-by-step instructions for installing the ODB system on UNIX-lik ...
- pl/sql的安装使用详解
一. 先安装 orcale10.1客户端 setup右键属性,按下图设置 net manager 设置,不设置 pl/sql developer没办法连接 二.再安装 pl/sql develo ...
- sqlite时间类型
SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么 ...
- RAM: Residual Attention Module for Single Image Super-Resolution
1. 摘要 注意力机制是深度神经网络的一个设计趋势,其在各种计算机视觉任务中都表现突出.但是,应用到图像超分辨领域的注意力模型大都没有考虑超分辨和其它高层计算机视觉问题的天然不同. 作者提出了一个新的 ...
- 《图解 CSS3 核心技术与案例实战》
第一章 解开 CSS3 的面纱 使用 CSS3 的好处 减少开发和维护成本:如传统实现圆角边框需要绘图.切图才能完成,而使用 css 可以直接完成 提高页面性能 渐进增强(Progressive En ...
- vue组件生命周期
分为4个阶段:create/mount/update/destroy 每一个阶段都对应着有自己的处理函数 create: beforeCreate created 初始化 mount: beforeM ...
- shader例子
1.水波涟漪:https://zhuanlan.zhihu.com/p/47204844 2.shaderToy转unity: https://zhuanlan.zhihu.com/p/5228708 ...
- lambda表达式(2)
转:http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html "Lambda表达式"是一个匿名函数,是一种高效 ...
- SpringBoot(十) -- Spring Data
一.Spring Data简介 Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问技术,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关 ...
- hdu-2389.rain on your parade(二分匹配HK算法)
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...