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 ...
随机推荐
- [CSP-S模拟测试]:string(线段树)
题目描述 给定一个由小写字母组成的字符串$s$. 有$m$次操作,每次操作给定$3$个参数$l,r,x$. 如果$x=1$,将$s[l]~s[r]$升序排序: 如果$x=0$,将$s[l]~s[r]$ ...
- mpvue 未找到app.json入口文件
[情况1] 通过 vue init mpvue/mpvue-quickstart myproject 创建一个mpvue项目,发现项目中没有app.json文件. 解决:npm run dev 编译一 ...
- ASP Loading
ASP 页面加载等待效果.如 显示"请稍后页面正在加载...",加载完成后隐藏这个loading. <%Response.buffer=false%> <html ...
- 协议-TCP:TCP
ylbtech-协议-TCP:TCP 传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793 ...
- 004-spring-data-elasticsearch 3.0.0.0使用【二】-spring-data之定义方法、创建repository实例、从聚合根发布事件
续上文 1.4.定义方法 存储库代理有两种方法可以从方法名称派生特定于存储的查询.它可以直接从方法名称派生查询,或者使用手动定义的查询.可用选项取决于实际store.但是,必须有一个策略来决定创建什么 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_06 Set集合_2_哈希值
没有重写就是十进制的整数,重写了想返回多少就是多少. 创建Person类,没有写继承关系,默认会继承Object类 打开Object这个类 找到HashCode这个方法.就这一行代码.甚至都没有方法体 ...
- TestNG 多线程测试
TestNG以注解的方式实现多线程测试 import org.testng.annotations.Test; public class TreadDemo { // invocationCount ...
- SpringBoot错误经验
1.在application.properties 添加 debug=true,可以看见项目的执行流程有助于调bug 2.如果错误显示端口号被占用 cmd 步骤1 查看端口号应用情况:netstat ...
- 第七周实验报告&课程总结
一.完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 代码: public cla ...
- 关于时间API
如何正确处理时间 现实生活的世界里,时间是不断向前的,如果向前追溯时间的起点,可能是宇宙出生时,又或是宇宙出现之前, 但肯定是我们目前无法找到的,我们不知道现在距离时间原点的精确距离.所以我们要表示时 ...