BinaryFormatter ignore assembly version
I have the following method to generate a hash of an object. It works pretty good! But when I change the version of the assembly, the hash is changing even when the object is the same.
public static string GetHash(Object item)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, item);
binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
memoryStream.Seek(0, SeekOrigin.Begin);
return Convert.ToBase64String(hashAlgorithm.ComputeHash(memoryStream));
}
How is it possible to ignore the assembly version?
c# serialization .net-assembly binaryformatter
add a comment |
I have the following method to generate a hash of an object. It works pretty good! But when I change the version of the assembly, the hash is changing even when the object is the same.
public static string GetHash(Object item)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, item);
binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
memoryStream.Seek(0, SeekOrigin.Begin);
return Convert.ToBase64String(hashAlgorithm.ComputeHash(memoryStream));
}
How is it possible to ignore the assembly version?
c# serialization .net-assembly binaryformatter
1
The object is not the same. A type in .NET isn't just the namespace and type name, it also includes the assembly it came from. Which includes the [AssemblyVersion]. A strong DLL-Hell counter-measure. And a strong check that prevents deserializing data that no longer matches a changed class. Your hash calculation just adds another check that the data has changed, possibly unnecessarily. No, you can't ignore the assembly version.
– Hans Passant
Oct 10 '13 at 8:59
add a comment |
I have the following method to generate a hash of an object. It works pretty good! But when I change the version of the assembly, the hash is changing even when the object is the same.
public static string GetHash(Object item)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, item);
binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
memoryStream.Seek(0, SeekOrigin.Begin);
return Convert.ToBase64String(hashAlgorithm.ComputeHash(memoryStream));
}
How is it possible to ignore the assembly version?
c# serialization .net-assembly binaryformatter
I have the following method to generate a hash of an object. It works pretty good! But when I change the version of the assembly, the hash is changing even when the object is the same.
public static string GetHash(Object item)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, item);
binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
memoryStream.Seek(0, SeekOrigin.Begin);
return Convert.ToBase64String(hashAlgorithm.ComputeHash(memoryStream));
}
How is it possible to ignore the assembly version?
c# serialization .net-assembly binaryformatter
c# serialization .net-assembly binaryformatter
edited Oct 10 '13 at 8:30
Jens Björnhager
5,19332142
5,19332142
asked Oct 10 '13 at 7:45
DavidDavid
2,09453887
2,09453887
1
The object is not the same. A type in .NET isn't just the namespace and type name, it also includes the assembly it came from. Which includes the [AssemblyVersion]. A strong DLL-Hell counter-measure. And a strong check that prevents deserializing data that no longer matches a changed class. Your hash calculation just adds another check that the data has changed, possibly unnecessarily. No, you can't ignore the assembly version.
– Hans Passant
Oct 10 '13 at 8:59
add a comment |
1
The object is not the same. A type in .NET isn't just the namespace and type name, it also includes the assembly it came from. Which includes the [AssemblyVersion]. A strong DLL-Hell counter-measure. And a strong check that prevents deserializing data that no longer matches a changed class. Your hash calculation just adds another check that the data has changed, possibly unnecessarily. No, you can't ignore the assembly version.
– Hans Passant
Oct 10 '13 at 8:59
1
1
The object is not the same. A type in .NET isn't just the namespace and type name, it also includes the assembly it came from. Which includes the [AssemblyVersion]. A strong DLL-Hell counter-measure. And a strong check that prevents deserializing data that no longer matches a changed class. Your hash calculation just adds another check that the data has changed, possibly unnecessarily. No, you can't ignore the assembly version.
– Hans Passant
Oct 10 '13 at 8:59
The object is not the same. A type in .NET isn't just the namespace and type name, it also includes the assembly it came from. Which includes the [AssemblyVersion]. A strong DLL-Hell counter-measure. And a strong check that prevents deserializing data that no longer matches a changed class. Your hash calculation just adds another check that the data has changed, possibly unnecessarily. No, you can't ignore the assembly version.
– Hans Passant
Oct 10 '13 at 8:59
add a comment |
2 Answers
2
active
oldest
votes
But when I change the version of the assembly, the hash is changing even when the object is the same.
yes, that is expected behaviour when using BinaryFormatter... it does not guarantee to create the same output - and especially since it includes full type information (including version) it is pretty much guaranteed to change between versions.
I would consider using a serializer that doesn't include type information; XmlSerializer, Json.NET or protobuf-net would leap to mind.
add a comment |
BinaryFormatter.AssemblyFormat is documented as:
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
There's no indication that it has an impact on the serializing path.
Personally I would avoid this method of hashing - it seems terribly fragile to me. Do you have no control over the object being hashed, or any way of hashing in a more stable way?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19289898%2fbinaryformatter-ignore-assembly-version%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
But when I change the version of the assembly, the hash is changing even when the object is the same.
yes, that is expected behaviour when using BinaryFormatter... it does not guarantee to create the same output - and especially since it includes full type information (including version) it is pretty much guaranteed to change between versions.
I would consider using a serializer that doesn't include type information; XmlSerializer, Json.NET or protobuf-net would leap to mind.
add a comment |
But when I change the version of the assembly, the hash is changing even when the object is the same.
yes, that is expected behaviour when using BinaryFormatter... it does not guarantee to create the same output - and especially since it includes full type information (including version) it is pretty much guaranteed to change between versions.
I would consider using a serializer that doesn't include type information; XmlSerializer, Json.NET or protobuf-net would leap to mind.
add a comment |
But when I change the version of the assembly, the hash is changing even when the object is the same.
yes, that is expected behaviour when using BinaryFormatter... it does not guarantee to create the same output - and especially since it includes full type information (including version) it is pretty much guaranteed to change between versions.
I would consider using a serializer that doesn't include type information; XmlSerializer, Json.NET or protobuf-net would leap to mind.
But when I change the version of the assembly, the hash is changing even when the object is the same.
yes, that is expected behaviour when using BinaryFormatter... it does not guarantee to create the same output - and especially since it includes full type information (including version) it is pretty much guaranteed to change between versions.
I would consider using a serializer that doesn't include type information; XmlSerializer, Json.NET or protobuf-net would leap to mind.
answered Oct 10 '13 at 8:47
Marc Gravell♦Marc Gravell
790k19521542558
790k19521542558
add a comment |
add a comment |
BinaryFormatter.AssemblyFormat is documented as:
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
There's no indication that it has an impact on the serializing path.
Personally I would avoid this method of hashing - it seems terribly fragile to me. Do you have no control over the object being hashed, or any way of hashing in a more stable way?
add a comment |
BinaryFormatter.AssemblyFormat is documented as:
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
There's no indication that it has an impact on the serializing path.
Personally I would avoid this method of hashing - it seems terribly fragile to me. Do you have no control over the object being hashed, or any way of hashing in a more stable way?
add a comment |
BinaryFormatter.AssemblyFormat is documented as:
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
There's no indication that it has an impact on the serializing path.
Personally I would avoid this method of hashing - it seems terribly fragile to me. Do you have no control over the object being hashed, or any way of hashing in a more stable way?
BinaryFormatter.AssemblyFormat is documented as:
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
There's no indication that it has an impact on the serializing path.
Personally I would avoid this method of hashing - it seems terribly fragile to me. Do you have no control over the object being hashed, or any way of hashing in a more stable way?
answered Oct 10 '13 at 7:49
Jon SkeetJon Skeet
1092k69279728459
1092k69279728459
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19289898%2fbinaryformatter-ignore-assembly-version%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The object is not the same. A type in .NET isn't just the namespace and type name, it also includes the assembly it came from. Which includes the [AssemblyVersion]. A strong DLL-Hell counter-measure. And a strong check that prevents deserializing data that no longer matches a changed class. Your hash calculation just adds another check that the data has changed, possibly unnecessarily. No, you can't ignore the assembly version.
– Hans Passant
Oct 10 '13 at 8:59