Component model and attributes with AllowMultiple=true

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Gravell

    Component model and attributes with AllowMultiple=true

    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):

    //

    //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.Componen tModel;

    class Program {
    static void Main() {
    TestClass obj = new TestClass();
    Console.WriteLi ne("Component model");
    foreach (Attribute attrib in
    TypeDescriptor. GetProperties(o bj)["TestProper ty"].Attributes) {
    Console.WriteLi ne(attrib.GetTy pe().Name + ": " +
    attrib.ToString ());
    }
    Console.WriteLi ne();
    Console.WriteLi ne("Reflection [no inherit]");
    foreach (Attribute attrib in
    obj.GetType().G etProperty("Tes tProperty").Get CustomAttribute s(false)) {
    Console.WriteLi ne(attrib.GetTy pe().Name + ": " +
    attrib.ToString ());
    }
    Console.WriteLi ne();
    Console.WriteLi ne("Reflection [with inherit]");
    foreach (Attribute attrib in
    obj.GetType().G etProperty("Tes tProperty").Get CustomAttribute s(true)) {
    Console.WriteLi ne(attrib.GetTy pe().Name + ": " +
    attrib.ToString ());
    }
    }
    }
    [ImmutableObject (true), AttributeUsage( AttributeTarget s.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(s tring value) {
    _value = value;
    }
    }
    public class TestClass {
    [Test("abc")]
    [Test("def")]
    public string TestProperty { get { return "Fred"; } }
    }


  • Greg Bacon

    #2
    Re: Component model and attributes with AllowMultiple=t rue

    In article <#p#iZSmAHHA.46 72@TK2MSFTNGP02 .phx.gbl>,
    Marc Gravell <marc.gravell@g mail.comwrote:

    : [...]
    :
    : http://www.govtalk.gov.uk/gdsc/html/noframes
    : /NationalInsuran ceNumber-2-1-Release.htm

    That page links to a schema[*] that provides the following pattern:

    [A-CEGHJ-NOPR-TW-Z]{2}[0-9]{6}[ABCD\s]{1}
    [*] http://www.govtalk.gov.uk/gdsc/schemas
    /CitizenIdentifi cationTypes-v1-4.xsd

    Yes, the trailing {1} is redundant, but it looks like a safe bet. :-)

    Greg
    --
    Patriotism cannot be a love of government.
    -- Robert LeFevre

    Comment

    • Marc Gravell

      #3
      Re: Component model and attributes with AllowMultiple=t rue

      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


      Comment

      Working...