Пример #1
0
        public DataFile(dbModel model)
            : this()
        {
            Model = model;

            m_mapPort  = MemoryMappedFile.Create(MapProtection.PageReadWrite, 4, Model.Name);
            m_FileName = Model.Name + ".df";
            string m_PathData = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Data");

            if (!Directory.Exists(m_PathData))
            {
                Directory.CreateDirectory(m_PathData);
            }
            m_FilePath = Path.Combine(m_PathData, m_FileName);

            if (OpenOrCreateFile())
            {
                http_Init();
                Opened = true;

                if (m_listItems == null)
                {
                    m_listItems = (IList)typeof(List <>).MakeGenericType(TypeDynamic).GetConstructor(Type.EmptyTypes).Invoke(null);
                }
            }
            else
            {
                Close();
            }
        }
Пример #2
0
        public bool CreateDb(dbModel model)
        {
            DataFile df = new DataFile(model);

            if (df.Opened)
            {
                string name = df.Model.Name;
                if (m_dataFile.TryAdd(name, df))
                {
                    m_listDataName.Add(name);
                }
                return(true);
            }
            return(false);
        }
Пример #3
0
        private Type buildTypeDynamic(dbModel m)
        {
            if (m == null || string.IsNullOrEmpty(m.Name) || m.Fields == null || m.Fields.Length == 0)
            {
                return(null);
            }

            var  fields = m.Fields.Select(x => new DynamicProperty(x.Name, x.Type)).OrderBy(x => x.Name).ToArray();
            Type type   = DynamicExpression.CreateClass(fields);
            //DynamicProperty[] at = new DynamicProperty[]
            //{
            //    new DynamicProperty("Name", typeof(string)),
            //    new DynamicProperty("Birthday", typeof(DateTime))
            //};
            //object obj = Activator.CreateInstance(type);
            //t.GetProperty("Name").SetValue(obj, "Albert", null);
            //t.GetProperty("Birthday").SetValue(obj, new DateTime(1879, 3, 14), null);

            var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
            // Obtain all serializable types having no explicit proto contract
            var serializableTypes = Assembly.GetExecutingAssembly()
                                    .GetTypes()
                                    .Where(t => t.IsSerializable && !Attribute.IsDefined(t, typeof(ProtoContractAttribute)));

            var metaType = model.Add(type, false);

            metaType.AsReferenceDefault = true;
            metaType.UseConstructor     = false;

            // Add contract for all the serializable fields
            var serializableFields = type
                                     .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                     .Where(fi => !Attribute.IsDefined(fi, typeof(NonSerializedAttribute)))
                                     .OrderBy(fi => fi.Name) // it's important to keep the same fields order in all the AppDomains
                                     .Select((fi, k) => new { info = fi, index = k });

            foreach (var field in serializableFields)
            {
                var metaField = metaType.AddField(field.index + 1, field.info.Name);
                metaField.AsReference = !field.info.FieldType.IsValueType;       // cyclic references support
                metaField.DynamicType = field.info.FieldType == typeof(object);  // any type support
            }
            // Compile model in place for better performance, .Compile() can be used if all types are known beforehand
            model.CompileInPlace();

            return(type);
        }