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

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

static private CreateGetMethod ( Type type, PropertyInfo propertyInfo ) : GenericGetter
type System.Type
propertyInfo System.Reflection.PropertyInfo
Результат GenericGetter
        internal static GenericGetter CreateGetMethod(Type type, PropertyInfo propertyInfo)
        {
            MethodInfo getMethod = propertyInfo.GetGetMethod();
            if (getMethod == null)
                return null;

            DynamicMethod getter = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type);

            ILGenerator il = getter.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.EmitCall(OpCodes.Call, getMethod, null);
                if (propertyInfo.PropertyType.IsValueType)
                    il.Emit(OpCodes.Box, propertyInfo.PropertyType);
            }
            else
            {
                if (!getMethod.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
                    il.EmitCall(OpCodes.Callvirt, getMethod, null);
                }
                else
                    il.Emit(OpCodes.Call, getMethod);

                if (propertyInfo.PropertyType.IsValueType)
                    il.Emit(OpCodes.Box, propertyInfo.PropertyType);
            }

            il.Emit(OpCodes.Ret);

            return (GenericGetter)getter.CreateDelegate(typeof(GenericGetter));
        }

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::CreateGetMethod