/// <summary> /// Converts to database value. /// </summary> /// <param name="member">The member.</param> /// <param name="value">The value.</param> /// <returns>System.Object.</returns> public object ConvertToDatabaseValue(DynamicMember member, object value) { if (value == null) { return(DBNull.Value); } if (member.Type == typeof(DateTime) && (DateTime)value == DateTime.MinValue || member.Type == typeof(DateTime?) && (DateTime)value == DateTime.MinValue) { return(new DateTime(1753, 01, 01)); // return (DateTime?)SqlDateTime.MinValue; } if (member.Type == typeof(byte[])) { return(value); } if (!member.Type.FullName.StartsWith("System.") || value is IEnumerable && member.Type != typeof(string) || value.GetType().IsCSharpClass()) // strings are enumerable of characters { var a = JsonConvert.SerializeObject(value); if (a == null) { return(DBNull.Value); } return(a.Replace("'", "''")); } return(value); }
/// <summary> Parameterizes a member from one object unto another. /// If the name matches an existing parameter, the member will be added to that. </summary> /// <param name="target"> The object on which to add a new member. </param> /// <param name="member"> The member to forward. </param> /// <param name="source"> The owner of the forwarded member. </param> /// <param name="name"> The name of the new property. If null, the name of the source member will be used.</param> /// <returns>The parameterization of the member..</returns> public static ParameterMemberData Parameterize(this IMemberData member, object target, object source, string name) { if (member.GetParameter(target, source) != null) { throw new Exception("Member is already parameterized."); } return(DynamicMember.ParameterizeMember(target, member, source, name)); }
public void Intercept(DynamicMember parent, System.Reflection.Emit.MethodBuilder builder, System.Reflection.Emit.ILGenerator generator, ref Label returnLabel) { var prop = parent as DynamicProperty; var hasMultipleValuesProperty = prop.DynamicTypeBuilder.DynamicMembers.OfType <DynamicProperty>().FirstOrDefault(p => p.MemberName == $"{prop.MemberName}_HasMultipleValues"); if (hasMultipleValuesProperty == null) { return; } generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4_0); generator.Emit(OpCodes.Call, hasMultipleValuesProperty.PropertySetMethod); generator.Emit(OpCodes.Nop); }
public void Intercept(DynamicMember parent, System.Reflection.Emit.MethodBuilder builder, System.Reflection.Emit.ILGenerator generator, ref Label returnLabel) { var prop = parent as DynamicProperty; var originalReturn = returnLabel; bool valueType = false; var compareMethod = prop.MemberType.GetMethod("op_Equality", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); if (compareMethod == null) { compareMethod = typeof(object).GetMethod("Equals", BindingFlags.Static | BindingFlags.Public); valueType = true; } generator.Emit(OpCodes.Nop); generator.Emit(OpCodes.Ldarg_1); if (valueType) { generator.Emit(OpCodes.Box, prop.MemberType); } if (!prop.IsOverride) { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldfld, prop.BackingField); } else { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Call, prop.PropertyGetMethod); } if (valueType) { generator.Emit(OpCodes.Box, prop.MemberType); } var endlabel = generator.DefineLabel(); generator.Emit(OpCodes.Call, compareMethod); generator.Emit(OpCodes.Ldc_I4_0); generator.Emit(OpCodes.Ceq); generator.Emit(OpCodes.Brfalse_S, returnLabel); generator.Emit(OpCodes.Br_S, endlabel); returnLabel = generator.DefineLabel(); generator.MarkLabel(returnLabel); var raiseMethod = prop.DynamicTypeBuilder.DynamicMembers .OfType <RaisePropertyChangedMethod>() .Select(p => p.Method) .OfType <MethodInfo>() .FirstOrDefault(); if (raiseMethod == null) { raiseMethod = prop.DynamicTypeBuilder.BaseType.GetMethod("OnPropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance); } if (raiseMethod == null) { #if (SILVERLIGHT) throw new MissingMemberException(string.Format("Missing Member {1} on type {0}", builder.DeclaringType.Name, "PropertyChanged")); #else throw new MissingMemberException(builder.DeclaringType.Name, "PropertyChanged"); #endif } generator.Emit(OpCodes.Nop); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldstr, prop.MemberName); generator.Emit(OpCodes.Callvirt, raiseMethod); generator.Emit(OpCodes.Br_S, originalReturn); generator.MarkLabel(endlabel); }
/// <summary> /// Dynamic member Assignment operator. /// </summary> /// <param name="DynamicMember">Dynamic member</param> /// <param name="Operand">Operand.</param> /// <param name="Start">Start position in script expression.</param> /// <param name="Length">Length of expression covered by node.</param> /// <param name="Expression">Expression containing script.</param> public DynamicMemberAssignment(DynamicMember DynamicMember, ScriptNode Operand, int Start, int Length, Expression Expression) : base(DynamicMember.LeftOperand, DynamicMember.RightOperand, Operand, Start, Length, Expression) { }
public BreakConditionDynamicMember(DynamicMember breakConditions) : base(breakConditions) { }
/// <summary> Removes a parameterization of a member. </summary> /// <param name="parameterizedMember"> The parameterized member owned by the source. </param> /// <param name="parameter"> The parameter to remove it from.</param> /// <param name="source"> The source of the member. </param> public static void Unparameterize(this IMemberData parameterizedMember, ParameterMemberData parameter, object source) { DynamicMember.UnparameterizeMember(parameter, parameterizedMember, source); }
/// <summary> This overload allows two DynamicMembers to share the same Get/Set value backing field.</summary> /// <param name="base"></param> public DynamicMember(DynamicMember @base) { dict = @base.dict; }
WriterDelegate WritePrimitiveElement(DynamicMember member, string name, object defaultValue, bool alwaysWrite) { if (alwaysWrite) { return (XElement result, ref object target) => { object value = member.GetValue(target) ?? defaultValue ?? string.Empty; result.Add(new XElement(name, value)); }; } else { return (XElement result, ref object target) => { object value = member.GetValue(target); if (value != defaultValue) result.Add(new XElement(name, value)); }; } }
WriterDelegate WriteComplexElement(DynamicMember member, string name, object defaultValue, bool alwaysWrite) { EQXSerializer serializer = EQXSerializer.GetSerializer(member.MemberType); if (alwaysWrite) { return (XElement result, ref object target) => { object value = member.GetValue(target) ?? defaultValue; if (value != null) result.Add(serializer.Serialize(ref value, name)); else result.Add(new XElement(name)); }; } else { return (XElement result, ref object target) => { object value = member.GetValue(target) ?? defaultValue; if (value != null) result.Add(new XElement(name)); }; } }
WriterDelegate WriteArray(DynamicMember member, string name, string itemName, object defaultValue, bool alwaysWrite) { EQXSerializer serializer = EQXSerializer.GetSerializer(member.ArrayItemType); if (!string.IsNullOrEmpty(itemName)) { if (alwaysWrite) { return (XElement parent, ref object target) => { XElement arrayElement = new XElement(name); IList value = member.GetValue(target) as IList; if (value != null && value.Count > 0) { for (int i = 0; i < value.Count; i++) { object itemValue = value[i]; arrayElement.Add(serializer.Serialize(ref itemValue, itemName)); } parent.Add(arrayElement); } }; } else { return (XElement parent, ref object target) => { XElement arrayElement = new XElement(name); IList value = member.GetValue(target) as IList; if (value != null) { for (int i = 0; i < value.Count; i++) { object itemValue = value[i]; arrayElement.Add(serializer.Serialize(ref itemValue, itemName)); } } parent.Add(arrayElement); }; } } else { // don't bother writing an empty element for flat arrays return (XElement parent, ref object target) => { IList value = member.GetValue(target) as IList; if (value != null) { for (int i = 0; i < value.Count; i++) { object itemValue = value[i]; parent.Add(serializer.Serialize(ref itemValue, name)); } } }; } }
ReaderDelegate ReadPrimitive(DynamicMember member, string name, object defaultValue) { return (XPathNavigator navigator, ref object target) => { XPathNavigator datanav = navigator.SelectSingleNode(name); if (datanav != null && !datanav.IsEmptyElement) { object value = null; try { value = datanav.ValueAs(member.MemberType); } catch (FormatException e) { // fixing sloppy dev typos, YAY! if (member.MemberType == typeof(bool)) { bool b; if (bool.TryParse(datanav.Value.ToLower(), out b)) value = b; else value = false; } else if (member.MemberType == typeof(int)) { string val = datanav.Value; if (val.EndsWith("+")) val = val.Replace("+", ""); int i; if (int.TryParse(val, out i)) value = i; else value = 0; } else throw e; } member.SetValue(ref target, value); } else if (defaultValue != null) member.SetValue(ref target, defaultValue); }; }
ReaderDelegate ReadComplex(DynamicMember member, string name, object defaultValue) { EQXSerializer serializer = EQXSerializer.GetSerializer(member.MemberType); return (XPathNavigator navigator, ref object target) => { XPathNavigator datanav = navigator.SelectSingleNode(name); if (datanav != null && !datanav.IsEmptyElement) { object value = null; serializer.Deserialize(datanav, ref value); member.SetValue(ref target, value); } else if (defaultValue != null) member.SetValue(ref target, defaultValue); }; }
ReaderDelegate ReadArray(DynamicMember member, string name, string itemName, object defaultValue) { EQXSerializer serializer = EQXSerializer.GetSerializer(member.ArrayItemType); if (!string.IsNullOrEmpty(itemName)) { return (XPathNavigator navigator, ref object target) => { XPathNodeIterator it = null; XPathNavigator datanav = navigator.SelectSingleNode(name); if (datanav != null && !datanav.IsEmptyElement) it = datanav.Select(string.Concat("./", itemName)); if (it != null && it.Count > 0) { IList value = Activator.CreateInstance(member.MemberType, it.Count) as IList; int i = 0; while (it.MoveNext()) { XPathNavigator itemnav = it.Current; if (itemnav != null && !itemnav.IsEmptyElement) { object itemValue = null; serializer.Deserialize(itemnav, ref itemValue); value[i++] = itemValue; } } member.SetValue(ref target, value); } else member.SetValue(ref target, defaultValue); }; } return (XPathNavigator navigator, ref object target) => { XPathNodeIterator it = null; if (itemName == null) it = navigator.Select("./" + name); else { XPathNavigator datanav = navigator.SelectSingleNode(name); if (datanav != null && !datanav.IsEmptyElement) it = datanav.Select("./" + itemName); } if (it != null && it.Count > 0) { IList value = Activator.CreateInstance(member.MemberType, it.Count) as IList; int i = 0; while (it.MoveNext()) { XPathNavigator itemnav = it.Current; if (itemnav != null && !itemnav.IsEmptyElement) { object itemValue = null; serializer.Deserialize(itemnav, ref itemValue); value[i++] = itemValue; } } member.SetValue(ref target, value); } }; }