Component model and attributes with AllowMultiple=true

M

Marc Gravell

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/noframes/NationalInsuranceNumber-2-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").GetCustomAttributes(false)) {
Console.WriteLine(attrib.GetType().Name + ": " +
attrib.ToString());
}
Console.WriteLine();
Console.WriteLine("Reflection [with inherit]");
foreach (Attribute attrib in
obj.GetType().GetProperty("TestProperty").GetCustomAttributes(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"; } }
}
 
M

Marc Gravell

Thanks anyway, but it doesn't fully meet the spec as stated, and is very
close to one of the pair of regex that I already posted.

I guess they couldn't brute-force it into a single regex either! Oh well, it
isn't always possible...

Of course, if somebody could explain the component-model / reflection
disparity that would be good too ;-p

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top