Example #1
0
            public KeyValueStart(Type type, Type keyType, Type keySerializerType, bool keyIsNullable, Type valueType, Type valueSerializerType, bool valueIsNullable)
            {
                PlanType = SerializerPlanItemType.KeyValueStart;
                Type     = type;
                var props = type.GetProperties();

                KeyType           = keyType;
                KeySerializerType = keySerializerType;
                KeyIsNullable     = keyIsNullable;
                Key      = props.First(p => p.Name == "Key").GetFastPropertyInfo();
                KeyDType = GetDataType(KeyType);
                var takey = GetTypeNameTuple(KeyType);

                KeyTypeNamespace = takey.Item1;
                KeyTypeName      = takey.Item2;
                KeyTypeAssembly  = takey.Item3;

                ValueType           = valueType;
                ValueSerializerType = valueSerializerType;
                ValueIsNullable     = valueIsNullable;
                Value      = props.First(p => p.Name == "Value").GetFastPropertyInfo();
                ValueDType = GetDataType(ValueType);
                var tavalue = GetTypeNameTuple(ValueType);

                ValueTypeNamespace = tavalue.Item1;
                ValueTypeName      = tavalue.Item2;
                ValueTypeAssembly  = tavalue.Item3;
            }
Example #2
0
        /// <summary>
        /// 获取属性对象
        /// </summary>
        /// <param name="instance">对象实例</param>
        /// <param name="propertyString">属性字符串</param>
        public static object GetObjectProperty(object instance, string propertyString)
        {
            if (propertyString.Length <= 0)
            {
                return(null);
            }
            string[] propertys  = propertyString.Split('.');
            object   tempObject = instance;

            for (int i = 0; i < propertys.Length; i++)
            {
                string           propName     = propertys[i];
                Type             objectType   = tempObject.GetType();
                FastPropertyInfo fastProperty = null;
                if (i == propertys.Length - 1)
                {
                    fastProperty = GetFastPropertyInfo(objectType, propName);
                    if (null != fastProperty && fastProperty.Property.CanRead)
                    {
                        return(fastProperty.Get(tempObject));
                    }
                }
                else
                {
                    fastProperty = GetFastPropertyInfo(objectType, propName);
                    if (null != fastProperty)
                    {
                        tempObject = fastProperty.Get(tempObject);
                    }
                }
            }
            return(null);
        }
Example #3
0
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="instance">对象实例</param>
        /// <param name="propertyString">属性字符串</param>
        /// <param name="propertyValue">属性值</param>
        public static void SetObjectProperty(object instance, string propertyString, object propertyValue)
        {
            if (propertyString.Length <= 0)
            {
                return;
            }

            string[] propertys  = propertyString.Split('.');
            object   tempObject = instance;

            for (int i = 0; i < propertys.Length; i++)
            {
                string           propName     = propertys[i];
                Type             objectType   = tempObject.GetType();
                FastPropertyInfo fastProperty = null;
                if (i == propertys.Length - 1)
                {
                    fastProperty = GetFastPropertyInfo(objectType, propName);
                    if (null != fastProperty && fastProperty.Property.CanWrite)
                    {
                        fastProperty.Set(tempObject, ObjectHelper.GetObjectFromString(propertyValue.ToString(), fastProperty.Property.PropertyType));
                    }
                }
                else
                {
                    fastProperty = GetFastPropertyInfo(objectType, propName);
                    if (null != fastProperty)
                    {
                        tempObject = fastProperty.Get(tempObject);
                    }
                }
            }
        }
Example #4
0
 internal DataPerPattern(string name, FastPropertyInfo property, object defaultValue, bool isGuid, bool isEnum)
 {
     Name         = name;
     Property     = property;
     DefaultValue = defaultValue;
     IsGuid       = isGuid;
     IsEnum       = isEnum;
 }
Example #5
0
 public PropertyReference(PropertyInfo pInfo)
 {
     PlanType     = SerializerPlanItemType.PropertyReference;
     Type         = pInfo.PropertyType;
     DefaultValue = Type.IsValueType ? Activator.CreateInstance(Type) : null;
     Property     = pInfo.GetFastPropertyInfo();
     Name         = pInfo.Name;
 }
Example #6
0
 public PropertyValuePlanItem(PropertyInfo pInfo, bool isNullable)
 {
     PlanType   = PlanItemType.PropertyValue;
     Type       = pInfo.PropertyType;
     IsNullable = isNullable;
     Property   = pInfo.GetFastPropertyInfo();
     Name       = pInfo.Name;
 }
Example #7
0
 public PropertyValue(PropertyInfo pInfo, Type serializer, bool isNullable)
 {
     PlanType       = SerializerPlanItemType.PropertyValue;
     Type           = pInfo.PropertyType;
     DefaultValue   = isNullable ? null : Type.IsValueType ? Activator.CreateInstance(Type) : null;
     IsNullable     = isNullable;
     Property       = pInfo.GetFastPropertyInfo();
     Name           = pInfo.Name;
     SerializerType = serializer;
 }
Example #8
0
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="propertyName">属性名称</param>
        /// <param name="propertyValue">属性值</param>
        public static void SetPropertyValue(object entity, string propertyName, object propertyValue)
        {
            FastPropertyInfo fastProerty = GetFastProperty(entity.GetType(), propertyName);

            if (fastProerty != null && fastProerty.Property != null)
            {
                fastProerty.Set(entity,
                                ObjectHelper.ConvertObjectValue(propertyValue, fastProerty.Property.PropertyType));
            }
        }
Example #9
0
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="propertyName">属性名称</param>
        public static object GetPropertyValue(object entity, string propertyName)
        {
            FastPropertyInfo fastProerty = GetFastProperty(entity.GetType(), propertyName);

            if (fastProerty != null && fastProerty.Property != null)
            {
                return(fastProerty.Get(entity));
            }
            return(null);
        }
        public void CanSetPropertyValueWithFastInfo()
        {
            FastPropertyInfo propInfo = new FastPropertyInfo(typeof(Foo).GetProperty("B"));

            Foo a = new Foo();
            Bar b = new Bar();

            propInfo.SetValue(a, b, null);

            Assert.AreSame(b, a.B);
        }
Example #11
0
        /// <summary>
        /// 获取属性包装对象
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <param name="propertyName">属性名称</param>
        public static FastPropertyInfo GetFastProperty(Type type, string propertyName)
        {
            //string key = type.FullName + propertyName;
            //FastPropertyInfo result = null;
            //if (!PropertyCache.TryGetValue(key, out result))
            //{
            //    result = new FastPropertyInfo(type.GetProperty(propertyName));
            //    PropertyCache.Add(key, result);
            //}
            FastPropertyInfo result = new FastPropertyInfo(type.GetProperty(propertyName));

            return(result);
        }
        public void CanGetPropertyValueFromFastInfo()
        {
            FastPropertyInfo propInfo = new FastPropertyInfo(typeof(Foo).GetProperty("B"));

            Foo a = new Foo();
            Bar b = new Bar();

            a.B = b;

            object b2 = propInfo.GetValue(a, null);

            Assert.AreSame(b, b2);
        }
Example #13
0
        /// <summary>
        /// 设置操作人信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="isUpdate">是否是更新</param>
        public void SetAuditable(object entity, bool isUpdate)
        {
            if (!IsEnableAuditable)
            {
                return;
            }
            FastPropertyInfo fastPro = null;
            var userId     = ProjectUser.UserID;
            var userName   = ProjectUser.UserName;
            var dateTime   = DateTime.Now;
            var entityType = entity.GetType();

            if (!isUpdate)
            {
                fastPro = GetFastProperty(entityType, CreateUserIdFieldName);
                if (fastPro != null && fastPro.Property != null)
                {
                    fastPro.Property.SetValue(entity, userId, null);
                }
                fastPro = GetFastProperty(entityType, CreateUserNameFieldName);
                if (fastPro != null && fastPro.Property != null)
                {
                    fastPro.Property.SetValue(entity, userName, null);
                }
                fastPro = GetFastProperty(entityType, CreateDateTimeFieldName);
                if (fastPro != null && fastPro.Property != null)
                {
                    fastPro.Property.SetValue(entity, dateTime, null);
                }
            }
            else
            {
                fastPro = GetFastProperty(entityType, UpdateUserIdFieldName);
                if (fastPro != null && fastPro.Property != null)
                {
                    fastPro.Property.SetValue(entity, userId, null);
                }
                fastPro = GetFastProperty(entityType, UpdateUserNameFieldName);
                if (fastPro != null && fastPro.Property != null)
                {
                    fastPro.Property.SetValue(entity, userName, null);
                }
                fastPro = GetFastProperty(entityType, UpdateDateTimeFieldName);
                if (fastPro != null && fastPro.Property != null)
                {
                    fastPro.Property.SetValue(entity, dateTime, null);
                }
            }
        }
Example #14
0
        /// <summary>
        /// 获取对象快速属性
        /// </summary>
        /// <param name="objectType">对象类型</param>
        /// <param name="propName">属性名称</param>
        public static FastPropertyInfo GetFastPropertyInfo(Type objectType, string propName)
        {
            FastPropertyInfo fastProperty = null;
            var key = string.Concat(propName, objectType.FullName);

            if (ObjectPropertyCache.ContainsKey(key))
            {
                fastProperty = ObjectPropertyCache[key];
            }
            else
            {
                PropertyInfo property = null;
                try
                {
                    property = objectType.GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.DeclaredOnly);
                }
                catch (AmbiguousMatchException)
                {
                }

                if (property == null)
                {
                    try
                    {
                        property = objectType.GetProperty(propName);
                    }
                    catch (AmbiguousMatchException)
                    {
                    }
                }
                if (property == null)
                {
                    return(null);
                }

                fastProperty = new FastPropertyInfo(property);
                if (!ObjectPropertyCache.ContainsKey(key))
                {
                    ObjectPropertyCache.Add(key, fastProperty);
                }
            }
            return(fastProperty);
        }
Example #15
0
 public static FastPropertyInfo GetFastPropertyInfo(this PropertyInfo prop)
 => FastPropertyInfo.Get(prop);