示例#1
0
        /// <summary>
        /// 静态快速赋值
        /// </summary>
        /// <param name="target"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetValue(Object target, String name, Object value)
        {
            if (target == null || String.IsNullOrEmpty(name))
            {
                return;
            }

            PropertyInfoX pix = Create(target.GetType(), name);

            if (pix == null)
            {
                return;
            }

            pix.SetValue(target, value);
        }
示例#2
0
        /// <summary>
        /// 尝试设置目标对象指定属性字段的值,返回是否成功
        /// </summary>
        /// <param name="target">目标对象</param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Boolean TrySetValue(Object target, String name, Object value)
        {
            //尝试匹配属性
            PropertyInfoX property = PropertyInfoX.Create(target.GetType(), name);

            if (property != null)
            {
                property.SetValue(target, value);
                return(true);
            }

            //尝试匹配字段
            FieldInfoX field = FieldInfoX.Create(target.GetType(), name);

            if (field != null)
            {
                field.SetValue(target, value);
                return(true);
            }

            return(false);
        }
示例#3
0
文件: FastTest.cs 项目: chenmsg/sujin
        /// <summary>
        ///
        /// </summary>
        public static void Test()
        {
            //XTrace.WriteLog("创建类型……");

            #region TypeX类型
            TypeX  type = TypeX.Create(typeof(FastTest));
            Object obj  = type.CreateInstance();
            Debug.Assert(obj != null, "创建实例出错!");

            obj = type.CreateInstance(123);
            Debug.Assert(obj != null, "创建实例出错!");

            //obj = type.CreateInstance("1234");
            //Debug.Assert(obj != null, "创建实例出错!");

            obj = type.CreateInstance(111, "aaa");
            Debug.Assert(obj != null, "创建实例出错!");

            //XTrace.WriteLog("创建值类型实例");
            type = TypeX.Create(typeof(ConsoleKeyInfo));
            obj  = type.CreateInstance();
            Debug.Assert(obj != null, "创建值类型实例出错!");

            //XTrace.WriteLog("创建数组类型实例");
            type = TypeX.Create(typeof(ConsoleKeyInfo[]));
            obj  = type.CreateInstance(5);
            Debug.Assert(obj != null, "创建数组类型实例出错!");
            #endregion

            #region 构造函数
            ConstructorInfoX ctr = ConstructorInfoX.Create(typeof(FastTest));
            obj = ctr.CreateInstance();
            Debug.Assert(obj != null, "创建实例出错!");

            ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32) });
            obj = ctr.CreateInstance(123);
            Debug.Assert(obj != null, "创建实例出错!");
            ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32), typeof(String) });
            obj = ctr.CreateInstance(111, "aaa");
            Debug.Assert(obj != null, "创建实例出错!");
            #endregion

            #region 字段
            FieldInfoX field = FieldInfoX.Create(typeof(FastTest), "_ID");
            (obj as FastTest).ID = 111;
            Int32 v = (Int32)field.GetValue(obj);
            Debug.Assert(v == 111, "字段取值出错!");
            field.SetValue(obj, 888);
            v = (Int32)field.GetValue(obj);
            Debug.Assert(v == 888, "字段赋值出错!");

            KeyValuePair <Int32, Int32> kv = new KeyValuePair <int, int>(123456, 222);
            field = FieldInfoX.Create(kv.GetType(), "Key");
            //field.SetValue(kv, 123456);
            v = (Int32)field.GetValue(kv);
            Debug.Assert(v == 123456, "字段取值出错!");

            field = FieldInfoX.Create(typeof(FastTest), "_Name");
            field.SetValue("动态赋值");
            String v2 = (String)field.GetValue();
            Debug.Assert(v2 == "动态赋值", "静态字段出错!");
            #endregion

            #region 属性
            PropertyInfoX p = typeof(FastTest).GetProperty("ID");

            v = (Int32)p.GetValue(obj);
            Debug.Assert(v == 888, "属性取值出错!");
            p.SetValue(obj, 999);
            v = (Int32)p.GetValue(obj);
            Debug.Assert(v == 999, "属性赋值出错!");

            p = PropertyInfoX.Create(typeof(FastTest), "Name");
            field.SetValue("属性动态赋值");
            v2 = (String)field.GetValue();
            Debug.Assert(v2 == "属性动态赋值", "静态字段出错!");
            #endregion

            #region 方法
            MethodInfoX method = MethodInfoX.Create(typeof(FastTest), "Test2");
            method.Invoke(obj);

            method = typeof(FastTest).GetMethod("GetFullName");
            Console.WriteLine(method.Invoke(null, 123, "abc"));
            #endregion
        }