NewArrayExpression.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 / fx / src / Core / Microsoft / Scripting / Ast / NewArrayExpression.cs / 1305376 / NewArrayExpression.cs

                            /* **************************************************************************** 
 *
 * Copyright (c) Microsoft Corporation.
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/ 

using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Diagnostics;
using System.Dynamic.Utils; 
using System.Runtime.CompilerServices;

#if SILVERLIGHT
using System.Core; 
#endif
 
namespace System.Linq.Expressions { 

    ///  
    /// Represents creating a new array and possibly initializing the elements of the new array.
    /// 
#if !SILVERLIGHT
    [DebuggerTypeProxy(typeof(Expression.NewArrayExpressionProxy))] 
#endif
    public class NewArrayExpression : Expression { 
        private readonly ReadOnlyCollection _expressions; 
        private readonly Type _type;
 
        internal NewArrayExpression(Type type, ReadOnlyCollection expressions) {
            _expressions = expressions;
            _type = type;
        } 

        internal static NewArrayExpression Make(ExpressionType nodeType, Type type, ReadOnlyCollection expressions) { 
            if (nodeType == ExpressionType.NewArrayInit) { 
                return new NewArrayInitExpression(type, expressions);
            } else { 
                return new NewArrayBoundsExpression(type, expressions);
            }
        }
 
        /// 
        /// Gets the static type of the expression that this  represents. (Inherited from .) 
        ///  
        /// The  that represents the static type of the expression.
        public sealed override Type Type { 
            get { return _type; }
        }

        ///  
        /// Gets the bounds of the array if the value of the  property is NewArrayBounds, or the values to initialize the elements of the new array if the value of the  property is NewArrayInit.
        ///  
        public ReadOnlyCollection Expressions { 
            get { return _expressions; }
        } 

        /// 
        /// Dispatches to the specific visit method for this node type.
        ///  
        protected internal override Expression Accept(ExpressionVisitor visitor) {
            return visitor.VisitNewArray(this); 
        } 

        ///  
        /// Creates a new expression that is like this one, but using the
        /// supplied children. If all of the children are the same, it will
        /// return this expression.
        ///  
        /// The  property of the result.
        /// This expression if no children changed, or an expression with the updated children. 
        public NewArrayExpression Update(IEnumerable expressions) { 
            if (expressions == Expressions) {
                return this; 
            }
            if (NodeType == ExpressionType.NewArrayInit) {
                return Expression.NewArrayInit(Type.GetElementType(), expressions);
            } 
            return Expression.NewArrayBounds(Type.GetElementType(), expressions);
        } 
    } 

    internal sealed class NewArrayInitExpression : NewArrayExpression { 
        internal NewArrayInitExpression(Type type, ReadOnlyCollection expressions)
            : base(type, expressions) {
        }
 

        ///  
        /// Returns the node type of this . (Inherited from .) 
        /// 
        /// The  that represents this expression. 
        public sealed override ExpressionType NodeType {
            get { return ExpressionType.NewArrayInit; }
        }
    } 

    internal sealed class NewArrayBoundsExpression : NewArrayExpression { 
        internal NewArrayBoundsExpression(Type type, ReadOnlyCollection expressions) 
            : base(type, expressions) {
        } 

        /// 
        /// Returns the node type of this . (Inherited from .)
        ///  
        /// The  that represents this expression.
        public sealed override ExpressionType NodeType { 
            get { return ExpressionType.NewArrayBounds; } 
        }
    } 

    public partial class Expression {

        #region NewArrayInit 

 
        ///  
        /// Creates a new array expression of the specified type from the provided initializers.
        ///  
        /// A Type that represents the element type of the array.
        /// The expressions used to create the array elements.
        /// An instance of the .
        public static NewArrayExpression NewArrayInit(Type type, params Expression[] initializers) { 
            return NewArrayInit(type, (IEnumerable)initializers);
        } 
 
        /// 
        /// Creates a new array expression of the specified type from the provided initializers. 
        /// 
        /// A Type that represents the element type of the array.
        /// The expressions used to create the array elements.
        /// An instance of the . 
        public static NewArrayExpression NewArrayInit(Type type, IEnumerable initializers) {
            ContractUtils.RequiresNotNull(type, "type"); 
            ContractUtils.RequiresNotNull(initializers, "initializers"); 
            if (type.Equals(typeof(void))) {
                throw Error.ArgumentCannotBeOfTypeVoid(); 
            }

            ReadOnlyCollection initializerList = initializers.ToReadOnly();
 
            Expression[] newList = null;
            for (int i = 0, n = initializerList.Count; i < n; i++) { 
                Expression expr = initializerList[i]; 
                RequiresCanRead(expr, "initializers");
 
                if (!TypeUtils.AreReferenceAssignable(type, expr.Type)) {
                    if (TypeUtils.IsSameOrSubclass(typeof(LambdaExpression), type) && type.IsAssignableFrom(expr.GetType())) {
                        expr = Expression.Quote(expr);
                    } else { 
                        throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type);
                    } 
                    if (newList == null) { 
                        newList = new Expression[initializerList.Count];
                        for (int j = 0; j < i; j++) { 
                            newList[j] = initializerList[j];
                        }
                    }
                } 
                if (newList != null) {
                    newList[i] = expr; 
                } 
            }
            if (newList != null) { 
                initializerList = new TrueReadOnlyCollection(newList);
            }

            return NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList); 
        }
 
        #endregion 

        #region NewArrayBounds 


        /// 
        /// Creates a  that represents creating an array that has a specified rank. 
        /// 
        /// A  that represents the element type of the array. 
        /// An array that contains Expression objects to use to populate the Expressions collection. 
        /// A  that has the  property equal to type and the  property set to the specified value.
        public static NewArrayExpression NewArrayBounds(Type type, params Expression[] bounds) { 
            return NewArrayBounds(type, (IEnumerable)bounds);
        }

 
        /// 
        /// Creates a  that represents creating an array that has a specified rank. 
        ///  
        /// A  that represents the element type of the array.
        /// An IEnumerable{T} that contains Expression objects to use to populate the Expressions collection. 
        /// A  that has the  property equal to type and the  property set to the specified value.
        public static NewArrayExpression NewArrayBounds(Type type, IEnumerable bounds) {
            ContractUtils.RequiresNotNull(type, "type");
            ContractUtils.RequiresNotNull(bounds, "bounds"); 

            if (type.Equals(typeof(void))) { 
                throw Error.ArgumentCannotBeOfTypeVoid(); 
            }
 
            ReadOnlyCollection boundsList = bounds.ToReadOnly();

            int dimensions = boundsList.Count;
            if (dimensions <= 0) throw Error.BoundsCannotBeLessThanOne(); 

            for (int i = 0; i < dimensions; i++) { 
                Expression expr = boundsList[i]; 
                RequiresCanRead(expr, "bounds");
                if (!TypeUtils.IsInteger(expr.Type)) { 
                    throw Error.ArgumentMustBeInteger();
                }
            }
 
            Type arrayType;
            if (dimensions == 1) { 
                //To get a vector, need call Type.MakeArrayType(). 
                //Type.MakeArrayType(1) gives a non-vector array, which will cause type check error.
                arrayType = type.MakeArrayType(); 
            } else {
                arrayType = type.MakeArrayType(dimensions);
            }
 
            return NewArrayExpression.Make(ExpressionType.NewArrayBounds, arrayType, bounds.ToReadOnly());
        } 
 
        #endregion
 
    }
}

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


                        

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