Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Expressions / Literal.cs / 1305376 / Literal.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities.Expressions { using System; using System.Activities.ExpressionParser; using System.Activities.XamlIntegration; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Linq.Expressions; using System.Runtime; using System.Text.RegularExpressions; using System.Windows.Markup; [ContentProperty("Value")] public sealed class Literal: CodeActivity , IExpressionContainer, IValueSerializableExpression { static Regex ExpressionEscapeRegex = new Regex(@"^(%*\[)"); public Literal() { } public Literal(T value) : this() { this.Value = value; } public T Value { get; set; } Expression IExpressionContainer.Expression { get { return Expression.Lambda >(Expression.Constant(this.Value, typeof(T)), ExpressionUtilities.RuntimeContextParameter); } } protected override void CacheMetadata(CodeActivityMetadata metadata) { Type literalType = typeof(T); if (!literalType.IsValueType && literalType != TypeHelper.StringType) { metadata.AddValidationError(SR.LiteralsMustBeValueTypesOrImmutableTypes(TypeHelper.StringType, literalType)); } } protected override T Execute(CodeActivityContext context) { return ExecuteWithTryGetValue(context); } internal override bool TryGetValue(ActivityContext context, out T value) { value = this.Value; return true; } public override string ToString() { return this.Value == null ? "null" : this.Value.ToString(); } public bool CanConvertToString(IValueSerializerContext context) { Type typeArgument; Type valueType; TypeConverter converter; if (this.Value == null) { return true; } typeArgument = typeof(T); valueType = this.Value.GetType(); if (valueType == TypeHelper.StringType) { string myValue = this.Value as string; if (string.IsNullOrEmpty(myValue)) { return false; } } converter = TypeDescriptor.GetConverter(typeArgument); if (typeArgument == valueType && converter != null && converter.CanConvertTo(TypeHelper.StringType) && converter.CanConvertFrom(TypeHelper.StringType)) { return true; } return false; } [SuppressMessage(FxCop.Category.Globalization, FxCop.Rule.SpecifyIFormatProvider, Justification = "we really do want the string as-is")] public string ConvertToString(IValueSerializerContext context) { Type typeArgument; Type valueType; TypeConverter converter; if (this.Value == null) { return "[Nothing]"; } typeArgument = typeof(T); valueType = this.Value.GetType(); converter = TypeDescriptor.GetConverter(typeArgument); Fx.Assert(typeArgument == valueType && converter != null && converter.CanConvertTo(TypeHelper.StringType) && converter.CanConvertFrom(TypeHelper.StringType), "Literal target type T and the return type mismatch or something wrong with its typeConverter!"); // handle a Literal of "[...]" by inserting escape chararcter '%' at the front if (typeArgument == TypeHelper.StringType) { string originalString = Convert.ToString(this.Value); if (originalString.EndsWith("]", StringComparison.Ordinal) && ExpressionEscapeRegex.IsMatch(originalString)) { return "%" + originalString; } } return converter.ConvertToString(context, this.Value); } [EditorBrowsable(EditorBrowsableState.Never)] public bool ShouldSerializeValue() { return !object.Equals(this.Value, default(T)); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PointValueSerializer.cs
- IndexOutOfRangeException.cs
- MailWebEventProvider.cs
- ImmutableObjectAttribute.cs
- BaseCollection.cs
- SoapParser.cs
- CompareInfo.cs
- ProxyWebPartConnectionCollection.cs
- EndpointAddressMessageFilter.cs
- DocumentGridPage.cs
- OleDbRowUpdatingEvent.cs
- IOThreadTimer.cs
- DataReaderContainer.cs
- FlowDocumentReaderAutomationPeer.cs
- ParenthesizePropertyNameAttribute.cs
- DefaultPrintController.cs
- HijriCalendar.cs
- CheckPair.cs
- ApplicationException.cs
- CmsUtils.cs
- SamlDoNotCacheCondition.cs
- ToolStripHighContrastRenderer.cs
- SecurityElement.cs
- LockingPersistenceProvider.cs
- QilXmlWriter.cs
- CodeNamespace.cs
- MetadataItemSerializer.cs
- ModifierKeysConverter.cs
- ImageDrawing.cs
- DynamicExpression.cs
- WarningException.cs
- CapabilitiesSection.cs
- OneToOneMappingSerializer.cs
- WebPartEditorOkVerb.cs
- TreeBuilderXamlTranslator.cs
- CompoundFileStreamReference.cs
- ToolStripContainerDesigner.cs
- EventlogProvider.cs
- ContractUtils.cs
- EntityProxyFactory.cs
- SQLBinaryStorage.cs
- StringExpressionSet.cs
- ConstNode.cs
- XmlQueryTypeFactory.cs
- SparseMemoryStream.cs
- DocumentApplication.cs
- MetadataPropertyCollection.cs
- PriorityBindingExpression.cs
- SchemaManager.cs
- _NegoStream.cs
- SchemaNotation.cs
- Boolean.cs
- AspNetPartialTrustHelpers.cs
- EngineSite.cs
- KeyPressEvent.cs
- ListViewEditEventArgs.cs
- IdleTimeoutMonitor.cs
- RegexEditorDialog.cs
- Collection.cs
- AsymmetricSignatureDeformatter.cs
- CatalogPartDesigner.cs
- SmtpCommands.cs
- Double.cs
- ZoneIdentityPermission.cs
- PropertyIDSet.cs
- Vector.cs
- SpeakCompletedEventArgs.cs
- Image.cs
- AdPostCacheSubstitution.cs
- _AutoWebProxyScriptWrapper.cs
- UrlAuthFailedErrorFormatter.cs
- Publisher.cs
- XamlToRtfParser.cs
- RuntimeResourceSet.cs
- PngBitmapDecoder.cs
- BooleanKeyFrameCollection.cs
- Rotation3DAnimationBase.cs
- SQLSingle.cs
- ScrollPatternIdentifiers.cs
- Int64Converter.cs
- EventsTab.cs
- AuthenticationModuleElementCollection.cs
- _RequestCacheProtocol.cs
- SynchronizationContext.cs
- ProfessionalColorTable.cs
- Typeface.cs
- HMAC.cs
- NamedPipeDuplicateContext.cs
- ThumbAutomationPeer.cs
- TimeSpanStorage.cs
- PeerDuplexChannelListener.cs
- RuleProcessor.cs
- EventProviderWriter.cs
- Rijndael.cs
- CompilationSection.cs
- ResumeStoryboard.cs
- PropertyGridView.cs
- HighlightVisual.cs
- EntityContainerAssociationSetEnd.cs
- ReadOnlyNameValueCollection.cs