Arango.fastJSON.Reflection.CreateSetMethod C# (CSharp) Метод

CreateSetMethod() статический приватный Метод

static private CreateSetMethod ( Type type, PropertyInfo propertyInfo ) : GenericSetter
type System.Type
propertyInfo System.Reflection.PropertyInfo
Результат GenericSetter
        internal static GenericSetter CreateSetMethod(Type type, PropertyInfo propertyInfo)
        {
            MethodInfo setMethod = propertyInfo.GetSetMethod();
            if (setMethod == null)
                return null;

            Type[] arguments = new Type[2];
            arguments[0] = arguments[1] = typeof(object);

            DynamicMethod setter = new DynamicMethod("_", typeof(object), arguments);
            ILGenerator il = setter.GetILGenerator();

            if (!type.IsClass) // structs
            {
                var lv = il.DeclareLocal(type);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Unbox_Any, type);
                il.Emit(OpCodes.Stloc_0);
                il.Emit(OpCodes.Ldloca_S, lv);
                il.Emit(OpCodes.Ldarg_1);
                if (propertyInfo.PropertyType.IsClass)
                    il.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
                else
                    il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                il.EmitCall(OpCodes.Call, setMethod, null);
                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Box, type);
            }
            else
            {
                if (!setMethod.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
                    il.Emit(OpCodes.Ldarg_1);
                    if (propertyInfo.PropertyType.IsClass)
                        il.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
                    else
                        il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                    il.EmitCall(OpCodes.Callvirt, setMethod, null);
                    il.Emit(OpCodes.Ldarg_0);
                }
                else
                {
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_1);
                    if (propertyInfo.PropertyType.IsClass)
                        il.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
                    else
                        il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                    il.Emit(OpCodes.Call, setMethod);
                }
            }

            il.Emit(OpCodes.Ret);

            return (GenericSetter)setter.CreateDelegate(typeof(GenericSetter));
        }

Usage Example

Пример #1
0
        public Dictionary <string, myPropInfo> Getproperties(Type type, string typename, bool IgnoreCaseOnDeserialize, bool customType)
        {
            Dictionary <string, myPropInfo> sd = null;

            if (_propertycache.TryGetValue(typename, out sd))
            {
                return(sd);
            }
            else
            {
                sd = new Dictionary <string, myPropInfo>();
                PropertyInfo[] pr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo p in pr)
                {
                    myPropInfo d = CreateMyProp(p.PropertyType, p.Name, customType);
                    //if (p.CanWrite)
                    //    d.Flags |= myPropInfoFlags.CanWrite;
                    d.setter = Reflection.CreateSetMethod(type, p);
                    if (d.setter != null)
                    {
                        d.CanWrite = true;// Flags |= myPropInfoFlags.CanWrite;
                    }
                    d.getter = Reflection.CreateGetMethod(type, p);
                    if (IgnoreCaseOnDeserialize)
                    {
                        sd.Add(p.Name.ToLower(), d);
                    }
                    else
                    {
                        sd.Add(p.Name, d);
                    }
                }
                FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (FieldInfo f in fi)
                {
                    myPropInfo d = CreateMyProp(f.FieldType, f.Name, customType);
                    d.CanWrite = true;// Flags |= myPropInfoFlags.CanWrite;
                    d.setter   = Reflection.CreateSetField(type, f);
                    d.getter   = Reflection.CreateGetField(type, f);
                    if (IgnoreCaseOnDeserialize)
                    {
                        sd.Add(f.Name.ToLower(), d);
                    }
                    else
                    {
                        sd.Add(f.Name, d);
                    }
                }

                _propertycache.Add(typename, sd);
                return(sd);
            }
        }
All Usage Examples Of Arango.fastJSON.Reflection::CreateSetMethod