Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Wmi / managed / System / Management / Instrumentation / CodeSpit.cs / 1305376 / CodeSpit.cs
namespace System.Management.Instrumentation { using System; using System.IO; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.Runtime.Versioning; class CodeWriter { int depth; ArrayList children = new ArrayList(); public static explicit operator String(CodeWriter writer) { return writer.ToString(); } public override string ToString() { StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); WriteCode(writer); string retString = writer.ToString(); writer.Close(); return retString; } void WriteCode(TextWriter writer) { string prefix = new String(' ', depth*4); foreach(Object child in children) { if(null == child) { writer.WriteLine(); } else if(child is string) { writer.Write(prefix); writer.WriteLine(child); } else ((CodeWriter)child).WriteCode(writer); } } public CodeWriter AddChild(string name) { Line(name); Line("{"); CodeWriter child = new CodeWriter(); child.depth = depth+1; children.Add(child); Line("}"); return child; } public CodeWriter AddChild(params string[] parts) { return AddChild(String.Concat(parts)); } public CodeWriter AddChildNoIndent(string name) { Line(name); CodeWriter child = new CodeWriter(); child.depth = depth+1; children.Add(child); return child; } public CodeWriter AddChild(CodeWriter snippet) { snippet.depth = depth; children.Add(snippet); return snippet; } public void Line(string line) { children.Add(line); } public void Line(params string[] parts) { Line(String.Concat(parts)); } public void Line() { children.Add(null); } } class ReferencesCollection { StringCollection namespaces = new StringCollection(); public StringCollection Namespaces { get { return namespaces; } } StringCollection assemblies = new StringCollection(); public StringCollection Assemblies { get { return assemblies; } } CodeWriter usingCode = new CodeWriter(); public CodeWriter UsingCode { get {return usingCode; } } [ResourceExposure(ResourceScope.None),ResourceConsumption(ResourceScope.Machine,ResourceScope.Machine)] public void Add(Type type) { if(!namespaces.Contains(type.Namespace)) { namespaces.Add(type.Namespace); usingCode.Line(String.Format("using {0};", type.Namespace)); } if(!assemblies.Contains(type.Assembly.Location)) assemblies.Add(type.Assembly.Location); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. namespace System.Management.Instrumentation { using System; using System.IO; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.Runtime.Versioning; class CodeWriter { int depth; ArrayList children = new ArrayList(); public static explicit operator String(CodeWriter writer) { return writer.ToString(); } public override string ToString() { StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); WriteCode(writer); string retString = writer.ToString(); writer.Close(); return retString; } void WriteCode(TextWriter writer) { string prefix = new String(' ', depth*4); foreach(Object child in children) { if(null == child) { writer.WriteLine(); } else if(child is string) { writer.Write(prefix); writer.WriteLine(child); } else ((CodeWriter)child).WriteCode(writer); } } public CodeWriter AddChild(string name) { Line(name); Line("{"); CodeWriter child = new CodeWriter(); child.depth = depth+1; children.Add(child); Line("}"); return child; } public CodeWriter AddChild(params string[] parts) { return AddChild(String.Concat(parts)); } public CodeWriter AddChildNoIndent(string name) { Line(name); CodeWriter child = new CodeWriter(); child.depth = depth+1; children.Add(child); return child; } public CodeWriter AddChild(CodeWriter snippet) { snippet.depth = depth; children.Add(snippet); return snippet; } public void Line(string line) { children.Add(line); } public void Line(params string[] parts) { Line(String.Concat(parts)); } public void Line() { children.Add(null); } } class ReferencesCollection { StringCollection namespaces = new StringCollection(); public StringCollection Namespaces { get { return namespaces; } } StringCollection assemblies = new StringCollection(); public StringCollection Assemblies { get { return assemblies; } } CodeWriter usingCode = new CodeWriter(); public CodeWriter UsingCode { get {return usingCode; } } [ResourceExposure(ResourceScope.None),ResourceConsumption(ResourceScope.Machine,ResourceScope.Machine)] public void Add(Type type) { if(!namespaces.Contains(type.Namespace)) { namespaces.Add(type.Namespace); usingCode.Line(String.Format("using {0};", type.Namespace)); } if(!assemblies.Contains(type.Assembly.Location)) assemblies.Add(type.Assembly.Location); } } } // 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
- DelegatingConfigHost.cs
- LocatorBase.cs
- ConfigurationSchemaErrors.cs
- TypeToTreeConverter.cs
- SortQuery.cs
- ElementProxy.cs
- MemoryRecordBuffer.cs
- thaishape.cs
- WindowsFormsLinkLabel.cs
- DomainUpDown.cs
- PolicyStatement.cs
- QilValidationVisitor.cs
- InsufficientMemoryException.cs
- ColorConverter.cs
- JulianCalendar.cs
- CodeRemoveEventStatement.cs
- CurrencyManager.cs
- TreeNodeClickEventArgs.cs
- DbExpressionVisitor_TResultType.cs
- ArrayWithOffset.cs
- HttpConfigurationSystem.cs
- RedirectionProxy.cs
- Token.cs
- ControlUtil.cs
- EncodingInfo.cs
- ScrollData.cs
- UserPersonalizationStateInfo.cs
- SqlStatistics.cs
- DataBinder.cs
- DataFormats.cs
- BCLDebug.cs
- ConnectorRouter.cs
- TextTreeRootNode.cs
- ScriptServiceAttribute.cs
- IndicCharClassifier.cs
- DispatcherExceptionEventArgs.cs
- ConfigErrorGlyph.cs
- Rules.cs
- InputScopeConverter.cs
- BindingManagerDataErrorEventArgs.cs
- MailWriter.cs
- NetworkInformationPermission.cs
- PerformanceCounterPermissionEntry.cs
- BaseUriWithWildcard.cs
- ThreadStateException.cs
- DynamicRenderer.cs
- CrossContextChannel.cs
- SqlDataSourceCommandParser.cs
- DataBoundControlHelper.cs
- DataServiceRequest.cs
- ValidationService.cs
- ParserContext.cs
- CornerRadius.cs
- Int16Converter.cs
- SystemGatewayIPAddressInformation.cs
- Expressions.cs
- VirtualDirectoryMapping.cs
- TimeoutConverter.cs
- DataGridViewRowContextMenuStripNeededEventArgs.cs
- SynchronizedDispatch.cs
- SpellerStatusTable.cs
- FrameworkContentElement.cs
- LoadGrammarCompletedEventArgs.cs
- XmlSchemaElement.cs
- SimpleWebHandlerParser.cs
- RemotingConfigParser.cs
- PixelFormat.cs
- COM2Enum.cs
- NamespaceCollection.cs
- CompoundFileStorageReference.cs
- FontCacheUtil.cs
- RetriableClipboard.cs
- WindowsButton.cs
- Semaphore.cs
- ListViewDeletedEventArgs.cs
- ApplyTemplatesAction.cs
- InstanceNormalEvent.cs
- XmlDataContract.cs
- FontSizeConverter.cs
- RightsManagementInformation.cs
- DataControlFieldCell.cs
- FixedPageStructure.cs
- CmsUtils.cs
- ActivityTypeCodeDomSerializer.cs
- ToolStrip.cs
- SystemIdentity.cs
- InkCanvasSelectionAdorner.cs
- ListControl.cs
- DataGridViewToolTip.cs
- DataServiceConfiguration.cs
- BamlTreeNode.cs
- CalendarTable.cs
- SmtpDigestAuthenticationModule.cs
- FieldInfo.cs
- ResXBuildProvider.cs
- ErrorProvider.cs
- SystemMulticastIPAddressInformation.cs
- regiisutil.cs
- MultiTargetingUtil.cs
- ButtonBaseAutomationPeer.cs