It appears that when using the component model (or the default
implementation, at least), then any duplicated attributes (e.g. from
properties) are dropped. In my test, component-model outputs "abc" only, and
reflection ouotputs "def" and "abc" (in that order).
Is there any way to get duplicated attributes to present themselves in the
component model? Runnable demo code follows at foot.
In context, I was looking to see if I could hang some regex expressions off
properties for validity checking (by another component, and it doesn't
prevent setting). Sometimes a single property needs to be validated by a
number (>1) of regex expressions [or at least, it is far cleaner that way].
And yes, I could perhaps use a single params array, but I was also trying to
capture "must match" and "must not match" separately as an additional
attribute parameter.
*alternatively*, can anybody brute-force this pair of patterns (which I
think meet the spec.) into a single regex? The first should match, the
second should fail (exclusions):
//
http://www.govtalk.gov.uk/gdsc/html/...-1-Release.htm
//1. Must be 9 characters.
//2. First 2 characters must be alpha.
//3. Next 6 characters must be numeric.
//4. Final character can be A, B, C, D or space.
//5. First character must not be D,F,I,Q,U or V
//6. Second characters must not be D, F, I, O, Q, U or V.
//7. First 2 characters must not be combinations of GB, NK, TN or ZZ
(the term combinations covers both GB and BG etc.)
regex1 (should match): @"^[A-CEGHJ-PRSTW-Z][A-CEGHJ-NPR-TW-Z]\d{6}[ABCD]?$"
regex1 (shouldn't match): @"^(GB|BG|NK|KN|TN|NT|ZZ)"
Marc
// duplicated attribute demo
using System;
using System.ComponentModel;
class Program {
static void Main() {
TestClass obj = new TestClass();
Console.WriteLine("Component model");
foreach (Attribute attrib in
TypeDescriptor.GetProperties(obj)["TestProperty"].Attributes) {
Console.WriteLine(attrib.GetType().Name + ": " +
attrib.ToString());
}
Console.WriteLine();
Console.WriteLine("Reflection [no inherit]");
foreach (Attribute attrib in
obj.GetType().GetProperty("TestProperty").GetCusto mAttributes(false)) {
Console.WriteLine(attrib.GetType().Name + ": " +
attrib.ToString());
}
Console.WriteLine();
Console.WriteLine("Reflection [with inherit]");
foreach (Attribute attrib in
obj.GetType().GetProperty("TestProperty").GetCusto mAttributes(true)) {
Console.WriteLine(attrib.GetType().Name + ": " +
attrib.ToString());
}
}
}
[ImmutableObject(true), AttributeUsage(AttributeTargets.Property,
AllowMultiple = true)]
public class TestAttribute : Attribute {
private readonly string _value;
public string Value { get { return _value; } }
public override string ToString() {
return Value;
}
public TestAttribute(string value) {
_value = value;
}
}
public class TestClass {
[Test("abc")]
[Test("def")]
public string TestProperty { get { return "Fred"; } }
}