Exemple #1
0
        public void Dispose()
        {
            if (this.propertyValues != null)
            {
                this.propertyValues.Clear();
            }
            this.propertyValues = null;
            foreach (var childrenType in childrenTypes)
            {
                var exPropertyInfoList = ExtendPropertysProvider.GetByType(childrenType.Value);
                foreach (var extendProperty in exPropertyInfoList)
                {
                    extendProperty.ValueChanged -= new EventHandler <ExtendPropertyValueChangedArgs>(exObject_ChildrenPropertyChanged);
                }
            }

            if (objects != null)
            {
                foreach (var o in objects)
                {
                    var obj = o as ExtendObject;
                    obj.ChildrenPropertyChanged -= new EventHandler <ExtendPropertyValueChangedArgs>(exObject_ChildrenPropertyChanged);
                }
            }
        }
Exemple #2
0
        protected ExtendProperty GetProperty(string name)
        {
            int propertyKey = OwnerType.GetHashCode() ^ name.GetHashCode();
            var property    = ExtendPropertysProvider.Get(propertyKey);

            return(property);
        }
Exemple #3
0
 public static ExtendProperty[] GetPropertys(Type ownerType)
 {
     return(ExtendPropertysProvider.GetByType(ownerType));
     //return ownerType.GetFields
     //    (BindingFlags.Static | BindingFlags.Public)
     //    .Where(f => f.FieldType == typeof(ExtendProperty))
     //    .Select(f => (ExtendProperty)f.GetValue(null)).ToArray();
 }
Exemple #4
0
        public static ExtendProperty GetProperty(Type ownerType, string propertyName)
        {
            int propertyKey = ownerType.GetHashCode() ^ propertyName.GetHashCode();
            var property    = ExtendPropertysProvider.Get(propertyKey);

            property.OwnerType = ownerType;
            return(property);
        }
Exemple #5
0
        public static ExtendProperty RegisterProperty(string propertyName, Type propertyType, Type ownerType, object defaultValue, MetaData metaData)
        {
            var property = new ExtendProperty(propertyName, propertyType, ownerType);

            property.OverrideDefaultValue(ownerType, defaultValue);
            property.MetaData = metaData;
            ExtendPropertysProvider.Set(property.GetHashCode(), property);

            return(property);
        }
Exemple #6
0
        public ExtendProperty AddOwner(Type ownerType, object defaultValue)
        {
            int newOwnerHash = ownerType.GetHashCode() ^ this.PropertyName.GetHashCode();

            if (defaultValue != null)
            {
                this.OverrideDefaultValue(ownerType, defaultValue);
            }
            ExtendPropertysProvider.Set(newOwnerHash, this);
            return(this);
        }
Exemple #7
0
        /// <summary>
        /// 对象初始化函数,对象被创建时,这个函数将被调用
        /// </summary>
        /// <param name="item">扩展对象</param>
        /// <param name="args">参数</param>
        public virtual void OnDoCreate(ExtendObject item, params object[] args)
        {
            OwnerType = this.GetType();
            var plist = ExtendPropertysProvider.GetByType(OwnerType).ToList();

            if (plist.Count <= 0)
            {
                plist = ExtendPropertysProvider.GetByType(OwnerType.BaseType).ToList();
            }

            var types = plist.Where(p => childrenTypes.Values.Any(cp => cp == p.PropertyType)).ToDictionary(m => m.PropertyName, e => e.PropertyType);

            foreach (var children in types)
            {
                var info = OwnerType.GetProperty(children.Key);
                if (info != null)
                {
                    var childObj = info.GetValue(this, null) as ExtendObject;
                    if (childObj != null)
                    {
                        objects.Add(childObj);
                        childObj.ChildrenPropertyChanged -=
                            new EventHandler <ExtendPropertyValueChangedArgs>(exObject_ChildrenPropertyChanged);
                        childObj.ChildrenPropertyChanged +=
                            new EventHandler <ExtendPropertyValueChangedArgs>(exObject_ChildrenPropertyChanged);
                    }
                }
                var childObjPropertyInfoList = ExtendPropertysProvider.GetByType(children.Value);
                foreach (var extendProperty in childObjPropertyInfoList)
                {
                    extendProperty.ValueChanged -=
                        new EventHandler <ExtendPropertyValueChangedArgs>(exObject_ChildrenPropertyChanged);

                    extendProperty.ValueChanged +=
                        new EventHandler <ExtendPropertyValueChangedArgs>(exObject_ChildrenPropertyChanged);
                }
            }
        }
Exemple #8
0
        protected virtual void Restore()
        {
            // 如果没有备份数据,则忽略此调用。
            if (____backup != null && ____backup.Count > 0)
            {
                //反序列化得到字典。
                HybridDictionary state;
                using (MemoryStream buffer = new MemoryStream(____backup.Pop()))
                {
                    buffer.Position = 0;
                    BinaryFormatter formatter = new BinaryFormatter();
                    try
                    {
                        state = (HybridDictionary)formatter.Deserialize(buffer);
                    }
                    catch
                    {
                        return;
                    }
                }

                object           source     = this;
                Type             sourceType = source.GetType();
                ExtendProperty[] propertys;

                // 获取所有字段信息。
                propertys = ExtendPropertysProvider.GetByType(this.GetType());


                foreach (ExtendProperty property in propertys)
                {
                    //获取新值。
                    object value = this.GetValue(property);

                    if (typeof(IBusinessObject).IsAssignableFrom(property.PropertyType))
                    {
                        //这是个子对象,检查只是否为空。
                        if (state.Contains(property.GetHashCode()))
                        {
                            //原来为空,设置为空。
                            this.SetValue(property, null);
                        }
                        else
                        {
                            if (value != null)
                            {
                                // 子对象调用Restore方法。
                                ((IUndoObject)value).Undo();
                            }
                        }
                    }
                    //如果字典包含该字段,则还原。
                    else if (state.Contains(property.GetHashCode()))
                    {
                        this.SetValue(property, state[property.GetHashCode()]);
                    }

                    //检查值是否为集合,如果是,就对每项调用Restore方法。
                    var oldValue = state[property.GetHashCode()];
                    if (oldValue != null && oldValue is ICollection)
                    {
                        var col = (ICollection)oldValue;
                        foreach (var item in col)
                        {
                            if (item is IUndoObject)
                            {
                                ((IUndoObject)item).Undo();
                            }
                        }
                    }
                }
            }
        }
Exemple #9
0
        protected virtual void Backup()
        {
            if (____backup == null)
            {
                ____backup = new Stack <byte[]>();
            }
            Type             sourceType = this.GetType();
            HybridDictionary state      = new HybridDictionary();

            ExtendProperty[] propertys;

            // 获取所有字段信息。
            propertys = ExtendPropertysProvider.GetByType(this.GetType());

            foreach (ExtendProperty property in propertys)
            {
                object value = this.GetValue(property);
                if (typeof(IBusinessObject).IsAssignableFrom(property.PropertyType))
                {
                    if (value == null)
                    {
                        //值为空,我们也要保存。
                        state.Add(property.GetHashCode(), null);
                    }
                    else
                    {
                        // 不为空,而且是同一类型,则同时调用Backup方法。
                        ((IUndoObject)value).Accept();
                    }
                }
                //检查字段类型是否为ICollection,以便为集合中的项进行备份。
                else if (typeof(ICollection).IsAssignableFrom(property.PropertyType) && value != null && ((ICollection)value).Count > 0)
                {
                    var col = (ICollection)value;
                    foreach (var item in col)
                    {
                        if (item is IUndoObject)
                        {
                            ((IUndoObject)item).Accept();
                        }
                    }

                    state.Add(property.GetHashCode(), value);
                }
                else
                {
                    //普通字段,加入字典。
                    state.Add(property.GetHashCode(), value);
                }
            }


            //序列化,推上栈。
            using (MemoryStream buffer = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();

                formatter.Serialize(buffer, state);
                ____backup.Push(buffer.ToArray());
            }
        }