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;
            }
        }
    }
  • 相关阅读:
    xss框架(一)之浏览器通信
    Joomla未授权创建特权用户漏洞和getshell脚本解析
    从零开始写网站登录爆破(一)
    CSRF学习整理
    vue中vue2-google-maps使用谷歌地图的基础操作
    vue中百度地图API的调用
    60秒定时减少
    git操作指令,以及常规git代码操作
    taro taroUi的H5打包后路径/修改为./
    vue enter事件无效,加入native
  • 原文地址:https://www.cnblogs.com/wolbo/p/11661253.html
Copyright © 2011-2022 走看看