コード例 #1
0
        public void Save(ModelSaveContext ctx)
        {
            Host.CheckValue(ctx, nameof(ctx));
            Host.Check(CanSave(), "Cannot save this transform as it was not specified as being savable");
            ctx.CheckAtModel();
            ctx.SetVersionInfo(SerializableLambdaTransform.GetVersionInfo());

            // *** Binary format ***
            // int: Number of bytes the load method was serialized to
            // byte[n]: The serialized load method info
            // <arbitrary>: Arbitrary bytes saved by the save action

            Host.AssertNonEmpty(_loadFuncBytes);
            ctx.Writer.WriteByteArray(_loadFuncBytes);

            using (var ms = new MemoryStream())
            {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, leaveOpen: true))
                    _saveAction(writer);
                ctx.Writer.WriteByteArray(ms.ToArray());
            }
        }
コード例 #2
0
        protected LambdaTransformBase(IHostEnvironment env, string name, Action <BinaryWriter> saveAction, LambdaTransform.LoadDelegate loadFunc)
        {
            Contracts.AssertValue(env);
            env.AssertNonWhiteSpace(name);
            Host = env.Register(name);

            Host.Assert((saveAction == null) == (loadFunc == null));

            if (saveAction != null)
            {
                _saveAction = saveAction;
                // First, verify as best we can, that we can recover the function, by attempting to do it once.
                _loadFuncBytes = SerializableLambdaTransform.GetSerializedStaticDelegate(loadFunc);
                Exception error;
                var       recoveredLoadFunc = SerializableLambdaTransform.DeserializeStaticDelegateOrNull(Host, _loadFuncBytes, out error);
                if (recoveredLoadFunc == null)
                {
                    Host.AssertValue(error);
                    throw Host.Except(error, "Load function does not appear recoverable");
                }
            }

            AssertConsistentSerializable();
        }