Feedback

C# - Equals

Veröffentlicht von am 12/31/2019
(0 Bewertungen)
Vergleicht das Objekt (Class) "this" mit einem anderen Objekt (Class). Das Snipped benutzt den Dateivergleich, speichert die Objekte aber nicht als Datei sondern in einem Memorystream.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
			<Title>Equals</Title>
			<Author>Klaus Ruttkowski</Author>
			<Description>Compares the object (class) "this" to another object (class).</Description>
			<HelpUrl>
      </HelpUrl>
			<Shortcut>
      </Shortcut>
		</Header>
		<Snippet>
			<Imports>
				<Import>
					<Namespace>System.Collections.Generic</Namespace>
				</Import>
				<Import>
					<Namespace>System.Text</Namespace>
				</Import>
				<Import>
					<Namespace>System.IO</Namespace>
				</Import>
				<Import>
					<Namespace>System.Xml.Serialization</Namespace>
				</Import>
				<Import>
					<Namespace>System.Xml</Namespace>
				</Import>
				<Import>
					<Namespace>System.Drawing</Namespace>
				</Import>
				<Import>
					<Namespace>System.Linq</Namespace>
				</Import>
			</Imports>
			<Code Language="csharp" Delimiter="$"><![CDATA[
#region Equals
// Calls
// this.Equals(ComparisonObject);
//
public bool Equals<T>(T value)
{
	int file1byte;
         int file2byte;

         MemoryStream memoryStreamX = new MemoryStream();
         MemoryStream memoryStreamY = new MemoryStream();

         var settings = new System.Xml.XmlWriterSettings { Encoding = Encoding.UTF8, CheckCharacters = false, NewLineHandling = NewLineHandling.None, };
         using (XmlWriter xmlWriter = XmlWriter.Create(memoryStreamX, settings))
         {

         	XmlSerializer serializer = new XmlSerializer(typeof(T));

                	serializer.Serialize(xmlWriter, this);
         }
         using (XmlWriter xmlWriter = XmlWriter.Create(memoryStreamY, settings))
         {

         	XmlSerializer serializer = new XmlSerializer(typeof(T));

                	serializer.Serialize(xmlWriter, value);
         }
         // Check the file sizes. If they are not the same, the files 
         // are not the same.
         if (memoryStreamX.Length != memoryStreamY.Length)
         {
         	// Return false to indicate files are different
                	return false;
         }

         // Read and compare a byte from each file until either a
         // non-matching set of bytes is found or until the end of
         // file1 is reached.
         do
         {
         	// Read one byte from each file.
         	file1byte = memoryStreamX.ReadByte();
                	file2byte = memoryStreamY.ReadByte();
         }
         while ((file1byte == file2byte) && (file1byte != -1));

         // Return the success of the comparison. "file1byte" is 
         // equal to "file2byte" at this point only if the files are 
         // the same.
         return ((file1byte - file2byte) == 0);
}
#endregion
]]></Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>
Abgelegt unter Equals, this.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!