ParameterExpression.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 / fx / src / Core / Microsoft / Scripting / Ast / ParameterExpression.cs / 1305376 / ParameterExpression.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.Diagnostics; 
using System.Dynamic.Utils; 

#if SILVERLIGHT 
using System.Core;
#endif

namespace System.Linq.Expressions { 

    ///  
    /// Represents a named parameter expression. 
    /// 
#if !SILVERLIGHT 
    [DebuggerTypeProxy(typeof(Expression.ParameterExpressionProxy))]
#endif
    public class ParameterExpression : Expression {
        private readonly string _name; 

        internal ParameterExpression(string name) { 
            _name = name; 
        }
 
        internal static ParameterExpression Make(Type type, string name, bool isByRef) {
            if (isByRef) {
                return new ByRefParameterExpression(type, name);
            } else { 
                if (!type.IsEnum) {
                    switch (Type.GetTypeCode(type)) { 
                        case TypeCode.Boolean: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Byte: return new PrimitiveParameterExpression(name);
                        case TypeCode.Char: return new PrimitiveParameterExpression(name); 
                        case TypeCode.DateTime: return new PrimitiveParameterExpression(name);
                        case TypeCode.DBNull: return new PrimitiveParameterExpression(name);
                        case TypeCode.Decimal: return new PrimitiveParameterExpression(name);
                        case TypeCode.Double: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Int16: return new PrimitiveParameterExpression(name);
                        case TypeCode.Int32: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Int64: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Object:
                            // common reference types which we optimize go here.  Of course object is in 
                            // the list, the others are driven by profiling of various workloads.  This list
                            // should be kept short.
                            if (type == typeof(object)) {
                                return new ParameterExpression(name); 
                            } else if (type == typeof(Exception)) {
                                return new PrimitiveParameterExpression(name); 
                            } else if (type == typeof(object[])) { 
                                return new PrimitiveParameterExpression(name);
                            } 
                            break;
                        case TypeCode.SByte: return new PrimitiveParameterExpression(name);
                        case TypeCode.Single: return new PrimitiveParameterExpression(name);
                        case TypeCode.String: return new PrimitiveParameterExpression(name); 
                        case TypeCode.UInt16: return new PrimitiveParameterExpression(name);
                        case TypeCode.UInt32: return new PrimitiveParameterExpression(name); 
                        case TypeCode.UInt64: return new PrimitiveParameterExpression(name); 
                    }
                } 
            }

            return new TypedParameterExpression(type, name);
        } 

        ///  
        /// Gets the static type of the expression that this  represents. (Inherited from .) 
        /// 
        /// The  that represents the static type of the expression. 
        public override Type Type {
            get { return typeof(object); }
        }
 
        /// 
        /// Returns the node type of this . (Inherited from .) 
        ///  
        /// The  that represents this expression.
        public sealed override ExpressionType NodeType { 
            get { return ExpressionType.Parameter; }
        }

        ///  
        /// The Name of the parameter or variable.
        ///  
        public string Name { 
            get { return _name; }
        } 

        /// 
        /// Indicates that this ParameterExpression is to be treated as a ByRef parameter.
        ///  
        public bool IsByRef {
            get { 
                return GetIsByRef(); 
            }
        } 

        internal virtual bool GetIsByRef() {
            return false;
        } 

        ///  
        /// Dispatches to the specific visit method for this node type. 
        /// 
        protected internal override Expression Accept(ExpressionVisitor visitor) { 
            return visitor.VisitParameter(this);
        }
    }
 
    /// 
    /// Specialized subclass to avoid holding onto the byref flag in a 
    /// parameter expression.  This version always holds onto the expression 
    /// type explicitly and therefore derives from TypedParameterExpression.
    ///  
    internal sealed class ByRefParameterExpression : TypedParameterExpression {
        internal ByRefParameterExpression(Type type, string name)
            : base(type, name) {
        } 

        internal override bool GetIsByRef() { 
            return true; 
        }
    } 

    /// 
    /// Specialized subclass which holds onto the type of the expression for
    /// uncommon types. 
    /// 
    internal class TypedParameterExpression : ParameterExpression { 
        private readonly Type _paramType; 

        internal TypedParameterExpression(Type type, string name) 
            : base(name) {
            _paramType = type;
        }
 
        public sealed override Type Type {
            get { return _paramType; } 
        } 
    }
 
    /// 
    /// Generic type to avoid needing explicit storage for primitive data types
    /// which are commonly used.
    ///  
    internal sealed class PrimitiveParameterExpression : ParameterExpression {
        internal PrimitiveParameterExpression(string name) 
            : base(name) { 
        }
 
        public sealed override Type Type {
            get { return typeof(T); }
        }
    } 

    public partial class Expression { 
 
        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree. 
        /// 
        /// The type of the parameter or variable.
        /// A  node with the specified name and type.
        public static ParameterExpression Parameter(Type type) { 
            return Parameter(type, null);
        } 
 
        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree. 
        /// 
        /// The type of the parameter or variable.
        /// A  node with the specified name and type.
        public static ParameterExpression Variable(Type type) { 
            return Variable(type, null);
        } 
 
        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree. 
        /// 
        /// The type of the parameter or variable.
        /// The name of the parameter or variable, used for debugging or pretty printing purpose only.
        /// A  node with the specified name and type. 
        public static ParameterExpression Parameter(Type type, string name) {
            ContractUtils.RequiresNotNull(type, "type"); 
 
            if (type == typeof(void)) {
                throw Error.ArgumentCannotBeOfTypeVoid(); 
            }

            bool byref = type.IsByRef;
            if (byref) { 
                type = type.GetElementType();
            } 
 
            return ParameterExpression.Make(type, name, byref);
        } 

        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree.
        ///  
        /// The type of the parameter or variable.
        /// The name of the parameter or variable, used for debugging or pretty printing purpose only. 
        /// A  node with the specified name and type. 
        public static ParameterExpression Variable(Type type, string name) {
            ContractUtils.RequiresNotNull(type, "type"); 
            if (type == typeof(void)) throw Error.ArgumentCannotBeOfTypeVoid();
            if (type.IsByRef) throw Error.TypeMustNotBeByRef();
            return ParameterExpression.Make(type, name, false);
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
/* **************************************************************************** 
 *
 * 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.Diagnostics; 
using System.Dynamic.Utils; 

#if SILVERLIGHT 
using System.Core;
#endif

namespace System.Linq.Expressions { 

    ///  
    /// Represents a named parameter expression. 
    /// 
#if !SILVERLIGHT 
    [DebuggerTypeProxy(typeof(Expression.ParameterExpressionProxy))]
#endif
    public class ParameterExpression : Expression {
        private readonly string _name; 

        internal ParameterExpression(string name) { 
            _name = name; 
        }
 
        internal static ParameterExpression Make(Type type, string name, bool isByRef) {
            if (isByRef) {
                return new ByRefParameterExpression(type, name);
            } else { 
                if (!type.IsEnum) {
                    switch (Type.GetTypeCode(type)) { 
                        case TypeCode.Boolean: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Byte: return new PrimitiveParameterExpression(name);
                        case TypeCode.Char: return new PrimitiveParameterExpression(name); 
                        case TypeCode.DateTime: return new PrimitiveParameterExpression(name);
                        case TypeCode.DBNull: return new PrimitiveParameterExpression(name);
                        case TypeCode.Decimal: return new PrimitiveParameterExpression(name);
                        case TypeCode.Double: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Int16: return new PrimitiveParameterExpression(name);
                        case TypeCode.Int32: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Int64: return new PrimitiveParameterExpression(name); 
                        case TypeCode.Object:
                            // common reference types which we optimize go here.  Of course object is in 
                            // the list, the others are driven by profiling of various workloads.  This list
                            // should be kept short.
                            if (type == typeof(object)) {
                                return new ParameterExpression(name); 
                            } else if (type == typeof(Exception)) {
                                return new PrimitiveParameterExpression(name); 
                            } else if (type == typeof(object[])) { 
                                return new PrimitiveParameterExpression(name);
                            } 
                            break;
                        case TypeCode.SByte: return new PrimitiveParameterExpression(name);
                        case TypeCode.Single: return new PrimitiveParameterExpression(name);
                        case TypeCode.String: return new PrimitiveParameterExpression(name); 
                        case TypeCode.UInt16: return new PrimitiveParameterExpression(name);
                        case TypeCode.UInt32: return new PrimitiveParameterExpression(name); 
                        case TypeCode.UInt64: return new PrimitiveParameterExpression(name); 
                    }
                } 
            }

            return new TypedParameterExpression(type, name);
        } 

        ///  
        /// Gets the static type of the expression that this  represents. (Inherited from .) 
        /// 
        /// The  that represents the static type of the expression. 
        public override Type Type {
            get { return typeof(object); }
        }
 
        /// 
        /// Returns the node type of this . (Inherited from .) 
        ///  
        /// The  that represents this expression.
        public sealed override ExpressionType NodeType { 
            get { return ExpressionType.Parameter; }
        }

        ///  
        /// The Name of the parameter or variable.
        ///  
        public string Name { 
            get { return _name; }
        } 

        /// 
        /// Indicates that this ParameterExpression is to be treated as a ByRef parameter.
        ///  
        public bool IsByRef {
            get { 
                return GetIsByRef(); 
            }
        } 

        internal virtual bool GetIsByRef() {
            return false;
        } 

        ///  
        /// Dispatches to the specific visit method for this node type. 
        /// 
        protected internal override Expression Accept(ExpressionVisitor visitor) { 
            return visitor.VisitParameter(this);
        }
    }
 
    /// 
    /// Specialized subclass to avoid holding onto the byref flag in a 
    /// parameter expression.  This version always holds onto the expression 
    /// type explicitly and therefore derives from TypedParameterExpression.
    ///  
    internal sealed class ByRefParameterExpression : TypedParameterExpression {
        internal ByRefParameterExpression(Type type, string name)
            : base(type, name) {
        } 

        internal override bool GetIsByRef() { 
            return true; 
        }
    } 

    /// 
    /// Specialized subclass which holds onto the type of the expression for
    /// uncommon types. 
    /// 
    internal class TypedParameterExpression : ParameterExpression { 
        private readonly Type _paramType; 

        internal TypedParameterExpression(Type type, string name) 
            : base(name) {
            _paramType = type;
        }
 
        public sealed override Type Type {
            get { return _paramType; } 
        } 
    }
 
    /// 
    /// Generic type to avoid needing explicit storage for primitive data types
    /// which are commonly used.
    ///  
    internal sealed class PrimitiveParameterExpression : ParameterExpression {
        internal PrimitiveParameterExpression(string name) 
            : base(name) { 
        }
 
        public sealed override Type Type {
            get { return typeof(T); }
        }
    } 

    public partial class Expression { 
 
        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree. 
        /// 
        /// The type of the parameter or variable.
        /// A  node with the specified name and type.
        public static ParameterExpression Parameter(Type type) { 
            return Parameter(type, null);
        } 
 
        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree. 
        /// 
        /// The type of the parameter or variable.
        /// A  node with the specified name and type.
        public static ParameterExpression Variable(Type type) { 
            return Variable(type, null);
        } 
 
        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree. 
        /// 
        /// The type of the parameter or variable.
        /// The name of the parameter or variable, used for debugging or pretty printing purpose only.
        /// A  node with the specified name and type. 
        public static ParameterExpression Parameter(Type type, string name) {
            ContractUtils.RequiresNotNull(type, "type"); 
 
            if (type == typeof(void)) {
                throw Error.ArgumentCannotBeOfTypeVoid(); 
            }

            bool byref = type.IsByRef;
            if (byref) { 
                type = type.GetElementType();
            } 
 
            return ParameterExpression.Make(type, name, byref);
        } 

        /// 
        /// Creates a  node that can be used to identify a parameter or a variable in an expression tree.
        ///  
        /// The type of the parameter or variable.
        /// The name of the parameter or variable, used for debugging or pretty printing purpose only. 
        /// A  node with the specified name and type. 
        public static ParameterExpression Variable(Type type, string name) {
            ContractUtils.RequiresNotNull(type, "type"); 
            if (type == typeof(void)) throw Error.ArgumentCannotBeOfTypeVoid();
            if (type.IsByRef) throw Error.TypeMustNotBeByRef();
            return ParameterExpression.Make(type, name, false);
        } 
    }
} 

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