Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Scripting / Ast / ConstantExpression.cs / 1305376 / ConstantExpression.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 an expression that has a constant value.
///
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.ConstantExpressionProxy))]
#endif
public class ConstantExpression : Expression {
// Possible optimization: we could have a Constant subclass that
// stores the unboxed value.
private readonly object _value;
internal ConstantExpression(object value) {
_value = value;
}
internal static ConstantExpression Make(object value, Type type) {
if ((value == null && type == typeof(object)) || (value != null && value.GetType() == type)) {
return new ConstantExpression(value);
} else {
return new TypedConstantExpression(value, type);
}
}
///
/// Gets the static type of the expression that this represents.
///
/// The that represents the static type of the expression.
public override Type Type {
get {
if (_value == null) {
return typeof(object);
}
return _value.GetType();
}
}
///
/// Returns the node type of this Expression. Extension nodes should return
/// ExpressionType.Extension when overriding this method.
///
/// The of the expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Constant; }
}
///
/// Gets the value of the constant expression.
///
public object Value {
get { return _value; }
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitConstant(this);
}
}
internal class TypedConstantExpression : ConstantExpression {
private readonly Type _type;
internal TypedConstantExpression(object value, Type type)
: base(value) {
_type = type;
}
public sealed override Type Type {
get { return _type; }
}
}
public partial class Expression {
///
/// Creates a that has the property set to the specified value. .
///
/// An to set the property equal to.
///
/// A that has the property equal to
/// and the property set to the specified value.
///
public static ConstantExpression Constant(object value) {
return ConstantExpression.Make(value, value == null ? typeof(object) : value.GetType());
}
///
/// Creates a that has the
/// and properties set to the specified values. .
///
/// An to set the property equal to.
/// A to set the property equal to.
///
/// A that has the property equal to
/// and the and
/// properties set to the specified values.
///
public static ConstantExpression Constant(object value, Type type) {
ContractUtils.RequiresNotNull(type, "type");
if (value == null && type.IsValueType && !TypeUtils.IsNullableType(type)) {
throw Error.ArgumentTypesMustMatch();
}
if (value != null && !type.IsAssignableFrom(value.GetType())) {
throw Error.ArgumentTypesMustMatch();
}
return ConstantExpression.Make(value, type);
}
}
}
// 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 an expression that has a constant value.
///
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.ConstantExpressionProxy))]
#endif
public class ConstantExpression : Expression {
// Possible optimization: we could have a Constant subclass that
// stores the unboxed value.
private readonly object _value;
internal ConstantExpression(object value) {
_value = value;
}
internal static ConstantExpression Make(object value, Type type) {
if ((value == null && type == typeof(object)) || (value != null && value.GetType() == type)) {
return new ConstantExpression(value);
} else {
return new TypedConstantExpression(value, type);
}
}
///
/// Gets the static type of the expression that this represents.
///
/// The that represents the static type of the expression.
public override Type Type {
get {
if (_value == null) {
return typeof(object);
}
return _value.GetType();
}
}
///
/// Returns the node type of this Expression. Extension nodes should return
/// ExpressionType.Extension when overriding this method.
///
/// The of the expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Constant; }
}
///
/// Gets the value of the constant expression.
///
public object Value {
get { return _value; }
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitConstant(this);
}
}
internal class TypedConstantExpression : ConstantExpression {
private readonly Type _type;
internal TypedConstantExpression(object value, Type type)
: base(value) {
_type = type;
}
public sealed override Type Type {
get { return _type; }
}
}
public partial class Expression {
///
/// Creates a that has the property set to the specified value. .
///
/// An to set the property equal to.
///
/// A that has the property equal to
/// and the property set to the specified value.
///
public static ConstantExpression Constant(object value) {
return ConstantExpression.Make(value, value == null ? typeof(object) : value.GetType());
}
///
/// Creates a that has the
/// and properties set to the specified values. .
///
/// An to set the property equal to.
/// A to set the property equal to.
///
/// A that has the property equal to
/// and the and
/// properties set to the specified values.
///
public static ConstantExpression Constant(object value, Type type) {
ContractUtils.RequiresNotNull(type, "type");
if (value == null && type.IsValueType && !TypeUtils.IsNullableType(type)) {
throw Error.ArgumentTypesMustMatch();
}
if (value != null && !type.IsAssignableFrom(value.GetType())) {
throw Error.ArgumentTypesMustMatch();
}
return ConstantExpression.Make(value, type);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Version.cs
- SqlFunctionAttribute.cs
- BitVector32.cs
- ListBindableAttribute.cs
- SynthesizerStateChangedEventArgs.cs
- XmlSchemaAll.cs
- DynamicActionMessageFilter.cs
- WebUtil.cs
- AutomationElement.cs
- PTManager.cs
- DataGridViewSelectedRowCollection.cs
- DbConnectionStringCommon.cs
- EntityKey.cs
- Glyph.cs
- ChannelServices.cs
- ComPersistableTypeElementCollection.cs
- SortDescriptionCollection.cs
- DtrList.cs
- SafeFindHandle.cs
- XmlAnyAttributeAttribute.cs
- KeyTime.cs
- TypeForwardedToAttribute.cs
- ReadOnlyTernaryTree.cs
- Control.cs
- DecimalConverter.cs
- DataSourceView.cs
- SessionPageStateSection.cs
- ScopeElement.cs
- IndicCharClassifier.cs
- BatchWriter.cs
- BitmapDownload.cs
- XmlCharType.cs
- DataGridViewSelectedCellCollection.cs
- ConfigUtil.cs
- ConfigErrorGlyph.cs
- IndentTextWriter.cs
- CursorConverter.cs
- TypefaceMetricsCache.cs
- SQLStringStorage.cs
- cookiecollection.cs
- GeometryModel3D.cs
- GeometryHitTestParameters.cs
- MessageQueueAccessControlEntry.cs
- TypeGeneratedEventArgs.cs
- FlowDocumentPaginator.cs
- ConsoleCancelEventArgs.cs
- NumberSubstitution.cs
- LocatorPart.cs
- TextTreeObjectNode.cs
- TokenBasedSetEnumerator.cs
- GenericWebPart.cs
- OrderingQueryOperator.cs
- XmlDataSource.cs
- propertyentry.cs
- LinqDataView.cs
- EventManager.cs
- AnonymousIdentificationSection.cs
- OrderedEnumerableRowCollection.cs
- UpdateCommand.cs
- Object.cs
- BoundPropertyEntry.cs
- MexTcpBindingElement.cs
- JsonMessageEncoderFactory.cs
- NestPullup.cs
- ContentDisposition.cs
- BinaryNode.cs
- XmlCharType.cs
- Timer.cs
- TileModeValidation.cs
- Converter.cs
- RequestCacheValidator.cs
- Simplifier.cs
- ImageListUtils.cs
- StrokeRenderer.cs
- ClonableStack.cs
- PerfCounters.cs
- DbConnectionInternal.cs
- ViewStateException.cs
- CFStream.cs
- GridViewSortEventArgs.cs
- InsufficientMemoryException.cs
- Baml2006ReaderFrame.cs
- AxHost.cs
- WorkflowWebService.cs
- MatrixTransform3D.cs
- ForceCopyBuildProvider.cs
- PrivilegeNotHeldException.cs
- Registry.cs
- TextStore.cs
- TimeoutHelper.cs
- _Semaphore.cs
- ComponentResourceKey.cs
- WinInetCache.cs
- RequestBringIntoViewEventArgs.cs
- DockAndAnchorLayout.cs
- WebResourceUtil.cs
- FormViewModeEventArgs.cs
- CommonServiceBehaviorElement.cs
- ImmComposition.cs
- BitHelper.cs