Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Attribute.IsDefaultAttribute Method

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public virtual bool IsDefaultAttribute ()
public boolean IsDefaultAttribute ()
public function IsDefaultAttribute () : boolean

Return Value

true if this instance is the default attribute for the class; otherwise, false.

The default implementation of this class returns false, and must be implemented in the derived class to be useful to that class.

The implementation of this method in a derived class compares the value of this instance to a standard default value, then returns a Boolean value that indicates whether the value of this instance is equal to the standard value. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.

The following code example illustrates the use of IsDefaultAttribute.

using System;
using System.Reflection;

namespace DefAttrCS 
{
    // An enumeration of animals. Start at 1 (0 = uninitialized).
    public enum Animal 
    {
        // Pets.
        Dog = 1,
        Cat,
        Bird,
    }

    // A custom attribute to allow a target to have a pet.
    public class AnimalTypeAttribute : Attribute 
    {
        // The constructor is called when the attribute is set.
        public AnimalTypeAttribute(Animal pet) 
        {
            thePet = pet;
        }

        // Provide a default constructor and make Dog the default.
        public AnimalTypeAttribute() 
        {
            thePet = Animal.Dog;
        }

        // Keep a variable internally ...
        protected Animal thePet;

        // .. and show a copy to the outside world.
        public Animal Pet 
        {
            get { return thePet; }
            set { thePet = Pet; }
        }

        // Override IsDefaultAttribute to return the correct response.
        public override bool IsDefaultAttribute() 
        {
            if (thePet == Animal.Dog)
                return true;

            return false;
        }
    }

    public class TestClass 
    {
        // Use the default constructor.
        [AnimalType]
        public void Method1()
        {}
    }

    class DemoClass 
    {
        static void Main(string[] args) 
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get type information for the method.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the AnimalType attribute for the method.
            AnimalTypeAttribute atAttr = 
                (AnimalTypeAttribute)Attribute.GetCustomAttribute(mInfo,
                typeof(AnimalTypeAttribute));
            // Check to see if the default attribute is applied.
            Console.WriteLine("The attribute {0} for method {1} in class {2}",
                atAttr.Pet, mInfo.Name, clsType.Name); 
            Console.WriteLine("{0} the default for the AnimalType attribute.", 
                atAttr.IsDefaultAttribute() ? "is" : "is not");
        }
    }
}

package ISelectionServiceExample; 

import System.*;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.Windows.Forms.*;

/*  This sample demonstrates using the ISelectionService
    interface to receive notification of selection change events.  
    The SelectionComponent control attempts to retrieve an instance 
    of the ISelectionService when it is sited. If it can, it attaches 
    event handlers for events provided by the service that display
    a message when a component is selected or deselected.

    To run this sample, add the SelectionComponent control to a Form and
    then select or deselect components in design mode to see the behavior 
    of the component change event handlers. 
 */
public class SelectionComponent extends System.Windows.Forms.UserControl
{    
    private System.Windows.Forms.TextBox tbox1;
    private ISelectionService selectionService;

    public SelectionComponent()
    {
        // Initialize control
        this.SuspendLayout();
        this.set_Name("SelectionComponent");
        this.set_Size(new System.Drawing.Size(608, 296));
        this.tbox1 = new System.Windows.Forms.TextBox();
        this.tbox1.set_Location(new System.Drawing.Point(24, 16));
        this.tbox1.set_Name("listBox1");
        this.tbox1.set_Multiline(true);
        this.tbox1.set_Size(new System.Drawing.Size(560, 251));
        this.tbox1.set_TabIndex(0);
        this.get_Controls().Add(this.tbox1);
        this.ResumeLayout();
    } //SelectionComponent

    /** @property
     */
    public ISite get_Site()
    {
        return super.get_Site();
    } //get_Site

    /** @property 
     */
    public void set_Site(ISite value)
    {
        // The ISelectionService is available in design mode 
        // only, and only after the component is sited.
        if (selectionService != null) {
            // Because the selection service has been 
            // previously obtained, the component may be in 
            // the process of being resited. 
            // Detatch the previous selection change event 
            // handlers in case the new selection
            // service is a new service instance belonging to 
            // another design mode service host.
            selectionService.remove_SelectionChanged(
                new EventHandler(OnSelectionChanged));
            selectionService.remove_SelectionChanging(
                new EventHandler(OnSelectionChanging));
        }
        // Establish the new site for the component.
        super.set_Site(value);
        if (super.get_Site() == null) {
            return;
        }
        // The selection service is not available outside of 
        // design mode. A call requesting the service 
        // using GetService while not in design mode will 
        // return null.
        selectionService = ((ISelectionService)(this.get_Site().
            GetService(ISelectionService.class.ToType())));

        // If an instance of the ISelectionService was obtained, 
        // attach event handlers for the selection 
        // changing and selection changed events.
        if (selectionService != null) {
            // Add an event handler for the SelectionChanging 
            // and SelectionChanged events.
            selectionService.add_SelectionChanging(
                new EventHandler(OnSelectionChanging));
            selectionService.add_SelectionChanged(
                new EventHandler(OnSelectionChanged));
        }
    } //set_Site

    private void OnSelectionChanged(Object sender, EventArgs args)
    {
        tbox1.AppendText(("The selected component was changed.  "
            + "Selected components:\r\n    " 
            + GetSelectedComponents() + "\r\n"));
    } //OnSelectionChanged

    private void OnSelectionChanging(Object sender, EventArgs args)
    {
        tbox1.AppendText(("The selected component is changing. "
            + "Selected components:\r\n    " 
            + GetSelectedComponents() + "\r\n"));
    } //OnSelectionChanging

    private String GetSelectedComponents()
    {
        String selectedString = "";
        Object components[] = new Object[((ICollection)(selectionService.
            GetSelectedComponents())).get_Count()];
        ((ICollection)(selectionService.GetSelectedComponents())).
            CopyTo(components, 0);
        for (int i = 0; i < components.length; i++) {
            if (i != 0) {
                selectedString += "&& ";
            }
            if (((IComponent)(selectionService.get_PrimarySelection())). 
                    Equals((IComponent)(components.get_Item(i)))) {
                selectedString += "PrimarySelection:";
            }
            selectedString += ((IComponent)(components.get_Item(i))).
                get_Site().get_Name() + " ";
        }
        return selectedString;
    } //GetSelectedComponents

    // Clean up any resources being used.
    protected void Dispose(boolean disposing)
    {
        // Detatch the event handlers for the selection service.
        if (selectionService != null) {
            selectionService.remove_SelectionChanging(
                new EventHandler(this.OnSelectionChanging));
            selectionService.remove_SelectionChanged(
                new EventHandler(this.OnSelectionChanged));
        }
        super.Dispose(disposing);
    } //Dispose
} //SelectionComponent

import System;
import System.Reflection;

package DefAttrJS {
    // An enumeration of animals. Start at 1 (0 = uninitialized).
    public enum Animal {
	    // Pets.
	    Dog = 1,
	    Cat,
	    Bird,
    }

    // A custom attribute to allow a target to have a pet.
    AttributeUsage(AttributeTargets.Method) public class AnimalTypeAttribute extends Attribute {
        // The constructor is called when the attribute is set.
        public function AnimalTypeAttribute(pet : Animal) {
	        thePet = pet;
        }

        // Provide a default constructor and make Dog the default.
        public function AnimalTypeAttribute() {
	        thePet = Animal.Dog;
        }

        // Keep a variable internally ...
        protected var thePet : Animal;

        // .. and show a copy to the outside world.
        public function get Pet() : Animal {
            return thePet; 		
        }

        public function set Pet(value : Animal) {		
            thePet = value;
        }

        // Override IsDefaultAttribute to return the correct response.
        public override function IsDefaultAttribute() : boolean {
            return thePet == Animal.Dog;
        }
    }

    public class TestClass {
        // Use the default constructor.
        AnimalType public function Method1() : void
        {}
    }

    class DemoClass {
        static function Main() : void  {
            // Get the class type to access its metadata.
            var clsType : Type  = TestClass;
            // Get type information for the method.
            var mInfo : MethodInfo = clsType.GetMethod("Method1");
            // Get the AnimalType attribute for the method.
            var atAttr : AnimalTypeAttribute = 
	            AnimalTypeAttribute(Attribute.GetCustomAttribute(mInfo, AnimalTypeAttribute));
            // Check to see if the default attribute is applied.
            Console.WriteLine("The attribute {0} for method {1} in class {2}",
	            atAttr.Pet, mInfo.Name, clsType.Name); 
            Console.WriteLine("{0} the default for the AnimalType attribute.", 
	            atAttr.IsDefaultAttribute() ? "is" : "is not");
        }
    }
}

DefAttrJS.DemoClass.Main();

/*
 * Output:
 * The attribute Dog for method Method1 in class TestClass
 * is the default for the AnimalType attribute.
 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft