Пример #1
0
        /// <summary>通过指定类型和成员名称,创建快速访问成员。按照属性、字段、构造、方法、事件的顺序</summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MemberInfoX Create(Type type, String name)
        {
            if (type == null || String.IsNullOrEmpty(name))
            {
                return(null);
            }

            var mis = type.GetMember(name, DefaultBinding | BindingFlags.IgnoreCase);

            if (mis == null || mis.Length < 1)
            {
                //return null;
                // 基类的字段是无法通过这种方法得到的
                return(FieldInfoX.Create(type, name));
            }
            if (mis.Length == 1)
            {
                return(Create(mis[0]));
            }

            var ts = new MemberTypes[] { MemberTypes.Property, MemberTypes.Field, MemberTypes.Constructor, MemberTypes.Method, MemberTypes.Event };

            foreach (var item in ts)
            {
                foreach (var mi in mis)
                {
                    if (mi.MemberType == item)
                    {
                        return(Create(mi));
                    }
                }
            }

            return(Create(mis[0]));
        }
Пример #2
0
        /// <summary>创建快速访问成员</summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static MemberInfoX Create(MemberInfo member)
        {
            if (member == null)
            {
                return(null);
            }

            switch (member.MemberType)
            {
            case MemberTypes.All:
                break;

            case MemberTypes.Constructor:
                return(ConstructorInfoX.Create(member as ConstructorInfo));

            case MemberTypes.Custom:
                break;

            case MemberTypes.Event:
                return(EventInfoX.Create(member as EventInfo));

            case MemberTypes.Field:
                return(FieldInfoX.Create(member as FieldInfo));

            case MemberTypes.Method:
                return(MethodInfoX.Create(member as MethodInfo));

            case MemberTypes.Property:
                return(PropertyInfoX.Create(member as PropertyInfo));

            case MemberTypes.TypeInfo:
            case MemberTypes.NestedType:
                return(TypeX.Create(member as Type));

            default:
                break;
            }
            return(null);
        }
Пример #3
0
        internal static void SetValue(Type type, Object target, String name, Object value)
        {
            if (type == null && target != null)
            {
                type = target.GetType();
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            FieldInfoX fix = Create(type, name);

            if (fix == null)
            {
                throw new Exception(string.Format("类{0}中无法找到{1}字段!", type.Name, name));
            }

            fix.SetValue(target, value);
        }