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 ...
随机推荐
- uploadify加ASP.NET MVC3.0上传文件(可多条)
页面代码: <div id="fileQueuePlug"></div> <input type="file" name=&quo ...
- oracle常用sql语句和函数
--查询表的字段数 select count(*) from user_tab_columns where table_name = '表名'; --查询数据库用户密码的profile(一般为defa ...
- Openstack 通过 SQLAlchemy-ORM 访问数据库
目录 目录 Demo SQLAlchemy 数据库的初始化 数据库的操作实现 数据库的操作请求 全部查询 单个查询 创建 更新 删除 Demo Github/JmilkFan/my-code-repe ...
- Delphi XE2 之 FireMonkey 入门(29) - 数据绑定: TBindingsList: 表达式的 Evaluate() 方法
Delphi XE2 之 FireMonkey 入门(29) - 数据绑定: TBindingsList: 表达式的 Evaluate() 方法 TBindingsList 中可能不止一个表达式, 通 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_5_BufferedWriter_字符缓冲输出流
使用newLine来换行 同样的效果 println的源码里面其实就用的就是newLine()
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- jvm jstack log分析工具,在线分析
http://spotify.github.io/threaddump-analyzer Spotify提供的Web版在线分析工具,可以将锁或条件相关联的线程聚合到一起.
- C++笔记(5)——浮点数的比较
判断是否相等 因为一个浮点数的存储并不总是精确的,例如在经过大量计算之后可能会将3.14保存为3.1400000000001或者3.1439999999999,这时候如果直接用==来比较这两个数的话会 ...
- c++中关于类的长度的猜想
在无意中,我偶然发现了类的长度并不是由函数的类型及个数决定,也并非是2的倍数.4的倍数. 在翻阅资料中,我得出了一些我认为可能的猜想. 我们先来看一串代码 #include<iostream&g ...
- STS安装Drools
download drools (including "Drools Engine" & "Drools and jBPM tools") from o ...