Esempio n. 1
0
        public void Precache()
        {
            Type          type   = typeof(CallModel);
            DynamicMethod method = new DynamicMethod("GetString", typeof(string), new Type[] { type });
            ILGenerator   il     = method.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, type.GetField("Age"));
            il.Emit(OpCodes.Ret);
            EmitGetString = (Func <CallModel, string>)(method.CreateDelegate(typeof(Func <CallModel, string>)));


            method = new DynamicMethod("GetDateTime", typeof(DateTime), new Type[] { type });
            il     = method.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, type.GetField("CreateTime"));
            il.Emit(OpCodes.Ret);
            EmitGetDateTime = (Func <CallModel, DateTime>)(method.CreateDelegate(typeof(Func <CallModel, DateTime>)));


            method = new DynamicMethod("SetDateString", null, new Type[] { type, typeof(string) });
            il     = method.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Stfld, type.GetField("Age"));
            il.Emit(OpCodes.Ret);
            EmitSetString = (Action <CallModel, string>)(method.CreateDelegate(typeof(Action <CallModel, string>)));


            method = new DynamicMethod("SetDateTime", null, new Type[] { type, typeof(DateTime) });
            il     = method.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Stfld, type.GetField("CreateTime"));
            il.Emit(OpCodes.Ret);
            EmitSetDateTime = (Action <CallModel, DateTime>)(method.CreateDelegate(typeof(Action <CallModel, DateTime>)));


            NatashaGetString = NFunc <CallModel, string> .Delegate("return arg.Age;");

            NatashaGetString(OriginModel);
            OriginGetString = item => item.Age;


            NatashaGetDateTime = NFunc <CallModel, DateTime> .Delegate("return arg.CreateTime;");

            NatashaGetDateTime(OriginModel);
            OriginGetDateTime = item => item.CreateTime;


            NatashaSetString = NAction <CallModel, string> .Delegate("arg1.Age=arg2;");

            NatashaSetString(OriginModel, OriginModel.Age);
            OriginSetString = (item, value) => item.Age = value;

            NatashaSetDateTime = DelegateOperator <ValueDelegate> .Delegate("model.CreateTime=value;");

            NatashaSetDateTime(OriginModel, OriginModel.CreateTime);
            OriginSetDateTime = OriginDateTime;
        }
        } // write()

        public void append(NAction <NOutputStream> appendCallback) // throws ResourceException
        {
            NOutputStream out_stream = append();

            try
            {
                appendCallback.run(out_stream);
            }
            catch (Exception e)
            {
                throw new NResourceException("Error occurred in append callback", e);
            }
            finally
            {
                FileHelper.safeClose(out_stream);
            }
        } // append()
        public void read(NAction <NInputStream> readCallback)
        {
            NInputStream in_stream = read();

            try
            {
                readCallback.run(in_stream);
            }
            catch (Exception e)
            {
                throw new NResourceException("Error occurred in read callback", e);
            }
            finally
            {
                FileHelper.safeClose(in_stream);
            }
        } // read()
        } // read()

        public void write(NAction <NOutputStream> writeCallback) // throws ResourceException
        {
            NOutputStream out_stream = write();

            try
            {
                writeCallback.run(out_stream);
            }
            catch (Exception e)
            {
                throw new NResourceException("Error occurred in write callback", e);
            }
            finally
            {
                FileHelper.safeClose(out_stream);
            }
        } // write()
        //{
        //    throw new NotImplementedException();
        //} // getName()

        public virtual void append(NAction <NInputStream> appendCallback)
        {
            throw new NotImplementedException();
        }