Esempio n. 1
0
        public T GetValue <T>(ref object obj)
        {
            T t = default(T);

            if (Member is FieldInfo)
            {
                var field = Member as FieldInfo;
                var tv    = field.GetValue(obj);
                if (typeof(T) == field.FieldType)
                {
                    t = (T)tv;
                }
                else
                {
                    t = (T)OeipAttributeHelper.ChangeType(tv, typeof(T));
                }
            }
            else if (Member is PropertyInfo)
            {
                var property = Member as PropertyInfo;
                var tv       = property.GetValue(obj);
                if (typeof(T) == property.PropertyType)
                {
                    t = (T)tv;
                }
                else
                {
                    t = (T)OeipAttributeHelper.ChangeType(tv, typeof(T));
                }
            }
            return(t);
        }
Esempio n. 2
0
        public void SetValue <T, U>(ref T obj, U member)
        {
            //结构装箱,不然SetValue自动装箱的值拿不到
            object o = obj;

            if (Member is FieldInfo)
            {
                var field = Member as FieldInfo;
                if (typeof(U) != field.FieldType)
                {
                    object ov = OeipAttributeHelper.ChangeType(member, field.FieldType);
                    field.SetValue(o, ov);
                }
                else
                {
                    field.SetValue(o, member);
                }
            }
            else if (Member is PropertyInfo)
            {
                var property = Member as PropertyInfo;
                if (typeof(U) != property.PropertyType)
                {
                    object ov = OeipAttributeHelper.ChangeType(member, property.PropertyType);
                    property.SetValue(o, ov);
                }
                else
                {
                    property.SetValue(o, member);
                }
            }
            obj = (T)o;
        }