[Mono-list] Re: Lamespec for System.Attribute?

Richard Creed creed@dircon.co.uk
Sat, 11 Aug 2001 21:57:07 +0100


Nick

I've been lurking on Mono for a week - just struck me as such a good idea
(although I have to admit to being a VB & VC++ programmer working entirely on MS
Windows platforms :^)

However I'm an MSDN subscriber, get all the .NET stuff, and although I've been
too busy to explore very much I'd like to help in Mono whenever I can be useful.
And coming from an MS programming mindset I may be of some use. So ...

I'm sure you're right. "element is not type attributeType" is not right.

The .NET Beta Documentation says:

Return Value
A null reference (Nothing in Visual Basic), if no custom attribute of type
attributeType is applied to element.
-or-
An Attribute reference to the single custom attribute of type attributeType that
is applied to element.
-or-
AmbiguousMatchException is thrown if more than one custom attribute of type
attributeType is applied to element.

Exceptions
ArgumentExceptionNull  -
    element or attributeType is a null reference (Nothing in Visual Basic)"
ArgumentException -
    attributeType is not derived from Attribute.
    -or-
    element is not type attributeType.
AmbiguousMatchException
    More than one of the requested attributes was found.

What it seems to be trying to achieve is:

1. preliminary check that element is not null (throw ArgumentExceptionNull?)
2. preliminary check that attributeType is a valid type (throw
ArgumentException?)
3. ....
object[] atts;
// get an array of all custom attributes of attributeType
atts = element.GetCustomAttributes(attributeType, ignoredBool));
// check atts is not an empty array
// if it is return a null reference
    //Documentation says: return "A null reference (Nothing in Visual Basic),
    //if no custom attribute of type attributeType is applied to element."
// else
if (atts.Count != 0)
{
    if (atts.Count == 1)
    {
        //We have what we want for GetCustomAttribute
        // so return 1st element
        //Documentation says : return "An Attribute reference to the single
        //custom attribute of type attributeType that is applied to element"
     }
    else
    {
         //more than one custom attribute of the given type
         //  - throw AmbiguousMatchException
    }
}
else
{
    //Documentation: return "A null reference (Nothing in Visual Basic),
    //if no custom attribute of type attributeType is applied to element."
}

Implementing GetCustomAttributes would be a precondition if you do it this way,
but I'm trying to probe the meaning of the method & the validity of the
documentation rather than anything else.

Does that concur?

Richard Creed