示例#1
0
        public void UpdateFields(FieldInfo[] fields)
        {
            Fields = fields;
            var _fields = fields.Select(x => new DynamicProperty(x.NAME, x.Type)).ToArray();

            TypeModel = DynamicExpression.CreateClass(_fields);
        }
示例#2
0
        public TabPageCustom(dbField[] fields)
            : this()
        {
            Fields = fields;
            var _fields = fields.Select(x => new DynamicProperty(x.Name, x.Type)).ToArray();

            TypeModel = DynamicExpression.CreateClass(_fields);
        }
示例#3
0
        public PanelCustom(FieldInfo[] fields)
            : this()
        {
            Fields = fields;
            var _fields = fields.Select(x => new DynamicProperty(x.NAME, x.Type)).ToArray();

            TypeModel = DynamicExpression.CreateClass(_fields);
        }
示例#4
0
        public void CreateClass_TheadSafe()
        {
            const int numOfTasks = 15;

            var properties = new[] { new DynamicProperty("prop1", typeof(string)) };

            var tasks = new List <Task>(numOfTasks);

            for (var i = 0; i < numOfTasks; i++)
            {
                tasks.Add(Task.Factory.StartNew(() => DynamicExpression.CreateClass(properties)));
            }

            Task.WaitAll(tasks.ToArray());
        }
        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);
        }
示例#6
0
        private object DeserializeDictionary(Dictionary <string, object> dic)
        {
            List <DynamicProperty> properties = new List <DynamicProperty>();

            foreach (KeyValuePair <string, object> kvp in dic)
            {
                double d = 0d;
                Guid   g = Guid.Empty;
                if (kvp.Value is object[] ||
                    kvp.Value is Dictionary <string, object> )
                {
                    properties.Add(new DynamicProperty(kvp.Key, typeof(object)));
                }
                else if (double.TryParse(kvp.Value.ToString(), out d))
                {
                    properties.Add(new DynamicProperty(kvp.Key, typeof(double)));
                }
                else if (Guid.TryParse(kvp.Value.ToString(), out g))
                {
                    properties.Add(new DynamicProperty(kvp.Key, typeof(Guid)));
                }
                else
                {
                    properties.Add(new DynamicProperty(kvp.Key, typeof(string)));
                }
            }

            Type   type = DynamicExpression.CreateClass(properties.ToArray());
            object obj  = type.GetConstructor(new Type[] { }).Invoke(new object[] { });

            foreach (KeyValuePair <string, object> kvp in dic)
            {
                var v = kvp.Value;

                if (kvp.Value is object[])
                {
                    v = DeserializeArray(kvp.Value as object[]);
                }

                if (kvp.Value is Dictionary <string, object> )
                {
                    v = DeserializeDictionary(kvp.Value as Dictionary <string, object>);
                }

                double d = 0d;
                Guid   g = Guid.Empty;

                if (double.TryParse(kvp.Value.ToString(), out d))
                {
                    v = d;
                }

                if (Guid.TryParse(kvp.Value.ToString(), out g))
                {
                    v = g;
                }

                type.GetProperty(kvp.Key)
                .GetSetMethod()
                .Invoke(obj, new object[] { v });
            }

            return(obj);
        }