Esempio n. 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 = Literacy.CreateSetter((FieldInfo)MemberInfo);
             }
             else
             {
                 Setter = Literacy.CreateSetter((PropertyInfo)MemberInfo);
             }
         }
     }
 }
Esempio n. 2
0
        /// <summary> 表示一个可以获取或者设置其内容的对象属性
        /// </summary>
        /// <param name="property">属性信息</param>
        private ObjectProperty(PropertyInfo property)
        {
            Field        = false;
            MemberInfo   = property;               //属性信息
            OriginalType = property.PropertyType;
            var get = property.GetGetMethod(true); //获取属性get方法,不论是否公开
            var set = property.GetSetMethod(true); //获取属性set方法,不论是否公开

            if (set != null && //set方法不为空
                property.GetIndexParameters().Length == 0)
            {
                CanWrite = true;         //属性可写
                Static   = set.IsStatic; //属性是否为静态属性
                IsPublic = set.IsPublic;
            }
            else if (property.DeclaringType.Name.StartsWith("<>f__AnonymousType")) //匿名类
            {
                CanWrite = true;
                IsPublic = false;
            }
            if (get != null)             //get方法不为空
            {
                CanRead  = true;         //属性可读
                Static   = get.IsStatic; //get.set只要有一个静态就是静态
                IsPublic = IsPublic || get.IsPublic;
            }
            ID  = System.Threading.Interlocked.Increment(ref Literacy.Sequence);
            UID = Guid.NewGuid();
            Init();
            if (set == null && CanWrite) //匿名类的属性设置特殊处理
            {
                Setter = (o, v) =>
                {
                    var field = ClassType.GetField("<" + Name + ">i__Field", (BindingFlags)(-1));
                    Setter = Literacy.CreateSetter(field, ClassType);
                    Setter(o, v);
                };
            }
            Attributes = new AttributeCollection(MemberInfo);
            var mapping = Attributes.First <IMemberMappingAttribute>();

            if (mapping != null)
            {
                MappingName = mapping.Name;
            }
        }