Esporta (0) Stampa
Espandi tutto

Metodo Attribute.IsDefaultAttribute

Aggiornamento: novembre 2007

Quando è sottoposto a override in una classe derivata, indica se il valore di questa istanza è il valore predefinito della classe derivata.

Spazio dei nomi:  System
Assembly:  mscorlib (in mscorlib.dll)

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

Valore restituito

Tipo: System.Boolean
true se questa istanza è l'attributo predefinito della classe; in caso contrario, false.

L'implementazione predefinita di questa classe restituisce false; per essere utile alla classe, deve essere implementata nella classe derivata.

L'implementazione di questo metodo in una classe derivata confronta il valore di questa istanza con un valore predefinito standard, quindi restituisce un valore Boolean che indica se il valore di questa istanza è uguale al valore standard. Il valore standard è generalmente codificato come costante nell'implementazione oppure è memorizzato a livello di codice in un campo utilizzato dall'implementazione.

Nell'esempio di codice riportato di seguito viene illustrato l'utilizzo del metodo 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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.

.NET Framework

Supportato in: 3.5, 3.0, 2.0, 1.1, 1.0

Aggiunte alla community

AGGIUNGI
Microsoft sta conducendo un sondaggio in linea per comprendere l'opinione degli utenti in merito al sito Web di MSDN. Se si sceglie di partecipare, quando si lascia il sito Web di MSDN verrà visualizzato il sondaggio in linea.

Si desidera partecipare?
Mostra:
© 2014 Microsoft