Пример #1
0
        public static SampleSheet DeepClone(SampleSheet sheet, Type sampleType)
        {
            // build protobuf model to take into account potentially extended Sample
            ProtoBuf.Meta.RuntimeTypeModel model = ProtoBuf.Meta.TypeModel.Create();

            var type = model.Add(typeof(SampleSheet), true);
            Type sampleSheetType = sheet.GetType();
            if (sampleSheetType != typeof(SampleSheet))
            {
                // sampleSheetType extended SampleSheet
                model.Add(sampleSheetType, true);
                type.AddSubType(49, sampleSheetType);
            }

            type = model.Add(typeof(SampleSheet.Sample), true);
            if (sampleType != typeof(SampleSheet.Sample))
            {
                // sampleType extended SampleSheet.Sample
                model.Add(sampleType, true);
                type.AddSubType(50, sampleType);
            }

            // serialize and deserialize
            using (MemoryStream stream = new MemoryStream())
            {
                model.Serialize(stream, sheet);
                stream.Position = 0;
                SampleSheet newsheet = null;
                newsheet = (SampleSheet)model.Deserialize(stream, newsheet, typeof(SampleSheet));
                return newsheet;
            }
        }
Пример #2
0
 public static SampleSheet DeepClone(SampleSheet sheet)
 {
     return DeepClone(sheet, typeof(SampleSheet.Sample));
 }