Пример #1
0
 private static void Parse(ILGenerator il, Type type)
 {
     if (type.GetTypeInfo().IsValueType)
     {
         //可空类型
         if (EmitUtil.IsNullable(type))
         {
             var realType = EmitUtil.GetNullableArg0(type);
             il.Emit(OpCodes.Call, realType.GetTypeInfo().GetMethod("Parse", new[] { typeof(string) }));
             // ReSharper disable once AssignNullToNotNullAttribute
             il.Emit(OpCodes.Newobj, type.GetTypeInfo().GetConstructor(new[] { realType }));
         }
         else
         {
             il.Emit(OpCodes.Call, type.GetTypeInfo().GetMethod("Parse", new[] { typeof(string) }));
         }
     }
 }
Пример #2
0
        private static LocalBuilder GetRealLocal(ILGenerator il, Type type, Dictionary <Type, LocalBuilder> realValueDict)
        {
            LocalBuilder localBuilder;
            Type         realType = type;

            //可空类型或者Datetime类型,都需要将参数存储起来
            if (EmitUtil.IsNullable(type))
            {
                realType = EmitUtil.GetNullableArg0(type);
            }
            if (realValueDict.ContainsKey(realType))
            {
                localBuilder = realValueDict[realType];
            }
            else
            {
                localBuilder = il.DeclareLocal(realType);
                realValueDict.Add(realType, localBuilder);
            }
            return(localBuilder);
        }
Пример #3
0
        private static void PropertyToString(ILGenerator il, Type type, Dictionary <Type, LocalBuilder> valueDict)
        {
            //值类型和可空类型
            if (type.GetTypeInfo().IsValueType)
            {
                //不可空类型,直接创建新的,可空类型就先从字典中取,取不到再新建

                var localBuilder = GetLocal(il, type, valueDict);
                il.Emit(OpCodes.Stloc_S, localBuilder);
                il.Emit(OpCodes.Ldloca_S, localBuilder);

                if (type == typeof(DateTime) || type == typeof(DateTime?))
                {
                    //可空datetime类型
                    if (type == typeof(DateTime?))
                    {
                        il.Emit(OpCodes.Call, type.GetTypeInfo().GetMethod("get_Value", new Type[] { }));
                        var realLocal = GetRealLocal(il, type, valueDict);
                        il.Emit(OpCodes.Stloc_S, realLocal);
                        il.Emit(OpCodes.Ldloca_S, realLocal);
                    }
                    il.Emit(OpCodes.Call, CultureInfo);
                    il.Emit(OpCodes.Call, DatetimeToString);
                }
                else
                {
                    if (EmitUtil.IsNullable(type))
                    {
                        il.Emit(OpCodes.Call, type.GetTypeInfo().GetMethod("get_Value", new Type[] { }));
                        var realLocal = GetRealLocal(il, type, valueDict);
                        il.Emit(OpCodes.Stloc_S, realLocal);
                        il.Emit(OpCodes.Ldloca_S, realLocal);
                    }

                    il.Emit(OpCodes.Call, EmitUtil.GetNullableArg0(type).GetTypeInfo().GetMethod("ToString", new Type[] { }));
                }
            }
        }