zoukankan      html  css  js  c++  java
  • 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, 0, 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 > 0;
                }
                return false;
            }
            public static bool IsPositiveIntegerOrZero(this string str)
            {
                int num;
                if (Int32.TryParse(str, out num))
                {
                    return num >= 0;
                }
                return false;
            }
            public static bool IsNegaitiveInteger(this string str)
            {
                int num;
                if (Int32.TryParse(str, out num))
                {
                    return num < 0;
                }
                return false;
            }
            public static bool IsZero(this string str)
            {
                int num;
                if (Int32.TryParse(str, out num))
                {
                    return num == 0;
                }
                return false;
            }
        }
    }
  • 相关阅读:
    关于LEA指令(单周期就可以做简单的算术计算)
    说说字符集和编码(非常形象,非常有意思)
    类依赖项的不透明性和透明性
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码]
    大端和小端
    Google C++测试框架系列:入门
    随机森林中的数学基础之大数定律
    K-means Algorithm
    JavaScript插件——按钮
    分布式系统设计(8)
  • 原文地址:https://www.cnblogs.com/wolbo/p/11661253.html
Copyright © 2011-2022 走看看