static void Main18(string[] args) { //Test usage AttrCooperator ac = new AttrCooperator(); Console.WriteLine(ac.ToString()); //Test custom match AlwaysLogNumberAttribute alna = new AlwaysLogNumberAttribute(); alna.Number = 1000; AlwaysLogNumberAttribute alna2 = new AlwaysLogNumberAttribute(); alna2.Number = 999; Console.WriteLine("Do they match: " + alna2.Match(alna)); Console.WriteLine("Do they equal: " + alna2.Equals(alna)); #region CustomAttributeData -> Get attribute Data, without instancing one Console.WriteLine(Environment.NewLine); IList <System.Reflection.CustomAttributeData> attList = System.Reflection.CustomAttributeData.GetCustomAttributes(typeof(AttrCooperator)); foreach (System.Reflection.CustomAttributeData data in attList) { Console.WriteLine(data.Constructor.DeclaringType); Console.WriteLine(data.ConstructorArguments.Count); if (data.NamedArguments.Count > 0) { Console.WriteLine(data.NamedArguments[0].MemberName); } } #endregion }
//different with Euqual, I can define the rule of Match //in this case, I want this matches other, if this number is less than other number public override bool Match(object obj) { if (obj == null) { return(false); } if (obj.GetType() != this.GetType()) { return(false); } AlwaysLogNumberAttribute ala = (AlwaysLogNumberAttribute)obj; if (this.Number > ala.Number) { return(false); } return(true); }