MultidimensionalArrayItemReference.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Expressions / MultidimensionalArrayItemReference.cs / 1305376 / MultidimensionalArrayItemReference.cs

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

namespace System.Activities.Expressions 
{
    using System.Collections.ObjectModel; 
    using System.ComponentModel; 
    using System.Runtime;
    using System.Runtime.Collections; 
    using System.Runtime.Serialization;
    using System.Windows.Markup;

    [ContentProperty("Indices")] 
    public sealed class MultidimensionalArrayItemReference : CodeActivity>
    { 
        Collection> indices; 

        [RequiredArgument] 
        [DefaultValue(null)]
        public InArgument Array
        {
            get; 
            set;
        } 
 
        [DefaultValue(null)]
        public Collection> Indices 
        {
            get
            {
                if (this.indices == null) 
                {
                    this.indices = new ValidatingCollection> 
                    { 
                        // disallow null values
                        OnAddValidationCallback = item => 
                        {
                            if (item == null)
                            {
                                throw FxTrace.Exception.ArgumentNull("item"); 
                            }
                        }, 
                    }; 
                }
                return this.indices; 
            }
        }

        protected override void CacheMetadata(CodeActivityMetadata metadata) 
        {
            if (this.Indices.Count == 0) 
            { 
                metadata.AddValidationError(SR.IndicesAreNeeded(this.GetType().Name, this.DisplayName));
            } 

            RuntimeArgument arrayArgument = new RuntimeArgument("Array", typeof(Array), ArgumentDirection.In, true);
            metadata.Bind(this.Array, arrayArgument);
            metadata.AddArgument(arrayArgument); 

            for (int i = 0; i < this.Indices.Count; i++) 
            { 
                RuntimeArgument indexArgument = new RuntimeArgument("Index_" + i, typeof(int), ArgumentDirection.In, true);
                metadata.Bind(this.Indices[i], indexArgument); 
                metadata.AddArgument(indexArgument);
            }

            RuntimeArgument resultArgument = new RuntimeArgument("Result", typeof(Location), ArgumentDirection.Out); 
            metadata.Bind(this.Result, resultArgument);
            metadata.AddArgument(resultArgument); 
        } 

        protected override Location Execute(CodeActivityContext context) 
        {
            Array items = this.Array.Get(context);

            if (items == null) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.MemberCannotBeNull("Array", this.GetType().Name, this.DisplayName))); 
            } 

            Type realItemType = items.GetType().GetElementType(); 
            if (!TypeHelper.AreTypesCompatible(typeof(TItem), realItemType))
            {
                throw FxTrace.Exception.AsError(new InvalidCastException(SR.IncompatibleTypeForMultidimensionalArrayItemReference(typeof(TItem).Name, realItemType.Name)));
            } 
            int[] itemIndex = new int[this.Indices.Count];
            for (int i = 0; i < this.Indices.Count; i++) 
            { 
                itemIndex[i] = this.Indices[i].Get(context);
            } 
            return new MultidimensionArrayLocation(items, itemIndex);
        }

        [DataContract] 
        class MultidimensionArrayLocation : Location
        { 
            [DataMember] 
            Array array;
 
            [DataMember(EmitDefaultValue = false)]
            int[] indices;

            public MultidimensionArrayLocation(Array array, int[] indices) 
                : base()
            { 
                this.array = array; 
                this.indices = indices;
            } 

            public override TItem Value
            {
                get 
                {
                    return (TItem)this.array.GetValue(indices); 
                } 
                set
                { 
                    this.array.SetValue(value, indices);
                }
            }
        } 
    }
} 

// 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