static void Main() { // Get the class type to access its metadata. Type clsType = typeof(TestClass); // Store author information in a list of tuples. var authorsInfo = new List <Tuple <String, AuthorsAttribute> >(); // Iterate through all the methods of the class. foreach (var method in clsType.GetMethods()) { // Get the Authors attribute for the method if it exists. AuthorsAttribute authAttr = (AuthorsAttribute)Attribute.GetCustomAttribute( method, typeof(AuthorsAttribute)); if (authAttr != null) { // Add the information to the author list. authorsInfo.Add(Tuple.Create(clsType.Name + "." + method.Name, authAttr)); } } // Iterate through the list bool[] listed = new bool[authorsInfo.Count]; Console.WriteLine("Method authors:\n"); for (int ctr = 0; ctr < authorsInfo.Count; ctr++) { var authorInfo = authorsInfo[ctr]; if (!listed[ctr]) { Console.WriteLine("{0} and {1}", authorInfo.Item2.AuthorName1, authorInfo.Item2.AuthorName2); listed[ctr] = true; Console.WriteLine(" {0}", authorInfo.Item1); for (int ctr2 = ctr + 1; ctr2 < authorsInfo.Count; ctr2++) { if (!listed[ctr2]) { if (authorInfo.Item2.Equals(authorsInfo[ctr2].Item2)) { Console.WriteLine(" {0}", authorsInfo[ctr2].Item1); listed[ctr2] = true; } } } } } }
public override bool Equals(Object obj) { AuthorsAttribute auth = obj as AuthorsAttribute; if (auth == null) { return(false); } return((_authorName1 == auth.AuthorName1 & _authorName2 == auth.AuthorName2) | (_authorName1 == auth.AuthorName2 & _authorName2 == auth.AuthorName1)); }
// Determine if the object is a match to this one. public override bool Match(object obj) { // Return false if obj is null or not an AuthorsAttribute. AuthorsAttribute authors2 = obj as AuthorsAttribute; if (authors2 == null) { return(false); } // Return true if obj and this instance are the same object reference. if (Object.ReferenceEquals(this, authors2)) { return(true); } // Return false if obj and this instance have different numbers of authors if (_authors.Count != authors2._authors.Count) { return(false); } bool matches = false; foreach (var author in _authors) { for (int ctr = 0; ctr < authors2._authors.Count; ctr++) { if (author == authors2._authors[ctr]) { matches = true; break; } if (ctr == authors2._authors.Count) { matches = false; } } } return(matches); }
// 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); } AuthorsAttribute authObj = obj as AuthorsAttribute; if (authObj != null) { // Check for identical order. if ((_authorName1 == authObj._authorName1) & (_authorName2 == authObj._authorName2)) { return(true); } // Check for reversed order. else if ((_authorName1 == authObj._authorName2) & (_authorName2 == authObj._authorName1)) { return(true); } else { return(false); } } else { return(false); } }
static void Main() { // Get the type for TestClass to access its metadata. Type clsType = typeof(TestClass); // Iterate through each method of the class. AuthorsAttribute authors = null; foreach (var method in clsType.GetMethods()) { // Check each method for the Authors attribute. AuthorsAttribute authAttr = (AuthorsAttribute)Attribute.GetCustomAttribute(method, typeof(AuthorsAttribute)); if (authAttr != null) { // Display the authors. Console.WriteLine($"{clsType.Name}.{method.Name} was authored by {authAttr}."); // Select Method1's authors as the basis for comparison. if (method.Name == "Method1") { authors = authAttr; Console.WriteLine(); continue; } // Compare first authors with the authors of this method. if (authors.Match(authAttr)) { Console.WriteLine("TestClass.Method1 was also authored by the same team."); } // Perform an equality comparison of the two attributes. Console.WriteLine($"{authors} {(authors.Equals(authAttr) ? "=" : "<>")} {authAttr}"); Console.WriteLine(); } } }
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 (var method in clsType1.GetMethods()) { // Check each method for the Authors attribute. AuthorsAttribute authAttr1 = (AuthorsAttribute) Attribute.GetCustomAttribute(method, typeof(AuthorsAttribute)); if (authAttr1 != null) { // Display the authors. Console.WriteLine("{0}.{1} was authored by {2} and {3}.", clsType1.Name, method.Name, authAttr1.AuthorName1, authAttr1.AuthorName2); // Iterate through each method of the second class. foreach (var method2 in clsType2.GetMethods()) { // Check each method for the Authors attribute. AuthorsAttribute authAttr2 = (AuthorsAttribute) Attribute.GetCustomAttribute(method2, typeof(AuthorsAttribute)); // Compare with the authors in the first class. if (authAttr2 != null && authAttr2.Match(authAttr1)) { Console.WriteLine("{0}.{1} was also authored by the same team.", clsType2.Name, method2.Name); } } Console.WriteLine(); } } }