• 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;
            }
        }
    }
  • 相关阅读:
    Sharepoint List Attachments in a Webpart : The Solution
    MOSS 文档库操作类
    许多实用的js 正则表达式
    SharePoint 调查列表的自定义错误页面
    排序算法——插入排序
    用vs2010创建带有AssociationForm的工作流
    does not have element "configuration/configSections/sectionGroup[@name='SharePoint']" or it is invalid
    CSharp数据库使用SqlCommand进行增,删,改,查询操作
    去掉视图选择栏
    IList与Xml互相转换
  • 原文地址:https://www.cnblogs.com/wolbo/p/11661253.html
Copyright © 2020-2023  润新知