AttributeTableBuilder.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Metadata / AttributeTableBuilder.cs / 1305376 / AttributeTableBuilder.cs

                            //---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------

namespace System.Activities.Presentation.Metadata 
{
 
    using System.Activities.Presentation.Internal.Metadata; 
    using System;
    using System.Collections; 
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Reflection;
    using System.Windows; 
    using System.Activities.Presentation;
 
    //  
    // An attribute table is a read only blob of data.  How
    // do you create one?  We will have a class called an 
    // Attribute Builder that can be used to create an attribute
    // table.  Attribute builders have methods you can call to
    // add metadata.  When you�re finished, you can produce an
    // attribute table from the builder.  Builder methods also 
    // support callback delegates so the entire process can be
    // deferred until needed. 
    //  
    public class AttributeTableBuilder
    { 

        private MutableAttributeTable _table = new MutableAttributeTable();
        private bool _cloneOnUse;
 
        //
        // Returns an attribute table we can make changes to 
        // 
        private MutableAttributeTable MutableTable
        { 
            get {
                if (_cloneOnUse)
                {
                    MutableAttributeTable clone = new MutableAttributeTable(); 
                    clone.AddTable(_table);
                    _table = clone; 
                    _cloneOnUse = false; 
                }
 
                return _table;
            }
        }
 
        // 
        // Adds a callback that will be invoked when metadata for the 
        // given type is needed.  The callback can add metadata to 
        // to the attribute table on demand, which is much more efficient
        // than adding metadata up front. 
        // 
        // 
        // 
        public void AddCallback(Type type, AttributeCallback callback) 
        {
            if (type == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("type");
            } 
            if (callback == null)
            {
                throw FxTrace.Exception.ArgumentNull("callback");
            } 
            MutableTable.AddCallback(type, callback);
        } 
 
        // 
        // Adds the contents of the provided attributes to this builder. 
        // Conflicts are resolved with a last-in-wins strategy.  When
        // building a large attribute table it is best to use AddCallback
        // to defer the work of creating attributes until they are needed.
        //  
        // The type to add class-level attributes to.
        //  
        // The new attributes to add. 
        // 
        // if type or attributes is null 
        public void AddCustomAttributes(Type type, params Attribute[] attributes) {
            if (type == null)
            {
                throw FxTrace.Exception.ArgumentNull("type"); 
            }
            if (attributes == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("attributes");
            } 
            MutableTable.AddCustomAttributes(type, attributes);
        }

        //  
        // Adds the contents of the provided attributes to this builder.
        // Conflicts are resolved with a last-in-wins strategy.  When 
        // building a large attribute table it is best to use AddCallback 
        // to defer the work of creating attributes until they are needed.
        //  
        // 
        // The type the member lives on.
        // 
        // An event or property descriptor to add attributes to. 
        // 
        // The new attributes to add. 
        //  
        // if descriptor or attributes is null
        public void AddCustomAttributes(Type ownerType, MemberDescriptor descriptor, params Attribute[] attributes) { 
            if (ownerType == null)
            {
                throw FxTrace.Exception.ArgumentNull("ownerType");
            } 
            if (descriptor == null)
            { 
                throw FxTrace.Exception.ArgumentNull("descriptor"); 
            }
            if (attributes == null) 
            {
                throw FxTrace.Exception.ArgumentNull("attributes");
            }
            MutableTable.AddCustomAttributes(ownerType, descriptor, attributes); 
        }
 
        //  
        // Adds the contents of the provided attributes to this builder.
        // Conflicts are resolved with a last-in-wins strategy.  When 
        // building a large attribute table it is best to use AddCallback
        // to defer the work of creating attributes until they are needed.
        // 
        //  
        // The type the member lives on.
        //  
        // An event or property info to add attributes to. 
        // 
        // The new attributes to add. 
        // 
        // if member or attributes is null
        public void AddCustomAttributes(Type ownerType, MemberInfo member, params Attribute[] attributes) {
            if (ownerType == null) 
            {
                throw FxTrace.Exception.ArgumentNull("ownerType"); 
            } 
            if (member == null)
            { 
                throw FxTrace.Exception.ArgumentNull("member");
            }
            if (attributes == null)
            { 
                throw FxTrace.Exception.ArgumentNull("attributes");
            } 
            MutableTable.AddCustomAttributes(ownerType, member, attributes); 
        }
 
        // 
        // Adds attributes to the member with the given name.  The member can be a property
        // or an event.  The member is evaluated on demand when the user queries
        // attributes on a given property or event. 
        // 
        //  
        // The type the member lives on. 
        // 
        //  
        // The member to add attributes for.  Only property and event members are supported;
        // all others will be ignored.
        // 
        //  
        // The new attributes to add.
        //  
        public void AddCustomAttributes(Type ownerType, string memberName, params Attribute[] attributes) { 
            if (ownerType == null)
            { 
                throw FxTrace.Exception.ArgumentNull("ownerType");
            }
            if (memberName == null)
            { 
                throw FxTrace.Exception.ArgumentNull("memberName");
            } 
            MutableTable.AddCustomAttributes(ownerType, memberName, attributes); 
        }
 
        // 
        // Adds the contents of the provided attributes to this builder.
        // Conflicts are resolved with a last-in-wins strategy.  When
        // building a large attribute table it is best to use AddCallback 
        // to defer the work of creating attributes until they are needed.
        //  
        //  
        // The type that owns the dependency property.
        //  
        // A dependency property to add attributes to.
        // 
        // The new attributes to add.
        //  
        // if dp, ownerType or attributes is null
        public void AddCustomAttributes(Type ownerType, DependencyProperty dp, params Attribute[] attributes) { 
            if (ownerType == null) 
            {
                throw FxTrace.Exception.ArgumentNull("ownerType"); 
            }
            if (dp == null)
            {
                throw FxTrace.Exception.ArgumentNull("dp"); 
            }
            if (attributes == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("attributes");
            } 
            MutableTable.AddCustomAttributes(ownerType, dp, attributes);
        }

        //  
        // Adds the contents of the provided attribute table to
        // this builder.  Conflicts are resolved with a last-in-wins 
        // strategy. 
        // 
        // An existing attribute table. 
        // if table is null
        public void AddTable(AttributeTable table)
        {
            if (table == null) 
            {
                throw FxTrace.Exception.ArgumentNull("table"); 
            } 
            MutableTable.AddTable(table.MutableTable);
        } 

        // 
        // Creates an attribute table that contains all of the attribute
        // definitions provided through AddAttribute calls.  The table is 
        // a snapshot of the current state of the attribute builder; any
        // subsequent AddAttribute calls are not included in the table. 
        // 
        // If callback methods were used to declare attributes, those methods
        // will not be evaluated during CreateTable.  Instead, the table will 
        // contain those callbacks and will evaluate them as needed.
        // 
        // 
        // An attribute table that can be passed to the metadata store. 
        // 
        public AttributeTable CreateTable() 
        { 
            _cloneOnUse = true;
            return new AttributeTable(_table); 
        }

        // 
        // This method can be used to verify that the attribute table 
        // that is being built contains valid attribute information.
        // Some overrides of AddCustomAttributes cannot validate that 
        // values passed to their prameters represent valid members on 
        // classes.  Therefore, incorrect information passed to
        // AddCustomAttributes may go undetected.  ValidateTable will 
        // run through the contents of the AttributeTableBuilder and
        // verify that all custom attribute information matches up with
        // physical members.  Note:  calling this method can be very
        // costly so you should only do it when validation is absolutely 
        // needed.
        //  
        // if the state of the table is invalid. 
        public void ValidateTable()
        { 
            MutableTable.ValidateTable();
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------

namespace System.Activities.Presentation.Metadata 
{
 
    using System.Activities.Presentation.Internal.Metadata; 
    using System;
    using System.Collections; 
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Reflection;
    using System.Windows; 
    using System.Activities.Presentation;
 
    //  
    // An attribute table is a read only blob of data.  How
    // do you create one?  We will have a class called an 
    // Attribute Builder that can be used to create an attribute
    // table.  Attribute builders have methods you can call to
    // add metadata.  When you�re finished, you can produce an
    // attribute table from the builder.  Builder methods also 
    // support callback delegates so the entire process can be
    // deferred until needed. 
    //  
    public class AttributeTableBuilder
    { 

        private MutableAttributeTable _table = new MutableAttributeTable();
        private bool _cloneOnUse;
 
        //
        // Returns an attribute table we can make changes to 
        // 
        private MutableAttributeTable MutableTable
        { 
            get {
                if (_cloneOnUse)
                {
                    MutableAttributeTable clone = new MutableAttributeTable(); 
                    clone.AddTable(_table);
                    _table = clone; 
                    _cloneOnUse = false; 
                }
 
                return _table;
            }
        }
 
        // 
        // Adds a callback that will be invoked when metadata for the 
        // given type is needed.  The callback can add metadata to 
        // to the attribute table on demand, which is much more efficient
        // than adding metadata up front. 
        // 
        // 
        // 
        public void AddCallback(Type type, AttributeCallback callback) 
        {
            if (type == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("type");
            } 
            if (callback == null)
            {
                throw FxTrace.Exception.ArgumentNull("callback");
            } 
            MutableTable.AddCallback(type, callback);
        } 
 
        // 
        // Adds the contents of the provided attributes to this builder. 
        // Conflicts are resolved with a last-in-wins strategy.  When
        // building a large attribute table it is best to use AddCallback
        // to defer the work of creating attributes until they are needed.
        //  
        // The type to add class-level attributes to.
        //  
        // The new attributes to add. 
        // 
        // if type or attributes is null 
        public void AddCustomAttributes(Type type, params Attribute[] attributes) {
            if (type == null)
            {
                throw FxTrace.Exception.ArgumentNull("type"); 
            }
            if (attributes == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("attributes");
            } 
            MutableTable.AddCustomAttributes(type, attributes);
        }

        //  
        // Adds the contents of the provided attributes to this builder.
        // Conflicts are resolved with a last-in-wins strategy.  When 
        // building a large attribute table it is best to use AddCallback 
        // to defer the work of creating attributes until they are needed.
        //  
        // 
        // The type the member lives on.
        // 
        // An event or property descriptor to add attributes to. 
        // 
        // The new attributes to add. 
        //  
        // if descriptor or attributes is null
        public void AddCustomAttributes(Type ownerType, MemberDescriptor descriptor, params Attribute[] attributes) { 
            if (ownerType == null)
            {
                throw FxTrace.Exception.ArgumentNull("ownerType");
            } 
            if (descriptor == null)
            { 
                throw FxTrace.Exception.ArgumentNull("descriptor"); 
            }
            if (attributes == null) 
            {
                throw FxTrace.Exception.ArgumentNull("attributes");
            }
            MutableTable.AddCustomAttributes(ownerType, descriptor, attributes); 
        }
 
        //  
        // Adds the contents of the provided attributes to this builder.
        // Conflicts are resolved with a last-in-wins strategy.  When 
        // building a large attribute table it is best to use AddCallback
        // to defer the work of creating attributes until they are needed.
        // 
        //  
        // The type the member lives on.
        //  
        // An event or property info to add attributes to. 
        // 
        // The new attributes to add. 
        // 
        // if member or attributes is null
        public void AddCustomAttributes(Type ownerType, MemberInfo member, params Attribute[] attributes) {
            if (ownerType == null) 
            {
                throw FxTrace.Exception.ArgumentNull("ownerType"); 
            } 
            if (member == null)
            { 
                throw FxTrace.Exception.ArgumentNull("member");
            }
            if (attributes == null)
            { 
                throw FxTrace.Exception.ArgumentNull("attributes");
            } 
            MutableTable.AddCustomAttributes(ownerType, member, attributes); 
        }
 
        // 
        // Adds attributes to the member with the given name.  The member can be a property
        // or an event.  The member is evaluated on demand when the user queries
        // attributes on a given property or event. 
        // 
        //  
        // The type the member lives on. 
        // 
        //  
        // The member to add attributes for.  Only property and event members are supported;
        // all others will be ignored.
        // 
        //  
        // The new attributes to add.
        //  
        public void AddCustomAttributes(Type ownerType, string memberName, params Attribute[] attributes) { 
            if (ownerType == null)
            { 
                throw FxTrace.Exception.ArgumentNull("ownerType");
            }
            if (memberName == null)
            { 
                throw FxTrace.Exception.ArgumentNull("memberName");
            } 
            MutableTable.AddCustomAttributes(ownerType, memberName, attributes); 
        }
 
        // 
        // Adds the contents of the provided attributes to this builder.
        // Conflicts are resolved with a last-in-wins strategy.  When
        // building a large attribute table it is best to use AddCallback 
        // to defer the work of creating attributes until they are needed.
        //  
        //  
        // The type that owns the dependency property.
        //  
        // A dependency property to add attributes to.
        // 
        // The new attributes to add.
        //  
        // if dp, ownerType or attributes is null
        public void AddCustomAttributes(Type ownerType, DependencyProperty dp, params Attribute[] attributes) { 
            if (ownerType == null) 
            {
                throw FxTrace.Exception.ArgumentNull("ownerType"); 
            }
            if (dp == null)
            {
                throw FxTrace.Exception.ArgumentNull("dp"); 
            }
            if (attributes == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("attributes");
            } 
            MutableTable.AddCustomAttributes(ownerType, dp, attributes);
        }

        //  
        // Adds the contents of the provided attribute table to
        // this builder.  Conflicts are resolved with a last-in-wins 
        // strategy. 
        // 
        // An existing attribute table. 
        // if table is null
        public void AddTable(AttributeTable table)
        {
            if (table == null) 
            {
                throw FxTrace.Exception.ArgumentNull("table"); 
            } 
            MutableTable.AddTable(table.MutableTable);
        } 

        // 
        // Creates an attribute table that contains all of the attribute
        // definitions provided through AddAttribute calls.  The table is 
        // a snapshot of the current state of the attribute builder; any
        // subsequent AddAttribute calls are not included in the table. 
        // 
        // If callback methods were used to declare attributes, those methods
        // will not be evaluated during CreateTable.  Instead, the table will 
        // contain those callbacks and will evaluate them as needed.
        // 
        // 
        // An attribute table that can be passed to the metadata store. 
        // 
        public AttributeTable CreateTable() 
        { 
            _cloneOnUse = true;
            return new AttributeTable(_table); 
        }

        // 
        // This method can be used to verify that the attribute table 
        // that is being built contains valid attribute information.
        // Some overrides of AddCustomAttributes cannot validate that 
        // values passed to their prameters represent valid members on 
        // classes.  Therefore, incorrect information passed to
        // AddCustomAttributes may go undetected.  ValidateTable will 
        // run through the contents of the AttributeTableBuilder and
        // verify that all custom attribute information matches up with
        // physical members.  Note:  calling this method can be very
        // costly so you should only do it when validation is absolutely 
        // needed.
        //  
        // if the state of the table is invalid. 
        public void ValidateTable()
        { 
            MutableTable.ValidateTable();
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK