Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
Übersetzung
Original
Dieser Artikel wurde noch nicht bewertet - Dieses Thema bewerten.

Attribute.Match-Methode

Beim Überschreiben in einer abgeleiteten Klasse wird ein Wert zurückgegeben, der angibt, ob diese Instanz einem bestimmten Objekt entspricht.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public virtual bool Match(
	Object obj
)

Parameter

obj
Typ: System.Object
Ein Object, das mit dieser Instanz von Attribute verglichen werden soll.

Rückgabewert

Typ: System.Boolean
true , wenn diese Instanz obj gleicht; andernfalls false.

Diese Methode ermittelt, ob ein Attribute einem anderen gleicht. Ihre Standardimplementierung ist identisch mit Equals, die einen Wert- und Verweisvergleich vornimmt. Diese Methode wird überschrieben, um Unterstützung für Attributwerte (z. B. Flags oder Bitfelder) zu implementieren, die aus Komponenten bestehen, die für sich selbst eine Bedeutung haben.

Angenommen, der Wert eines Attributs ist ein binäres Feld, das in ein Bitfeld aus Flags aufgeteilt ist. In zwei Instanzen dieses Attributs stimmt ein Flag in der Menge überein, während sich alle anderen Flags unterscheiden. Die Equals-Methode kann nicht ermitteln, ob die beiden Instanzen das gleiche Flag festgelegt haben, wohingegen die Match-Methode dies kann.

Das folgende Codebeispiel veranschaulicht die Verwendung der Match-Methode im Kontext von Attribute.


using System;
using System.Reflection;

namespace MatchCS {
	// A custom attribute to allow 2 authors per method.
	public class AuthorsAttribute : Attribute {
		public AuthorsAttribute(string name1, string name2) {
			authorName1 = name1;
			authorName2 = name2;
		}

		protected string authorName1;
		protected string authorName2;
	
		public string AuthorName1 {
			get { return authorName1; }
			set { authorName1 = AuthorName1; }
		}

		public string AuthorName2 {
			get { return authorName2; }
			set { authorName2 = AuthorName2; }
		}

		// Use the hash code of the string objects and xor them together.
		public override int GetHashCode() {
			return authorName1.GetHashCode() ^ authorName2.GetHashCode();
		}

		// Determine if the object is a match to this one.
		public override bool Match(object obj) {
			// Obviously a match.
			if (obj == this)
				return true;

			// Obviously we're not null, so no.
			if (obj == null)
				return false;

			if (obj is AuthorsAttribute)
				// Combine the hash codes and see if they're unchanged.
				return (((AuthorsAttribute)obj).GetHashCode() & GetHashCode())
					== GetHashCode();
			else
				return false;
		}
	}

	// Add some authors to methods of a class.
	public class TestClass1 {
		[Authors("William Shakespeare", "Herman Melville")]
		public void Method1()
		{}

		[Authors("Leo Tolstoy", "John Milton")]
		public void Method2()
		{}
	}

	// Add authors to a second class's methods.
	public class TestClass2 {
		[Authors("William Shakespeare", "Herman Melville")]
		public void Method1()
		{}

		[Authors("Leo Tolstoy", "John Milton")]
		public void Method2()
		{}

		[Authors("William Shakespeare", "John Milton")]
		public void Method3()
		{}
	}

	class DemoClass {
		static void Main(string[] args) {
			// Get the type for both classes to access their metadata.
			Type clsType1 = typeof(TestClass1);
			Type clsType2 = typeof(TestClass2);

			// Iterate through each method of the first class.
			foreach(MethodInfo mInfo1 in clsType1.GetMethods()) {
				// Check each method for the Authors attribute.
				AuthorsAttribute authAttr1 = (AuthorsAttribute)
					Attribute.GetCustomAttribute(mInfo1, 
					typeof(AuthorsAttribute));
				if (authAttr1 != null) {
					// Display the authors.
					Console.WriteLine("Method {0} was authored by {1} " +
										"and {2}.", mInfo1.Name, 
										authAttr1.AuthorName1, 
										authAttr1.AuthorName2);
					// Iterate through each method of the second class.
					foreach(MethodInfo mInfo2 in clsType2.GetMethods()) {
						// Check each method for the Authors attribute.
						AuthorsAttribute authAttr2 = (AuthorsAttribute)
							Attribute.GetCustomAttribute(mInfo2, 
							typeof(AuthorsAttribute));
						// Compare with the authors in the first class.
						if (authAttr2 != null && authAttr2.Match(authAttr1))
							Console.WriteLine("Method {0} in class {1} " +
								"was authored by the same team.",
								mInfo2.Name, clsType2.Name);
					}
					Console.WriteLine("");
				}
			}
		}
	}
}

/*
 * Output:
 * Method Method1 was authored by William Shakespeare and Herman Melville.
 * Method Method1 in class TestClass2 was authored by the same team.
 *
 * Method Method2 was authored by Leo Tolstoy and John Milton.
 * Method Method2 in class TestClass2 was authored by the same team.
 */


.NET Framework

Unterstützt in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Unterstützt in: 4, 3.5 SP1

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core-Rolle wird nicht unterstützt), Windows Server 2008 R2 (Server Core-Rolle wird mit SP1 oder höher unterstützt; Itanium wird nicht unterstützt)

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)

Community-Beiträge

HINZUFÜGEN
Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?
© 2013 Microsoft. Alle Rechte vorbehalten.