Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / QualifiedId.cs / 1305376 / QualifiedId.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities { using System; using System.Collections.Generic; using System.Runtime; using System.Text; class QualifiedId : IEquatable{ byte[] compressedId; public QualifiedId(Activity element) { int bufferSize = 0; Stack ids = new Stack (); int id = element.InternalId; bufferSize += GetEncodedSize(id); ids.Push(id); IdSpace space = element.MemberOf; while (space != null && space.ParentId != 0) { bufferSize += GetEncodedSize(space.ParentId); ids.Push(space.ParentId); space = space.Parent; } this.compressedId = new byte[bufferSize]; int offset = 0; while (ids.Count > 0) { offset += Encode(ids.Pop(), this.compressedId, offset); } } public QualifiedId(byte[] bytes) { this.compressedId = bytes; } public static bool TryGetElementFromRoot(Activity root, QualifiedId id, out Activity targetElement) { return TryGetElementFromRoot(root, id.compressedId, out targetElement); } public static bool TryGetElementFromRoot(Activity root, byte[] idBytes, out Activity targetElement) { Fx.Assert(root.MemberOf != null, "We need to have our IdSpaces set up for this to work."); Activity currentActivity = root; IdSpace currentIdSpace = root.MemberOf; int offset = 0; while (offset < idBytes.Length) { int value; offset += Decode(idBytes, offset, out value); if (currentIdSpace == null) { targetElement = null; return false; } currentActivity = currentIdSpace[value]; if (currentActivity == null) { targetElement = null; return false; } currentIdSpace = currentActivity.ParentOf; } targetElement = currentActivity; return true; } public static QualifiedId Parse(string value) { QualifiedId result; if (!TryParse(value, out result)) { throw FxTrace.Exception.AsError(new FormatException(SR.InvalidActivityIdFormat)); } return result; } public static bool TryParse(string value, out QualifiedId result) { Fx.Assert(!string.IsNullOrEmpty(value), "We should have already made sure it isn't null or empty."); string[] idStrings = value.Split('.'); int[] ids = new int[idStrings.Length]; int bufferSize = 0; for (int i = 0; i < idStrings.Length; i++) { // only support non-negative integers as id segments int parsedInt; if (!int.TryParse(idStrings[i], out parsedInt) || parsedInt < 0) { result = null; return false; } ids[i] = parsedInt; bufferSize += GetEncodedSize(ids[i]); } byte[] bytes = new byte[bufferSize]; int offset = 0; for (int i = 0; i < ids.Length; i++) { offset += Encode(ids[i], bytes, offset); } result = new QualifiedId(bytes); return true; } public static bool Equals(byte[] lhs, byte[] rhs) { if (lhs.Length == rhs.Length) { for (int i = 0; i < lhs.Length; i++) { if (lhs[i] != rhs[i]) { return false; } } return true; } return false; } public byte[] AsByteArray() { // Note that we don't do a copy because we assume all users will // treat it as immutable. return this.compressedId; } public bool Equals(QualifiedId rhs) { return Equals(this.compressedId, rhs.compressedId); } public override string ToString() { StringBuilder builder = new StringBuilder(); bool needDot = false; int offset = 0; while (offset < this.compressedId.Length) { if (needDot) { builder.Append('.'); } int value; offset += Decode(this.compressedId, offset, out value); builder.Append(value); needDot = true; } return builder.ToString(); } // This is the same Encode/Decode logic as the WCF FramingEncoder static int Encode(int value, byte[] bytes, int offset) { Fx.Assert(value >= 0, "Must be non-negative"); int count = 1; while ((value & 0xFFFFFF80) != 0) { bytes[offset++] = (byte)((value & 0x7F) | 0x80); count++; value >>= 7; } bytes[offset] = (byte)value; return count; } // This is the same Encode/Decode logic as the WCF FramingEncoder static int Decode(byte[] buffer, int offset, out int value) { int bytesConsumed = 0; value = 0; while (offset < buffer.Length) { int next = buffer[offset]; value |= (next & 0x7F) << (bytesConsumed * 7); bytesConsumed++; if ((next & 0x80) == 0) { break; } offset++; } return bytesConsumed; } static int GetEncodedSize(int value) { Fx.Assert(value >= 0, "Must be non-negative"); int count = 1; while ((value & 0xFFFFFF80) != 0) { count++; value >>= 7; } return count; } } } // 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
- TdsParserSessionPool.cs
- GCHandleCookieTable.cs
- ChooseAction.cs
- ResourceDictionary.cs
- VerificationException.cs
- DataColumn.cs
- VirtualPathUtility.cs
- ViewBox.cs
- XDRSchema.cs
- ImageFormatConverter.cs
- DataGridItem.cs
- Typeface.cs
- TraceSection.cs
- AbstractExpressions.cs
- LayoutInformation.cs
- SecurityResources.cs
- DSASignatureDeformatter.cs
- Policy.cs
- compensatingcollection.cs
- ListControlBoundActionList.cs
- ComponentManagerBroker.cs
- WorkingDirectoryEditor.cs
- EnumConverter.cs
- EncodingTable.cs
- AutomationPatternInfo.cs
- Part.cs
- DataGridViewIntLinkedList.cs
- PeerDuplexChannelListener.cs
- ToolStripTextBox.cs
- ListenerHandler.cs
- BaseTypeViewSchema.cs
- AggregateNode.cs
- WindowAutomationPeer.cs
- Enum.cs
- InvokeHandlers.cs
- PriorityChain.cs
- ToolTip.cs
- SecurityTokenSerializer.cs
- RelativeSource.cs
- StringResourceManager.cs
- MSHTMLHost.cs
- PersonalizationProviderCollection.cs
- SignatureGenerator.cs
- TimeoutValidationAttribute.cs
- EditorZoneAutoFormat.cs
- EntityAdapter.cs
- DocumentViewerBase.cs
- DocComment.cs
- XmlSecureResolver.cs
- EventLogHandle.cs
- RequestUriProcessor.cs
- RegexCapture.cs
- DelegatingConfigHost.cs
- Attachment.cs
- Positioning.cs
- TabControl.cs
- PersistNameAttribute.cs
- EFDataModelProvider.cs
- ScrollContentPresenter.cs
- SymmetricAlgorithm.cs
- FontFaceLayoutInfo.cs
- HostingEnvironmentSection.cs
- TextReader.cs
- AssemblyBuilder.cs
- UpdateTracker.cs
- Graphics.cs
- StringAnimationUsingKeyFrames.cs
- wgx_render.cs
- DateTimeOffsetStorage.cs
- ServiceErrorHandler.cs
- XmlSchemaResource.cs
- __Filters.cs
- NavigationFailedEventArgs.cs
- Parser.cs
- EntityModelBuildProvider.cs
- QueueProcessor.cs
- HttpCookieCollection.cs
- DataObjectPastingEventArgs.cs
- ManifestResourceInfo.cs
- BrowserCapabilitiesFactory35.cs
- HierarchicalDataSourceConverter.cs
- CharacterString.cs
- LogEntrySerialization.cs
- GradientStop.cs
- TabItem.cs
- XmlSchemaSearchPattern.cs
- BrowserDefinitionCollection.cs
- ListControl.cs
- CollectionChange.cs
- PieceNameHelper.cs
- TrackingAnnotationCollection.cs
- DataGrid.cs
- PersonalizationEntry.cs
- QilPatternVisitor.cs
- StrictAndMessageFilter.cs
- PersonalizationStateInfoCollection.cs
- DataServiceConfiguration.cs
- EncoderNLS.cs
- newinstructionaction.cs
- BitmapSourceSafeMILHandle.cs