Find a single Attribute of the type 'attributeType' from the supplied class : Attribute « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » AttributeScreenshots 
Find a single Attribute of the type 'attributeType' from the supplied class
 

/*
 * Copyright  2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;

namespace Spring.Util
{
    /// <summary>
    /// General utility methods for working with annotations
    /// </summary>
    public class AttributeUtils
    {
        /// <summary>
        /// Find a single Attribute of the type 'attributeType' from the supplied class, 
        /// traversing it interfaces and super classes if no attribute can be found on the 
        /// class iteslf. 
        /// </summary>
        /// <remarks>
        /// This method explicitly handles class-level attributes which are not declared as
        /// inherited as well as attributes on interfaces.
        /// </remarks>
        /// <param name="type">The class to look for attributes on .</param>
        /// <param name="attributeType">Type of the attribibute to look for.</param>
        /// <returns>the attribute of the given type found, or <code>null</code></returns>
        public static Attribute FindAttribute(Type type, Type attributeType)
        {
            Attribute[] attributes = Attribute.GetCustomAttributes(type, attributeType, false);  // we will traverse hierarchy ourselves.
            if (attributes.Length > 0)
            {
                return attributes[0];
            }
            foreach (Type interfaceType in type.GetInterfaces())
            {
                Attribute attrib = FindAttribute(interfaceType, attributeType);
                if (attrib != null)
                {
                    return attrib;
                }
            }
            if (type.BaseType == null)
            {
                return null;
            }
            return FindAttribute(type.BaseType, attributeType);
        }
    }
}

   
  
Related examples in the same category
1.Set Attribute Value
2.Get Attribute Value
3.Get Int value from xml attribute
4.Get Attribute value
5.Get attribute or return default value
6.Get Bool Attribute Or Throw exception
7.Get attribute value and convert to integer type
8.Get attribute value and convert to integer type or throw exception
9.Get Attribute and return Int64
10.Get Attribute and return Int64 or throw exception
11.Adds an XmlAttribute to a XmlNode
12.Get boolean value if an attribute is 1 or true
13.Attempts to get and cast attribute from an XmlElement
14.Attempts to get and convert an attribute from an XmlElement
15.Gets a string from an attribute or node.
16.Remove all the xml namespaces (xmlns) attributes in the xml string
17.Get Attribute With Conversion
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.