protected virtual IList DeserializeArray(JsonReader jreader, JsonSerializer serializer, Type type, IList sourceList) { if (type == null) { while (jreader.Read() && jreader.TokenType != JsonToken.EndArray) { } return(null); } var typeInfo = Serialization.Instance.GetTypeInfo(type); var itemType = typeInfo.ListItemType; var temp = sourceList ?? (IList)EmitInvoker.CreateObject(type); temp.Clear(); while (jreader.Read() && jreader.TokenType != JsonToken.EndArray) { var item = Read(jreader, itemType, serializer, null); if (item == null) { continue; } temp.Add(item); } return(temp); }
public static Func <T, V> GetEmitGet(MethodInfo info) { var method = new DynamicMethod(EmitInvoker.GetMethodName(info), typeof(V), new Type[] { typeof(T) }, info.DeclaringType, true); ILGenerator il = method.GetILGenerator(); if (!info.IsStatic) { il.Emit(OpCodes.Ldarg_0); } if (info.IsStatic) { il.EmitCall(OpCodes.Call, info, null); } else { il.EmitCall(OpCodes.Callvirt, info, null); } il.Emit(OpCodes.Ret); return((Func <T, V>)method.CreateDelegate(typeof(Func <T, V>))); }
public static Action <T, V> GetEmitSet(MethodInfo info) { if (info == null) { return(null); } DynamicMethod method = new DynamicMethod(EmitInvoker.GetMethodName(info), typeof(void), new Type[] { typeof(T), typeof(V) }, info.DeclaringType, true); var il = method.GetILGenerator(); if (!info.IsStatic) { il.Emit(OpCodes.Ldarg_0); } il.Emit(OpCodes.Ldarg_1); if (info.IsStatic) { il.EmitCall(OpCodes.Call, info, null); } else { il.EmitCall(OpCodes.Callvirt, info, null); } il.Emit(OpCodes.Ret); return((Action <T, V>)method.CreateDelegate(typeof(Action <T, V>))); }
private static Func <object[], object> GetConstructorInvoker(ConstructorInfo info) { var dynamicMethod = new DynamicMethod( string.Format("Dynamic{0}{1}{2}", info.DeclaringType.Name.Replace("`1", ""), "Ctor", info.IsGenericMethod ? "G" : ""), typeof(object), new Type[] { typeof(object[]) }, true); var il = dynamicMethod.GetILGenerator(); var ps = info.GetParameters(); var locals = new LocalBuilder[ps.Length]; for (int i = 0; i < ps.Length; i++) { locals[i] = il.DeclareLocal(ps[i].ParameterType, true); } for (int i = 0; i < ps.Length; i++) { il.Emit(OpCodes.Ldarg_0); EmitInvoker.EmitFastInt(il, i); il.Emit(OpCodes.Ldelem_Ref); EmitInvoker.EmitCastToReference(il, ps[i].ParameterType); il.Emit(OpCodes.Stloc, locals[i]); } for (int i = 0; i < ps.Length; i++) { il.Emit(OpCodes.Ldloc, locals[i]); } il.Emit(OpCodes.Newobj, info); il.Emit(OpCodes.Ret); return((Func <object[], object>)dynamicMethod.CreateDelegate(typeof(Func <object[], object>))); }
public static Pull Fabric(Type type, int blockSize) { Type gtype = type.IsValueType || type.IsEnum ? typeof(NullablePullArray <>).MakeGenericType(type) : typeof(PullArray <>).MakeGenericType(type); return((Pull)EmitInvoker.CreateObject(gtype, ctorTypes, new object[] { blockSize }, true)); }
public TypeSerializationInfo(Type type, IEnumerable <PropertyInfo> properties) { Type = type; TypeName = TypeHelper.FormatBinary(Type); IsAttribute = TypeHelper.IsXmlAttribute(Type); if (IsAttribute) { Serialazer = TypeHelper.GetValueSerializer(type); return; } if (!Type.IsInterface) { Constructor = EmitInvoker.Initialize(type, Type.EmptyTypes); } IsList = TypeHelper.IsList(type); if (IsList) { IsNamedList = TypeHelper.IsInterface(type, typeof(INamedList)); ListItemType = TypeHelper.GetItemType(type); ListDefaulType = ListItemType != typeof(object) && !ListItemType.IsInterface && !type.IsGenericType && !type.IsArray && !TypeHelper.IsInterface(type, typeof(ISortable)); ListItemIsAttribute = TypeHelper.IsXmlAttribute(ListItemType); ListConstructor = EmitInvoker.Initialize(type, new[] { typeof(int) }); } IsDictionary = TypeHelper.IsDictionary(type); Properties = new NamedList <PropertySerializationInfo>(); Properties.Indexes.Add(PropertySaveInfoIsAttributeInvoker.Instance); foreach (var property in properties) { if (TypeHelper.IsNonSerialize(property)) { var exist = GetProperty(property.Name); if (exist != null) { Properties.Remove(exist); } continue; } var info = new PropertySerializationInfo(property); { var exist = GetProperty(info.Name); if (exist != null) { Properties.Remove(exist); } Properties.Add(info); } } }
public CompaundInvoker(string property, List <MemberParseInfo> list) { this.property = property; foreach (var info in list) { invokers.Add(EmitInvoker.Initialize(info.Info, info.Index == null, info.Index)); } lastInvoker = invokers.LastOrDefault(); }
public TypeSerializationInfo(Type type, IEnumerable <PropertyInfo> properties) { Type = type; TypeName = TypeHelper.FormatBinary(Type); IsAttribute = TypeHelper.IsSerializeAttribute(Type); if (IsAttribute) { Serialazer = TypeHelper.GetValueSerializer(type); return; } if (!Type.IsInterface && !Type.IsAbstract) { Constructor = EmitInvoker.Initialize(type, Type.EmptyTypes); } IsList = TypeHelper.IsList(type); if (IsList) { IsNamedList = TypeHelper.IsInterface(type, typeof(INamedList)); ListItemType = TypeHelper.GetItemType(type); ListDefaulType = ListItemType != typeof(object) && !ListItemType.IsInterface && !type.IsGenericType && !type.IsArray && !TypeHelper.IsInterface(type, typeof(ISortable)); ListItemIsAttribute = TypeHelper.IsSerializeAttribute(ListItemType); ListConstructor = EmitInvoker.Initialize(type, new[] { typeof(int) }); } IsDictionary = TypeHelper.IsDictionary(type); Properties = new NamedList <PropertySerializationInfo>(6, (ListIndex <PropertySerializationInfo, string>)PropertySerializationInfo.NameInvoker.Instance.CreateIndex(false)); Properties.Indexes.Add(PropertySerializationInfo.IsAttributeInvoker.Instance); int order = 0; foreach (var property in properties) { var exist = GetProperty(property.Name); if (TypeHelper.IsNonSerialize(property)) { if (exist != null) { Properties.Remove(exist); } continue; } //var method = property.GetGetMethod() ?? property.GetSetMethod(); if (exist != null)// && method.Equals(method.GetBaseDefinition()) { Properties.Remove(exist); } Properties.Add(new PropertySerializationInfo(property, ++order)); } Properties.ApplySortInternal(PropertySerializationInfo.OrderInvoker.Instance.CreateComparer <PropertySerializationInfo>()); }
public static object CreateObject(Type type) { if (type == typeof(string)) { return(""); } var invoker = EmitInvoker.Initialize(type, Type.EmptyTypes, true); return(invoker?.Create()); }
protected virtual IList DeserializeArray(ref Utf8JsonReader jreader, JsonSerializerOptions options, Type type, IList sourceList) { if (type == null) { while (jreader.Read() && jreader.TokenType != JsonTokenType.EndArray) { } return(null); } var itemType = TypeHelper.GetItemType(type); var client = Client.Provider.GetClient(itemType); var temp = sourceList ?? (IList)EmitInvoker.CreateObject(type); var referenceList = temp as IReferenceList; if (referenceList != null && client != null && referenceList.Owner.SyncStatus == SynchronizedStatus.Load) { var referanceBuffer = new HashSet <ISynchronized>((IEnumerable <ISynchronized>)referenceList); while (jreader.Read() && jreader.TokenType != JsonTokenType.EndArray) { #if NETSTANDARD2_0 var item = Read(ref jreader, itemType, options, null); #else var item = client.Converter.Read(ref jreader, itemType, options); #endif if (item is ISynchronized synched) { referenceList.Add(item); referanceBuffer.Remove(synched); } } foreach (var item in referanceBuffer) { if (!client.Remove(item)) { referenceList.Remove(item); } } } else { temp.Clear(); while (jreader.Read() && jreader.TokenType != JsonTokenType.EndArray) { var item = Read(ref jreader, itemType, options, null); if (item == null) { continue; } temp.Add(item); } } return(temp); }
public static Func <T, K, V> GetInvokerGet(MethodInfo info) { var ps = info.GetParameters(); var method = new DynamicMethod(EmitInvoker.GetMethodName(info), typeof(V), new Type[] { typeof(T), typeof(K) }, true); ILGenerator il = method.GetILGenerator(); if (!info.IsStatic) { il.Emit(OpCodes.Ldarg_0); } if (ps.Length > 0) { il.Emit(OpCodes.Ldarg_1); } for (int i = 1; i < ps.Length; i++) { //if (ps[i].ParameterType.IsByRef) // il.Emit(OpCodes.Ldloca_S, locals[i - 1]); //else // il.Emit(OpCodes.Ldloc, locals[i - 1]); if (ps[i].DefaultValue is Enum) { EmitInvoker.EmitFastInt(il, (int)ps[i].DefaultValue); //EmitCastToReference(il, ps[i].ParameterType); //il.Emit(OpCodes.Castclass, ps[i].ParameterType); } else if (ps[i].DefaultValue is int) { EmitInvoker.EmitFastInt(il, (int)ps[i].DefaultValue); } else if (ps[i].DefaultValue is string) { il.Emit(OpCodes.Ldstr, (string)ps[i].DefaultValue); } else if (ps[i].DefaultValue == null) { il.Emit(OpCodes.Ldnull); } } if (info.IsStatic) { il.EmitCall(OpCodes.Call, info, null); } else { il.EmitCall(OpCodes.Callvirt, info, null); } il.Emit(OpCodes.Ret); return((Func <T, K, V>)method.CreateDelegate(typeof(Func <T, K, V>))); }
public static IQueryParameter CreateTreeFilter(Type type) { var parameter = (IQueryParameter)EmitInvoker.CreateObject(typeof(QueryParameter <>).MakeGenericType(type)); parameter.Invoker = (IInvoker)EmitInvoker.CreateObject(typeof(TreeInvoker <>).MakeGenericType(type)); parameter.Comparer = CompareType.Equal; parameter.Value = true; parameter.IsGlobal = true; parameter.FormatIgnore = true; return(parameter); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var itemType = TypeHelper.GetItemType(objectType); var items = (existingValue as IList) ?? (IList)EmitInvoker.CreateObject(objectType); while (reader.Read() && reader.TokenType != JsonToken.EndArray) { items.Add(serializer.Deserialize(reader, itemType)); } return(items); }
public static object Parse(Type type, string value) { try { return(Enum.Parse(type, value)); } catch { var itemType = (typeof(EnumItem <>)).MakeGenericType(type); return(EmitInvoker.Invoke(itemType, nameof(Parse), null, value)); } }
public static ValueSerializer GetValueSerializer(Type type) { if (!cacheValueSerializer.TryGetValue(type, out var serializer)) { var attribute = type.GetCustomAttribute <ValueSerializerAttribute>(false); serializer = null; if (attribute != null && attribute.ValueSerializerType != null) { serializer = (ValueSerializer)CreateObject(attribute.ValueSerializerType); } else if (type == typeof(string)) { serializer = StringValueSerializer.Instance; } else if (type == typeof(int)) { serializer = IntValueSerializer.Instance; } else if (type == typeof(double)) { serializer = DoubleValueSerializer.Instance; } else if (type == typeof(DateTime)) { serializer = DateTimeValueSerializer.Instance; } else if (type == typeof(Type)) { serializer = TypeValueSerializer.Instance; } else if (type == typeof(CultureInfo)) { serializer = CultureInfoValueSerializer.Instance; } else if (type.IsEnum) { serializer = (ValueSerializer)EmitInvoker.CreateObject(typeof(EnumValueSerializer <>).MakeGenericType(type)); } else { var converter = GetTypeConverter(type); if (converter != null) { serializer = new TypeConverterValueSerializer { Converter = converter }; } } return(cacheValueSerializer[type] = serializer); } return(serializer); }
public PropertySerializationInfo(PropertyInfo property) { Property = property; IsText = TypeHelper.IsXmlText(property); IsAttribute = TypeHelper.IsXmlAttribute(property) && !IsText; Invoker = EmitInvoker.Initialize(property, true); Default = TypeHelper.GetDefault(property); if (IsAttribute || IsText) { Serialazer = TypeHelper.GetValueSerializer(property); } Name = property.Name; }
public static string GetFullName(IGroup item, string separator, string member) { var invoker = EmitInvoker.Initialize(item.GetType(), member); string rez = ""; IGroup g = item; while (g != null) { object val = invoker.GetValue(g); if (val != null) { rez = val + separator + rez; } g = g.Group; } return(rez.Substring(0, rez.Length - separator.Length)); }
private GetHandler <T, V> GetInvokerGet(MethodInfo info) { var method = new DynamicMethod(EmitInvoker.GetMethodName(info), typeof(V), new Type[] { typeof(T) }, true); ILGenerator il = method.GetILGenerator(); if (!info.IsStatic) { il.Emit(OpCodes.Ldarga_S, 0); //il.Emit(OpCodes.Ldind_Ref); } il.EmitCall(OpCodes.Call, info, null); il.Emit(OpCodes.Ret); return((GetHandler <T, V>)method.CreateDelegate(typeof(GetHandler <T, V>))); }
public void Load() { if (File.Exists(FileName)) { if (Project is IFileSerialize) { ((IFileSerialize)Project).Load(FileName); } else { Project = Serialization.Deserialize(FileName, Project); } } if (Editor == null && type != null) { Editor = EmitInvoker.CreateObject(type.Editor, true) as IProjectEditor; } synch = true; }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = base.CreateProperty(member, memberSerialization); if (property.Ignored) { return(property); } if (property.NullValueHandling != null) { if (TypeHelper.IsInterface(property.DeclaringType, typeof(ISynchronized))) { var propertyName = property.PropertyName; if (!TypeHelper.IsEnumerable(property.PropertyType)) { property.ShouldSerialize = instance => { var e = (ISynchronized)instance; return(e.Changes.ContainsKey(propertyName)); }; } else if (TypeHelper.IsInterface(TypeHelper.GetItemType(property.PropertyType), typeof(ISynchronized))) { var propertyInvoker = EmitInvoker.Initialize(property.DeclaringType, propertyName); property.ShouldSerialize = instance => { var collection = (IEnumerable)propertyInvoker.GetValue(instance); return(collection != null && collection.TypeOf <ISynchronized>().Any(p => p.SyncStatus != SynchronizedStatus.Actual)); }; } } } else if (member.GetCustomAttribute <JsonIgnoreSerializationAttribute>() != null) { property.ShouldSerialize = instance => false; } return(property); }
public PropertySerializationInfo(PropertyInfo property, int order = -1) { Property = property; Name = property.Name; DataType = Property.PropertyType; var keys = PropertySerializationInfoKeys.None; if (TypeHelper.IsSerializeText(property)) { keys |= PropertySerializationInfoKeys.Text; } else if (TypeHelper.IsSerializeAttribute(property)) { keys |= PropertySerializationInfoKeys.Attribute; } if (TypeHelper.IsSerializeWriteable(property)) { keys |= PropertySerializationInfoKeys.Writeable; } if (TypeHelper.IsRequired(property)) { keys |= PropertySerializationInfoKeys.Required; } if (TypeHelper.IsJsonSynchronized(property)) { keys |= PropertySerializationInfoKeys.ChangeSensitive; } if (TypeHelper.IsReadOnly(property)) { keys |= PropertySerializationInfoKeys.ReadOnly; } Keys = keys; Order = TypeHelper.GetOrder(property, order); Invoker = EmitInvoker.Initialize(property, true); Default = TypeHelper.GetDefault(property); if (IsAttribute || IsText) { Serialazer = TypeHelper.GetValueSerializer(property); } }
public static Action <T, K, V> GetInvokerSet(MethodInfo info) { DynamicMethod method = new DynamicMethod(EmitInvoker.GetMethodName(info), typeof(void), new Type[] { typeof(T), typeof(K), typeof(V) }, true); var ps = info.GetParameters(); var il = method.GetILGenerator(); if (!info.IsStatic) { il.Emit(OpCodes.Ldarg_0); } if (ps.Length > 0) { il.Emit(OpCodes.Ldarg_1); } if (ps.Length > 1) { il.Emit(OpCodes.Ldarg_2); } for (int i = 2; i < ps.Length; i++) { //if (ps[i].ParameterType.IsByRef) // il.Emit(OpCodes.Ldloca_S, locals[i - 2]); //else // il.Emit(OpCodes.Ldloc, locals[i - 2]); } if (info.IsStatic) { il.EmitCall(OpCodes.Call, info, null); } else { il.EmitCall(OpCodes.Callvirt, info, null); } il.Emit(OpCodes.Ret); return((Action <T, K, V>)method.CreateDelegate(typeof(Action <T, K, V>))); }
public static Func <T, V> GetFieldGetInvoker(FieldInfo info) { var dynamicMethod = new DynamicMethod( string.Format("Dynamic{0}{1}Get", info.DeclaringType.Name, info.Name), typeof(V), new Type[] { typeof(T) }, true); ILGenerator il = dynamicMethod.GetILGenerator(); if (!info.IsStatic) { il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, info); } else { var val = info.GetRawConstantValue(); if (val is int) { EmitInvoker.EmitFastInt(il, (int)val); } else if (val is long) { il.Emit(OpCodes.Ldc_I8, (long)val); } else if (val is float) { il.Emit(OpCodes.Ldc_R4, (float)val); } else { il.Emit(OpCodes.Ldsfld, info); } } //EmitBoxIfNeeded(il, info.FieldType); il.Emit(OpCodes.Ret); return((Func <T, V>)dynamicMethod.CreateDelegate(typeof(Func <T, V>))); }
public static IList Create(Type type, int capacity) { return((IList)EmitInvoker.CreateObject(type, new Type[] { typeof(int) }, new object[] { capacity }, true)); }
public static object GetValue(MemberInfo info, object item) { return(EmitInvoker.GetValue(info, item)); }
public static void SetValue(MemberInfo info, object item, object val) { EmitInvoker.SetValue(info, item, val); }
public static Func <T, object[], V> GetMethodInvoker(MethodInfo info) { var method = new DynamicMethod(EmitInvoker.GetMethodName(info), typeof(V), new Type[] { typeof(T), typeof(object[]) }, true); var il = method.GetILGenerator(); var ps = info.GetParameters(); var paramTypes = new Type[ps.Length]; var locals = new LocalBuilder[paramTypes.Length]; for (int i = 0; i < paramTypes.Length; i++) { if (ps[i].ParameterType.IsByRef) { paramTypes[i] = ps[i].ParameterType.GetElementType(); } else { paramTypes[i] = ps[i].ParameterType; } } for (int i = 0; i < paramTypes.Length; i++) { locals[i] = il.DeclareLocal(paramTypes[i], true); } for (int i = 0; i < paramTypes.Length; i++) { il.Emit(OpCodes.Ldarg_1); EmitInvoker.EmitFastInt(il, i); il.Emit(OpCodes.Ldelem_Ref); EmitInvoker.EmitCastToReference(il, paramTypes[i]); il.Emit(OpCodes.Stloc, locals[i]); } if (!info.IsStatic) { il.Emit(OpCodes.Ldarg_0); } for (int i = 0; i < paramTypes.Length; i++) { if (ps[i].ParameterType.IsByRef) { il.Emit(OpCodes.Ldloca_S, locals[i]); } else { il.Emit(OpCodes.Ldloc, locals[i]); } } if (info.IsStatic) { il.EmitCall(OpCodes.Call, info, null); } else { il.EmitCall(OpCodes.Callvirt, info, null); } if (info.ReturnType == typeof(void)) { il.Emit(OpCodes.Ldnull); } for (int i = 0; i < paramTypes.Length; i++) { if (ps[i].ParameterType.IsByRef) { il.Emit(OpCodes.Ldarg_1); EmitInvoker.EmitFastInt(il, i); il.Emit(OpCodes.Ldloc, locals[i]); if (locals[i].LocalType.IsValueType) { il.Emit(OpCodes.Box, locals[i].LocalType); } il.Emit(OpCodes.Stelem_Ref); } } il.Emit(OpCodes.Ret); return((Func <T, object[], V>)method.CreateDelegate(typeof(Func <T, object[], V>))); }
protected virtual IList ReadArray(JsonReader jreader, JsonSerializer serializer, Type type, IList sourceList) { if (type == null) { while (jreader.Read() && jreader.TokenType != JsonToken.EndArray) { } return(null); } var typeInfo = Serialization.Instance.GetTypeInfo(type); var itemType = typeInfo.ListItemType; var client = Client.Provider.GetClient(itemType); var temp = sourceList ?? (IList)EmitInvoker.CreateObject(type); var referenceList = temp as IReferenceList; if (referenceList != null && client != null) { var isLoad = referenceList.Owner.SyncStatus == SynchronizedStatus.Load || referenceList.Owner.SyncStatus == SynchronizedStatus.Actual; if (isLoad) { foreach (ISynchronized item in referenceList) { item.SyncStatus = SynchronizedStatus.Actual; } } while (jreader.Read() && jreader.TokenType != JsonToken.EndArray) { #if NETSTANDARD2_0 var item = client.Converter.Read(jreader, null, serializer); #else var item = Read(jreader, itemType, serializer, null); #endif if (item is ISynchronized synched) { referenceList.Add(item); } } //foreach (var item in referanceBuffer) //{ // if (!client.Remove(item)) // { // referenceList.Remove(item); // } //} } else { temp.Clear(); while (jreader.Read() && jreader.TokenType != JsonToken.EndArray) { var item = Read(jreader, itemType, serializer, null); if (item == null) { continue; } temp.Add(item); } } return(temp); }
public InvokerComparer(PropertyInfo info, object index, ListSortDirection direction = ListSortDirection.Ascending) : this(index == null ? EmitInvoker.Initialize(info, true) : EmitInvoker.Initialize(info, index), direction) { }
public InvokerComparer(Type type, string property, ListSortDirection direction = ListSortDirection.Ascending) : this(EmitInvoker.Initialize(type, property), direction) { }