public static object ToEntity(this IDictionary <string, object> source, Type type) { if (source.IsNullOrEmpty()) { return(null); } FastType fastType = FastType.Get(type); var instance = Activator.CreateInstance(type); foreach (var p in fastType.Setters) { if (p.Name.IsNullOrEmpty()) { continue; } if (source.Keys.Contains(p.Name)) { p.SetValue(instance, source[p.Name].ConvertToType(p.Type)); } } var temp = instance as IExtProperty; if (temp != null) { var keys = source.Keys.Where(o => o.StartsWith(SysConfig.ExtFieldName, StringComparison.OrdinalIgnoreCase)); if (!keys.IsNullOrEmpty()) { foreach (var key in keys) { temp.Properties[key] = source[key]; } } } return(instance); }
public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer) { var instance = Activator.CreateInstance(type); FastType ft = FastType.Get(type); foreach (var p in ft.Setters) { if (!dictionary.Keys.Contains(p.Name)) { continue; } if (p.Info.GetCustomAttribute <ScriptIgnoreAttribute>(true) != null || p.Type.IsGenericType || typeof(IControl).IsAssignableFrom(p.Type)) { continue; } if (p.Info.GetCustomAttribute <HtmlEncodeAttribute>(true) != null) { p.SetValue(instance, HttpUtility.HtmlDecode(dictionary[p.Name].ToString())); } else { p.SetValue(instance, dictionary[p.Name]); } } return(instance); }
public static T Read <T>(IDictionary <string, object> data, Type instanceType) { if (null != data) { FastType reflection = FastType.Get(instanceType); object instance = Activator.CreateInstance(instanceType); foreach (String key in data.Keys) { FastProperty prop = reflection.Setters.SingleOrDefault(p => MatchColumnName(p.Name, key)); if (null != prop) { prop.SetValue(instance, data[key].ConvertToType(prop.Type)); } } return((T)instance); } else { return(default(T)); } }
/// <summary> /// Generates the C# class that can be used to deserialize all types registered in this context. /// </summary> /// <param name="w">The writer to which the generated C# code will be written to.</param> /// <param name="namespace">The namespace of the generated class.</param> public void GenerateCSharpCode(TextWriter w, string @namespace) { w.WriteLine("// <auto-generated>"); w.WriteLine("// This code was generated by a tool."); w.WriteLine("// But might require manual tweaking."); w.WriteLine("// </auto-generated>"); w.WriteLine(); w.WriteLine("using System.ComponentModel;"); w.WriteLine("using System.Collections;"); w.WriteLine("using System.Collections.Generic;"); w.WriteLine("using Galador.Reflection.Serialization;"); w.WriteLine(); w.Write("namespace "); w.Write(@namespace); w.WriteLine(" {"); foreach (var item in this.Objects.OfType <TypeData>().Where(x => x.Kind == PrimitiveType.Object && x.IsSupported)) { if (item.IsGeneric && !item.IsGenericTypeDefinition) { continue; } if (item.IsGenericParameter) { continue; } var rtype = item.RuntimeType(); if (rtype != null && FastType.IsFromMscorlib(rtype.Type)) { continue; } w.WriteLine(); GenerateCSharpCode(w, item); } w.WriteLine("}"); }
protected static void AssertScalarField(IFieldSet fieldSet, int fieldIndex, FastType type, String name, Operator op) { var field = (Scalar)fieldSet.GetField(fieldIndex); AssertScalarField(field, type, name); Assert.AreEqual(op, field.Operator); }
public ComposedScalar(QName name, FastType fastType, Scalar[] fields, bool optional, IComposedValueConverter valueConverter) : base(name, optional) { _fields = fields; _valueConverter = valueConverter; _fastType = fastType; }
private void GetChildren(IControl e, IList <IControl> controls) { var children = controls.Where(o => o.ParentId.Equals(e.Id)); if (children.IsNullOrEmpty()) { return; } var fastType = FastType.Get(e.GetType()); foreach (var ee in children) { var gettter = fastType.GetGetter(ee.TargetId); if (gettter.Type.IsGenericType) { var instance = gettter.GetValue(e); if (instance == null) { var t = typeof(List <>); var type = gettter.Type.GetGenericArguments()[0]; t = t.MakeGenericType(type); instance = Activator.CreateInstance(t); gettter.SetValue(e, instance); } var add = instance.GetType().GetMethod("Add"); add.Invoke(instance, new object[] { ee }); } else { gettter.SetValue(e, ee); } GetChildren(ee, controls); } }
public ComposedScalar(ComposedScalar other) : base(other) { _fastType = other._fastType; _fields = other._fields.CloneArray(); _valueConverter = other._valueConverter; }
private static void ValidateDecodedValueIsCorrectForType(ScalarValue value, FastType type) { if (value == null) { return; } type.ValidateValue(value); }
internal PropertyValue(PropertyPath path, int index, MemberInfo member) { this.index = index; Path = path; var ot = FastType.GetType(member.DeclaringType); Member = ot.DeclaredMembers[member.Name]; }
protected static void AssertScalarField(IFieldSet fieldSet, int fieldIndex, FastType type, String name, OperatorCodec operatorCodec, ScalarValue defaultValue) { var field = (Scalar)fieldSet.GetField(fieldIndex); AssertScalarField(field, type, name); Assert.AreEqual(operatorCodec, field.OperatorCodec); Assert.AreEqual(defaultValue, field.DefaultValue); }
public Scalar(Scalar other) : base(other) { _defaultValue = (ScalarValue)other._defaultValue.Clone(); _fastType = other._fastType; _initialValue = (ScalarValue)other._initialValue.Clone(); _operator = other._operator; _operatorCodec = other._operatorCodec; _typeCodec = other._typeCodec; _dictionary = other._dictionary; }
private Scalar(QName name, FastType fastType, Operator op, OperatorCodec operatorCodec, ScalarValue defaultValue, bool optional) : base(name, optional) { _operator = op; _operatorCodec = operatorCodec; _dictionary = DictionaryFields.Global; _defaultValue = defaultValue ?? ScalarValue.Undefined; _fastType = fastType; _typeCodec = fastType.GetCodec(op, optional); _initialValue = (defaultValue == null || defaultValue.IsUndefined) ? _fastType.DefaultValue : defaultValue; op.Validate(this); }
protected static void AssertComposedScalarField(ComposedScalar field, FastType type, String name, Operator exponentOp, ScalarValue exponentValue, Operator mantissaOp, ScalarValue mantissaValue) { Assert.AreEqual(type, field.FastType); Assert.AreEqual(name, field.Name); Scalar[] fields = field.Fields; Assert.AreEqual(exponentOp, fields[0].Operator); Assert.AreEqual(exponentValue, fields[0].DefaultValue); Assert.AreEqual(mantissaOp, fields[1].Operator); Assert.AreEqual(mantissaValue, fields[1].DefaultValue); }
public int UpdateFieldsNotNull(object entity) { IList <string> includedProperties = new List <string>(); FastProperty[] properties = FastType.Get(entity.GetType()).Setters; foreach (FastProperty property in properties) { if (null != property.GetValue(entity)) { includedProperties.Add(property.Name); } } return(this.UpdateFields(entity, includedProperties.ToArray())); }
public static Table ReadTable(Type type) { var table = new Table(); table.Name = GetTableName(type); table.Schema = GetSchemaName(type); FastType fastType = FastType.Get(type); foreach (var p in fastType.Setters) { if (p.Info.PropertyType.IsValueType || p.Type == typeof(string)) { var column = new Column(); column.Name = p.Name; column.IsNullable = true; column.IsAutoIncrement = false; GetDefaultDataType(p.Type, column); var attr = p.Info.GetCustomAttribute <ColumnAttribute>(true); if (attr != null) { if (attr.IsDataField == false) { continue; } column.Name = attr.Name.IsNullOrEmpty() ? p.Name : attr.Name; column.Type = attr.DataType.IsNullOrEmpty() ? column.Type : attr.DataType; column.Length = attr.Length.IsNullOrEmpty() ? column.Length : attr.Length; column.Showable = attr.Showable; column.Editable = attr.Editable; column.Searchable = attr.Searchable; column.IsAutoIncrement = attr.IsAutoIncrement; } var attr1 = p.Info.GetCustomAttribute <PrimaryKeyAttribute>(true); if (attr1 != null || p.Name.Equals("Id", StringComparison.OrdinalIgnoreCase)) { column.IsKey = true; column.IsNullable = false; table.Keys.Add(column); } column.Comment = p.Info.GetDisplayName(); if ("text|int|bit|datetime".Split('|').Contains(column.Type, StringComparer.OrdinalIgnoreCase)) { column.Length = string.Empty; } table.Columns.Add(column); } } return(table); }
public override Field Convert(GroupValue fieldDef, ITemplateRegistry templateRegistry, ConversionContext context) { FastType type = _templateTypeMap[fieldDef.Group]; bool optional = fieldDef.GetBool("Optional"); IFieldValue retInitialValue; ScalarValue initialValue = fieldDef.TryGetValue("InitialValue", out retInitialValue) && retInitialValue != null ? (ScalarValue)retInitialValue : ScalarValue.Undefined; string name = fieldDef.GetString("Name"); IFieldValue rettempNs; string tempNs = fieldDef.TryGetValue("Ns", out rettempNs) && rettempNs != null?rettempNs.ToString() : ""; var qname = new QName(name, tempNs); Scalar scalar; IFieldValue retOperator; if (fieldDef.TryGetValue("Operator", out retOperator) && retOperator != null) { GroupValue operatorGroup = ((GroupValue)retOperator).GetGroup(0); Operator operatortemp = GetOperator(operatorGroup.Group); scalar = new Scalar(qname, type, operatortemp, initialValue, optional); IFieldValue retDictionary; if (operatorGroup.TryGetValue("Dictionary", out retDictionary) && retDictionary != null) { scalar.Dictionary = retDictionary.ToString(); } IFieldValue retKey; if (operatorGroup.TryGetValue("Key", out retKey) && retKey != null) { GroupValue retOperatorGroup = (GroupValue)retKey; string keyName = retOperatorGroup.GetString("Name"); string ns = retOperatorGroup.GetString("Ns"); scalar.Key = new QName(keyName, ns); } } else { scalar = new Scalar(qname, type, Operator.None, initialValue, optional); } IFieldValue retAuxId; if (fieldDef.TryGetValue("AuxId", out retAuxId) && retAuxId != null) { scalar.Id = retAuxId.ToString(); } return(scalar); }
public object Resolve(string name) { object value; var fastType = FastType.Get(Sec.User.GetType()); var getter = fastType.GetGetter(name); if (getter != null) { return(getter.GetValue(Sec.User)); } else { return(Sec.User.Properties.TryGetValue(name, out value) ? value : null); } }
public static OperatorCodec GetCodec(Operator op, FastType type) { Tuple <Operator, FastType> key = Tuple.Create(op, type); OperatorCodec codec; if (OperatorMap.TryGetValue(key, out codec)) { return(codec); } Global.ErrorHandler.OnError(null, StaticError.OperatorTypeIncomp, "The operator '{0}' is not compatible with type '{1}'", op, type); throw new ArgumentOutOfRangeException("op" + ",type", key, "Not found"); }
public static void RecSave(IControl instance) { using (TransactionScope tran = new TransactionScope()) { SaveComponent(instance); var pis = FastType.Get(instance.GetType()).Setters; foreach (var p in pis) { if (typeof(IControl).IsAssignableFrom(p.Type)) { var newInstance = p.GetValue(instance) as IControl; if (newInstance != null) { newInstance.PageId = instance.PageId; newInstance.TargetId = p.Name; newInstance.ParentId = instance.Id; if (newInstance.Id.IsNullOrEmpty()) { newInstance.Id = newInstance.ParentId + newInstance.GetType().Name; } RecSave(newInstance); } } if (p.Type.IsGenericType && typeof(IEnumerable).IsAssignableFrom(p.Type) && !p.Name.Equals("Controls", StringComparison.CurrentCultureIgnoreCase)) { var lists = p.GetValue(instance) as IEnumerable; if (lists != null) { int i = 0; foreach (var list in lists) { var newInstance = list as IControl; newInstance.PageId = instance.PageId; newInstance.TargetId = p.Name; newInstance.ParentId = instance.Id; if (newInstance.Id.IsNullOrEmpty()) { newInstance.Id = newInstance.ParentId + newInstance.GetType().Name + i; } RecSave(newInstance); i++; } } } } tran.Complete(); } }
public override Field Parse(XmlElement fieldNode, bool optional, ParsingContext context) { Operator op = Operator.None; string defaultValue = null; string key = null; string ns = ""; XmlElement operatorElement = GetOperatorElement(fieldNode); if (operatorElement != null) { if (operatorElement.HasAttribute("value")) { defaultValue = operatorElement.GetAttribute("value"); } op = Operator.GetOperator(operatorElement.LocalName); if (operatorElement.HasAttribute("key")) { key = operatorElement.GetAttribute("key"); } if (operatorElement.HasAttribute("ns")) { ns = operatorElement.GetAttribute("ns"); } if (operatorElement.HasAttribute("dictionary")) { context.Dictionary = operatorElement.GetAttribute("dictionary"); } } FastType type = GetType(fieldNode, context); var scalar = new Scalar(GetName(fieldNode, context), type, op, type.GetValue(defaultValue), optional); if (fieldNode.HasAttribute("id")) { scalar.Id = fieldNode.GetAttribute("id"); } if (key != null) { scalar.Key = new QName(key, ns); } scalar.Dictionary = context.Dictionary; ParseExternalAttributes(fieldNode, scalar); return(scalar); }
public object Resolve(string name) { object value; var fastType = FastType.Get(Sec.User.GetType()); var getter = fastType.GetGetter(name); if (name == "RoleName") { return(string.Join("|", Sec.User.Roles.Select(o => o.RoleType.Name))); } if (getter != null) { return(getter.GetValue(Sec.User)); } else { return(Sec.User.Properties.TryGetValue(name, out value) ? value : null); } }
public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer) { var result = new Dictionary <string, object>(); FastType ft = FastType.Get(obj.GetType()); string propertyName = string.Empty; foreach (var p in ft.Getters) { if (p.Info.GetCustomAttribute <ScriptIgnoreAttribute>(true) != null) { continue; } propertyName = p.Info.Name; var attr1 = p.Info.GetCustomAttribute <ScriptNameAttribute>(true); if (attr1 != null) { propertyName = attr1.Name; } if (p.Type == typeof(DateTime)) { var attr = p.Info.GetCustomAttribute <TableFieldAttribute>(true); if (attr != null) { string value = string.Format(CultureInfo.CurrentCulture, attr.DataFormatString, new object[] { p.GetValue(obj) }); result.Add(propertyName, value); } else { result.Add(propertyName, p.GetValue(obj).ConvertTo <DateTime>().ToString("yyyy年MM月dd日")); } } else if (p.Type.IsEnum) { result.Add(propertyName, p.GetValue(obj).ConvertTo <int>()); } else { result.Add(propertyName, p.GetValue(obj)); } } return(result); }
private void GetChildren(IControl e) { var children = Controls.Where(o => o.ParentId.Equals(e.Id)); if (children.IsNullOrEmpty()) { return; } var fastType = FastType.Get(e.GetType()); foreach (var ee in children) { var gettter = fastType.GetGetter(ee.TargetId); if (gettter.Type.IsGenericType) { var instance = gettter.GetValue(e); if (instance == null) { var t = typeof(List <>); var type = gettter.Type.GetGenericArguments()[0]; t = t.MakeGenericType(type); instance = Activator.CreateInstance(t); gettter.SetValue(e, instance); } var add = instance.GetType().GetMethod("Add"); add.Invoke(instance, new object[] { ee }); } else { if (gettter.GetValue(e) != null) { throw new FoxOneException("This Property:{0} of Control:{1} is already Set", ee.TargetId, e.Id); } gettter.SetValue(e, ee); } GetChildren(ee); } }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var request = controllerContext.HttpContext.Request.Form; string entityTypeFullName = request[EntityFullNameHiddenName]; if (entityTypeFullName.IsNullOrEmpty()) { throw new FoxOneException("Missing_ControlType_FullName"); } var type = TypeHelper.GetType(entityTypeFullName); object entity = Activator.CreateInstance(type); var pis = FastType.Get(type).Setters; foreach (var p in pis) { if (request.AllKeys.Contains(p.Name, StringComparer.Create(Thread.CurrentThread.CurrentCulture, true))) { var requestValue = request[p.Name]; p.SetValue(entity, requestValue.ConvertToType(p.Info.PropertyType)); } } return(entity); }
protected static void AssertScalarField(Scalar scalar, FastType type, String name, String id, String ns, String dictionary, String key, String keyNamespace, Operator op, ScalarValue defaultVal, bool optional) { var qname = new QName(name, ns); Assert.AreEqual(type, scalar.FastType); Assert.AreEqual(op, scalar.Operator); Assert.AreEqual(qname, scalar.QName); var keyName = new QName(key, keyNamespace); Assert.AreEqual(keyName, scalar.Key); if (id == null) { Assert.True(scalar.IsIdNull()); } else { Assert.AreEqual(id, scalar.Id); } Assert.AreEqual(dictionary, scalar.Dictionary); Assert.AreEqual(defaultVal, scalar.DefaultValue); Assert.AreEqual(optional, scalar.IsOptional); }
public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer) { var result = new Dictionary <string, object>(); FastType ft = FastType.Get(obj.GetType()); foreach (var p in ft.Getters) { if (p.Info.GetCustomAttribute <ScriptIgnoreAttribute>(true) != null || p.Type.IsGenericType || typeof(IControl).IsAssignableFrom(p.Type)) { continue; } if (p.Info.GetCustomAttribute <HtmlEncodeAttribute>(true) != null) { result.Add(p.Name, HttpUtility.HtmlEncode(p.GetValue(obj))); } else { result.Add(p.Name, p.GetValue(obj)); } } return(result); }
public ComposedScalar(string name, FastType fastType, Scalar[] fields, bool optional, IComposedValueConverter valueConverter) : this(new QName(name), fastType, fields, optional, valueConverter) { }
public Scalar(QName name, FastType fastType, OperatorCodec operatorCodec, ScalarValue defaultValue, bool optional) : this(name, fastType, operatorCodec.Operator, operatorCodec, defaultValue, optional) { }
public Scalar(QName name, FastType fastType, Operator op, ScalarValue defaultValue, bool optional) : this(name, fastType, op, op.GetCodec(fastType), defaultValue, optional) { }