コード例 #1
0
        public object Load(string contents, Type type)
        {
            var gtype = type.GenericTypeArguments.FirstOrDefault();

            if (_concurrent.Keys.Contains(gtype.Name))
            {
                object obj = null;
                _concurrent.TryGetValue(gtype.Name, out obj);
                return(obj);
            }
            return(baseStore.Load(contents, type));
        }
コード例 #2
0
        private void Init()
        {
            if (this.store == null)
            {
                this.store = new DefaultStoreStrategy();
            }

            //Assembly asm= Assembly.GetExecutingAssembly();
            var props = this.GetType().GetProperties();

            foreach (var prop in props)
            {
                var propType = prop.PropertyType;
                if (propType.GenericTypeArguments.Length == 0)
                {
                    continue;
                }
                var attribute = prop.GetCustomAttribute <FileSetUsePrefixPathAttribute>(true);
                var gen_type  = propType.GenericTypeArguments[0];
                var ret_type  = gen_type.GetType();
                if (ret_type.IsInstanceOfType(typeof(IFileSet)))
                {
                    bool isInit = false;
                    //load
                    var    prefix   = attribute == null ? null : this.PrefixPath;
                    string filename = store.GetFileName(gen_type, prefix);

                    if (File.Exists(filename))
                    {
                        //string contents = File.ReadAllText(filename);
                        string contents = store.PreLoad(gen_type, prefix);
                        //object o = XmlHelper.DeserializeObject(contents, prop.PropertyType);
                        object o = store.Load(contents, prop.PropertyType);
                        if (o != null)
                        {
                            prop.SetValue(this, o);
                            isInit = true;
                        }
                        //XmlHelper.SerializeObject(o, prop.PropertyType);
                    }
                    else if (string.IsNullOrEmpty(filename))
                    {
                        object o = store.Load(string.Empty, prop.PropertyType);
                        if (o != null)
                        {
                            var list = o as IEnumerable;
                            if (list != null)
                            {
                                var gtype    = prop.PropertyType.GenericTypeArguments.FirstOrDefault();
                                var instance = Activator.CreateInstance(prop.PropertyType);
                                foreach (var item in list)
                                {
                                    var method = item
                                                 .GetType()
                                                 .GetMethod("MakeDirty");

                                    if (method != null)
                                    {
                                        method.Invoke(item, new object[] { false });
                                    }

                                    prop.PropertyType
                                    .GetMethod("Add")
                                    .Invoke(instance, new object[] { item });
                                }
                                prop.SetValue(this, instance);
                                isInit = true;
                            }
                            o = null;
                        }
                        if (o != null)
                        {
                            prop.SetValue(this, o);
                            isInit = true;
                        }
                    }

                    if (!isInit)
                    {
                        //if empty = initial
                        var instance = Activator.CreateInstance(prop.PropertyType);
                        prop.SetValue(this, instance);
                    }
                }
                //Name: prop.Name
            }
        }