Пример #1
0
 /// <summary> 加载Setter
 /// </summary>
 public void LoadSetter()
 {
     if (Setter != PreSetter)
     {
         return;
     }
     lock (this)
     {
         if (Setter == PreSetter) //Setter未编译
         {
             if (!CanWrite)       //当前成员可读
             {
                 Setter = ErrorSetter;
             }
             else if (Field)
             {
                 Setter = ZTReflector.CreateSetter((FieldInfo)_Member);
             }
             else
             {
                 Setter = ZTReflector.CreateSetter((PropertyInfo)_Member);
             }
         }
     }
 }
Пример #2
0
        /// <summary> 加载Getter
        /// </summary>
        public void LoadGetter()
        {
            if (Getter != PreGetter)
            {
                return;
            }

            lock (this)
            {
                if (Getter == PreGetter) //Getter未编译
                {
                    if (!CanRead)        //当前对象不可读
                    {
                        Getter = ErrorGetter;
                    }
                    else if (Field)
                    {
                        Getter = ZTReflector.CreateGetter((FieldInfo)_Member);
                    }
                    else
                    {
                        Getter = ZTReflector.CreateGetter((PropertyInfo)_Member);
                    }
                }
            }
        }
Пример #3
0
        /// <summary> 获取缓存
        /// </summary>
        /// <param name="type">反射对象类型</param>
        /// <param name="ignoreCase">属性/字段名称是否忽略大小写</param>
        /// <returns></returns>
        public static ZTReflector Cache(Type type, bool ignoreCase)
        {
            ZTReflector lit;
            Dictionary <Type, ZTReflector> item = ignoreCase ? _IgnoreCaseItems : _Items;

            if (item.TryGetValue(type, out lit))
            {
                if (lit._Type != type)
                {
                    throw new Exception("缓存中的对象反射类型与参数type不一致!");
                }
            }
            else
            {
                lock (item)
                {
                    if (item.TryGetValue(type, out lit) == false)
                    {
                        lit = new ZTReflector(type, ignoreCase);
                        item.Add(type, lit);
                    }
                }
            }
            return(lit);
        }
Пример #4
0
        public static GenericCollection GetDict(Type type)
        {
            GenericCollection dict;

            if (Cache.TryGetValue(type, out dict))
            {
                return(dict);
            }
            lock (Cache)
            {
                if (Cache.TryGetValue(type, out dict))
                {
                    return(dict);
                }
                dict      = new GenericCollection();
                dict.Init = ZTReflector.CreateNewObject(type);
                if (type.IsGenericType)
                {
                    var ga = type.GetGenericArguments();
                    if (ga.Length > 1)
                    {
                        dict.KeyType     = type.GetGenericArguments()[0];
                        dict.ElementType = type.GetGenericArguments()[1];
                        dict.ElementType = Nullable.GetUnderlyingType(dict.ElementType) ?? dict.ElementType;
                    }
                    else
                    {
                        dict.ElementType = typeof(object);
                    }
                }
                else
                {
                    dict.ElementType = typeof(object);
                }
                Cache.Add(type, dict);
                return(dict);
            }
        }
Пример #5
0
        public static GenericCollection GetList(Type type)
        {
            GenericCollection list;

            if (Cache.TryGetValue(type, out list))
            {
                return(list);
            }
            if (type.GetInterface("System.Collections.IList") != typeof(IList))
            {
                return(null);
            }
            lock (Cache)
            {
                if (Cache.TryGetValue(type, out list))
                {
                    return(list);
                }
                list      = new GenericCollection();
                list.Init = ZTReflector.CreateNewObject(type);
                if (type.IsGenericType)
                {
                    list.ElementType = type.GetGenericArguments()[0];
                    list.ElementType = Nullable.GetUnderlyingType(list.ElementType) ?? list.ElementType;
                }
                else if (type.IsArray)
                {
                    list.ElementType = type.GetElementType();
                }
                else
                {
                    list.ElementType = typeof(object);
                }
                Cache.Add(type, list);
                return(list);
            }
        }