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 ...
随机推荐
- 电脑配置Java环境变量之后,在cmd中仍然无法识别
在电脑上配置了Java的环境变量,但是在cmd框中仍然无法识别: 解决方法:cmd.exe右键---以管理员身份运行,即可识别
- 利用Javascript解决HTML大数据列表引起的网页加载慢/卡死问题。
在一些网页应用中,有时会碰到一个超级巨大的列表,成千上万行,这时大部份浏览器解析起来就非常痛苦了(有可能直接卡死). 也许你们会说可以分页或动态加载啊?但是有可能需求不允许分页,动态加载?网络的延迟也 ...
- Oracle Flashback Database
Oracle Flashback Database Ensure that the prerequisites described in Prerequisites of Flashback Data ...
- WPF Good UI 2
自定义一个漂亮的window窗口UI <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation& ...
- Android studio 不能创建Activity等文件
这是我之前安装Android studio的一系列问题:http://tieba.baidu.com/p/5921373177 1. 不能创建Activity等许多文件: 2. 工程运行不了: 3. ...
- 阶段1 语言基础+高级_1-3-Java语言高级_03-常用API第二部分_第6节 基本类型包装类_1_包装类的概念
只有这两个比较特殊
- Java各类型占字节数
byte 1字节short 2字节int 4字节long 8字节float 4字节double 8字节char 2字节boolean 1字节 其中,换算关系: 1GB=1024MB 1MB=1024K ...
- Python笔记(三十)_python标准异常总结
python标准异常总结 AssertionError 断言语句(assert)失败 AttributeError 尝试访问未知的对象属性 EOFError 用户输入文件末尾标志EOF(Ctrl+d) ...
- python列表-定义
一.定义: 1.“列表”是一个值,它包含多个字构成的序列. 2.术语“列表值”指的是列表本身(它作为一个值,可以保存在变量中,或传递给函数,像所有其他值一样),而不是指列表值之内的那些值.列表值看起来 ...
- HTML: 引号不能忽视
在js中常常生成拼接html,然后放到dom中,但是有些拼接的html标签需要加一些指或者属性,这个时候不能忽略引号 如果data.link_tel有空格,不加单引号导致value的值不完全 str ...