|
642,31 |
642,6 |
|
**シリアライズによる複製 [#CloneUsingSerialization]
|
**シリアライズによる複製 [#CloneUsingSerialization]
|
|
詳細コピーを行う方法の一つとして、シリアライズを利用する方法があります。 例として、シリアライズ方法のひとつである[[バイナリシリアル化>programming/netfx/serialization/0_abstract#BinaryFormatter]]を用いて詳細コピーを行う例を挙げます。
|
詳細コピーを行う方法の一つとして、シリアライズを利用する方法があります。 例として、シリアライズ方法のひとつである[[バイナリシリアル化>programming/netfx/serialization/0_abstract#BinaryFormatter]]を用いて詳細コピーを行う例を挙げます。
|
|
|
|
+ |
$remarks
|
|
+ |
この例で使用しているBinaryFormatter.&msdn(netfx,member,System.Runtime.Serialization.Formatters.dll,System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize){Serialize};/&msdn(netfx,member,System.Runtime.Serialization.Formatters.dll,System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize){Deserialize};について、.NET 5以降においては''使用は推奨されず、できるだけ早く使用をやめる必要がある''とされています。 以下のコードでは、これを示すコンパイル時警告[[SYSLIB0011:https://docs.microsoft.com/ja-jp/dotnet/core/compatibility/syslib0011]]が出力されます。 特にASP.NET 5.0以降では、明示的にBinaryFormatterの使用を有効にしない限り''常に例外&msdn(netfx,type,System.NotSupportedException){NotSupportedException};がスローされます''。
|
|
+ |
|
|
+ |
$blockquote(cite-title=BinaryFormatter セキュリティ ガイド | Microsoft Docs,cite=https://docs.microsoft.com/ja-jp/dotnet/standard/serialization/binaryformatter-security-guide)
|
|
+ |
BinaryFormatter 型は危険であり、データ処理用としては "推奨されません"。 アプリケーションでは、処理するデータが信頼できると思われる場合でも、できるだけ早く BinaryFormatter の使用をやめる必要があります。 BinaryFormatter は安全ではなく、セキュリティで保護することはできません。
|
|
+ |
$blockquote$
|
|
+ |
|
|
+ |
$blockquote(cite-title=基本クラス ライブラリの破壊的変更 - .NET Core | Microsoft Docs,cite=https://docs.microsoft.com/ja-jp/dotnet/core/compatibility/corefx#binaryformatter-serialization-methods-are-obsolete-and-prohibited-in-aspnet-apps)
|
|
+ |
#heading(BinaryFormatter シリアル化メソッドが古い形式になり、ASP.NET アプリでは使用不可に)
|
|
+ |
|
|
+ |
BinaryFormatter、Formatter、および IFormatter の Serialize と Deserialize のメソッドが古いと見なされ、警告が示されるようになりました。 また、ASP.NET アプリでは、BinaryFormatter のシリアル化が既定で禁止されます。
|
|
+ |
|
|
+ |
#heading(変更の説明)
|
|
+ |
|
|
+ |
BinaryFormatter のセキュリティ脆弱性により、次のメソッドは古いと見なされ、ID SYSLIB0011 のコンパイル時警告が生成されるようになりました。 また、ASP.NET Core 5.0 以降のアプリでは、Web アプリによって BinaryFormatter 機能が再有効化されていない限り、NotSupportedException がスローされます。
|
|
+ |
|
|
+ |
-BinaryFormatter.Serialize
|
|
+ |
-BinaryFormatter.Deserialize
|
|
+ |
$blockquote$
|
|
+ |
|
|
+ |
これに従い、(特にBinaryFormatter/SoapFormatterを使用した)シリアライズによるオブジェクトの複製は、ASP.NET 5.0以降では明示的に有効にしない限り使用できない手段で、またそれ以外のフレームワークでも推奨できる手段ではなくなっています。
|
|
+ |
|
|
+ |
オブジェクトの''詳細コピー''を行いたい場合は、[[コピーコンストラクタ>#CloneUsingCopyConstructor]]や[[MemberwiseCloneメソッド>#Object.MemberwiseClone]]、またそれらによる複製を実装・提供する[[ICloneableインターフェイス>#ICloneable]]・複製用のメソッドを組み合わせることにより、オブジェクト内における''複製が必要なフィールドを再帰的に詳細コピーする''のが望ましい実装となります。
|
|
+ |
$remarks$
|
|
+ |
|
|
|
$samplecode(lang=c#,copyright-year=2016,シリアライズによる複製)
|
$samplecode(lang=c#,copyright-year=2016,シリアライズによる複製)
|
|
#code{{
|
#code{{
|
|
using System;
|
using System;
|
|
688,13 |
663,13 |
|
BinaryFormatter f = new BinaryFormatter();
|
BinaryFormatter f = new BinaryFormatter();
|
|
|
|
|
// 現在のインスタンスをシリアル化してMemoryStreamに格納
|
// 現在のインスタンスをシリアル化してMemoryStreamに格納
|
~ |
f.Serialize(stream, this); // [.NET 5] warning SYSLIB0011: 'BinaryFormatter.Serialize(Stream, object)' は旧形式です ('BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.')
|
f.Serialize(stream, this);
|
|
|
|
|
// ストリームの位置を先頭に戻す
|
// ストリームの位置を先頭に戻す
|
|
stream.Position = 0L;
|
stream.Position = 0L;
|
|
|
|
|
// MemoryStreamに格納された内容を逆シリアル化する
|
// MemoryStreamに格納された内容を逆シリアル化する
|
~ |
return (Account)f.Deserialize(stream); // [.NET 5] warning SYSLIB0011: 'BinaryFormatter.Deserialize(Stream)' は旧形式です ('BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.')
|
return (Account)f.Deserialize(stream);
|
|
}
|
}
|
|
}
|
}
|
|
}
|
}
|
|
742,13 |
717,13 |
|
Dim f As New BinaryFormatter()
|
Dim f As New BinaryFormatter()
|
|
|
|
|
' 現在のインスタンスをシリアル化してMemoryStreamに格納
|
' 現在のインスタンスをシリアル化してMemoryStreamに格納
|
~ |
f.Serialize(stream, Me) ' [.NET 5] warning SYSLIB0011: 'Public Overloads Sub Serialize(serializationStream As Stream, graph As Object)' は廃止されています: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.'。
|
f.Serialize(stream, Me)
|
|
|
|
|
' ストリームの位置を先頭に戻す
|
' ストリームの位置を先頭に戻す
|
|
stream.Position = 0L
|
stream.Position = 0L
|
|
|
|
|
' MemoryStreamに格納された内容を逆シリアル化する
|
' MemoryStreamに格納された内容を逆シリアル化する
|
~ |
Return CType(f.Deserialize(stream), Account) ' [.NET 5] warning SYSLIB0011: 'Public Overloads Function Deserialize(serializationStream As Stream) As Object' は廃止されています: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.'。
|
Return CType(f.Deserialize(stream), Account)
|
|
End Using
|
End Using
|
|
End Function
|
End Function
|
|
End Class
|
End Class
|
|
818,11 |
793,11 |
|
using (MemoryStream stream = new MemoryStream()) {
|
using (MemoryStream stream = new MemoryStream()) {
|
|
BinaryFormatter f = new BinaryFormatter();
|
BinaryFormatter f = new BinaryFormatter();
|
|
|
|
~ |
f.Serialize(stream, source); // [.NET 5] warning SYSLIB0011: 'BinaryFormatter.Serialize(Stream, object)' は旧形式です ('BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.')
|
f.Serialize(stream, source);
|
|
|
|
|
stream.Position = 0L;
|
stream.Position = 0L;
|
|
|
|
~ |
return (T)f.Deserialize(stream); // [.NET 5] warning SYSLIB0011: 'BinaryFormatter.Deserialize(Stream)' は旧形式です ('BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.')
|
return (T)f.Deserialize(stream);
|
|
}
|
}
|
|
}
|
}
|
|
|
|
|
857,6 |
832,7 |
|
Console.WriteLine("{0}:{1} ({2})", a2.ID, a2.Name, string.Join(", ", a2.ContactAddresses));
|
Console.WriteLine("{0}:{1} ({2})", a2.ID, a2.Name, string.Join(", ", a2.ContactAddresses));
|
|
}
|
}
|
|
}
|
}
|
- |
|
|
|
}}
|
}}
|
|
$samplecode(lang=vb)
|
$samplecode(lang=vb)
|
|
#code{{
|
#code{{
|
|
881,11 |
857,11 |
|
Using stream As New MemoryStream()
|
Using stream As New MemoryStream()
|
|
Dim f As New BinaryFormatter()
|
Dim f As New BinaryFormatter()
|
|
|
|
~ |
f.Serialize(stream, source) ' [.NET 5] warning SYSLIB0011: 'Public Overloads Sub Serialize(serializationStream As Stream, graph As Object)' は廃止されています: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.'。
|
f.Serialize(stream, source)
|
|
|
|
|
stream.Position = 0L
|
stream.Position = 0L
|
|
|
|
~ |
Return CType(f.Deserialize(stream), T) ' [.NET 5] warning SYSLIB0011: 'Public Overloads Function Deserialize(serializationStream As Stream) As Object' は廃止されています: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.'。
|
Return CType(f.Deserialize(stream), T)
|
|
End Using
|
End Using
|
|
End Function
|
End Function
|
|
|
|