- What is the default implementation of the Match() method?
The default implementation of the Match() method calls the Equals() method. For our purpose we can override this method.
Example 1:
[Flags]
public enum AttributesValues : byte
{
First = 0,
Second = 2,
Third = 4,
Forth = 8,
Fifth = 16
}
public class AttributeWithOverridenMatchMethod : Attribute
{
public AttributesValues Values { set; get; }
public AttributeWithOverridenMatchMethod(AttributesValues attrValues)
{
Values = attrValues;
}
public override bool Match(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
return (this.Values & ((AttributeWithOverridenMatchMethod)obj).Values) == ((AttributeWithOverridenMatchMethod)obj).Values;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
return (this.Values == ((AttributeWithOverridenMatchMethod)obj).Values);
}
public override int GetHashCode()
{
return (int)this.Values;
}
}
public class Base{}
[AttributeWithOverridenMatchMethod(AttributesValues.First )]
public class Foo : Base{ }
[AttributeWithOverridenMatchMethod(AttributesValues.First | AttributesValues.Second)]
public class Bar : Base{ }
class Program
{
static void Main(string[] args)
{
Base foo = new Foo();
Base bar = new Bar();
AttributeWithOverridenMatchMethod []attr = {
new AttributeWithOverridenMatchMethod(AttributesValues.First),
new AttributeWithOverridenMatchMethod(AttributesValues.Second),
new AttributeWithOverridenMatchMethod(AttributesValues.First | AttributesValues.Second),
new AttributeWithOverridenMatchMethod(AttributesValues.Third)
};
foreach (var item in attr)
{
string attrVal = item.Values.ToString();
Trace.WriteLine("-=======================================-");
Trace.WriteLine("Class: foo; Attribute: " + attrVal + "; Contains: " + ContainsAttribute(foo, item));
Trace.WriteLine("-=======================================-");
Trace.WriteLine("Class: bar; Attribute: " + attrVal + "; Contains: " + ContainsAttribute(bar, item));
}
Console.ReadKey();
}
static bool ContainsAttribute(Base obj, AttributeWithOverridenMatchMethod comparingAttribute)
{
var attribute = Attribute.GetCustomAttribute(obj.GetType(), typeof(AttributeWithOverridenMatchMethod), false);
return attribute.Match(comparingAttribute);
}
}
Output:
-=======================================-
Class: foo; Attribute: First; Contains: True
-=======================================-
Class: bar; Attribute: First; Contains: True
-=======================================-
Class: foo; Attribute: Second; Contains: False
-=======================================-
Class: bar; Attribute: Second; Contains: True
-=======================================-
Class: foo; Attribute: Second; Contains: False
-=======================================-
Class: bar; Attribute: Second; Contains: True
-=======================================-
Class: foo; Attribute: Third; Contains: False
-=======================================-
Class: bar; Attribute: Third; Contains: False