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
- DisplayNameAttribute.cs
- SourceFilter.cs
- FacetDescriptionElement.cs
- AsyncResult.cs
- ReachDocumentSequenceSerializer.cs
- SqlVersion.cs
- RadioButtonStandardAdapter.cs
- mediaeventargs.cs
- RootBrowserWindow.cs
- sqlser.cs
- GridItemCollection.cs
- IconEditor.cs
- CreateDataSourceDialog.cs
- XpsViewerException.cs
- _UriSyntax.cs
- MissingManifestResourceException.cs
- HttpHandlersSection.cs
- DebugTrace.cs
- BufferModesCollection.cs
- GroupQuery.cs
- ProfileProvider.cs
- VarInfo.cs
- Constants.cs
- HtmlTable.cs
- DataTableCollection.cs
- SetStoryboardSpeedRatio.cs
- XmlUrlResolver.cs
- ObjectDataSourceChooseTypePanel.cs
- UserControlBuildProvider.cs
- TypeInitializationException.cs
- TraceLog.cs
- SerializerWriterEventHandlers.cs
- CoreSwitches.cs
- ToolBarButtonClickEvent.cs
- OrthographicCamera.cs
- Scene3D.cs
- Privilege.cs
- CmsUtils.cs
- GridViewSelectEventArgs.cs
- DisableDpiAwarenessAttribute.cs
- StylusPointPropertyInfo.cs
- IntellisenseTextBox.designer.cs
- BackgroundWorker.cs
- WebBrowserBase.cs
- DBParameter.cs
- SafeFileMappingHandle.cs
- PrintController.cs
- GAC.cs
- DateTimeConverter.cs
- DuplicateWaitObjectException.cs
- ChannelBase.cs
- FileEnumerator.cs
- TypeSource.cs
- DBConnection.cs
- ColumnHeader.cs
- DataGridViewColumnHeaderCell.cs
- MessageQueueEnumerator.cs
- ParameterReplacerVisitor.cs
- XmlEncoding.cs
- WeakReference.cs
- ColorAnimation.cs
- Menu.cs
- IisHelper.cs
- DataListGeneralPage.cs
- XhtmlBasicControlAdapter.cs
- Sequence.cs
- SessionState.cs
- ClonableStack.cs
- MulticastOption.cs
- SubpageParagraph.cs
- UnsafeNativeMethods.cs
- VisualCollection.cs
- SubclassTypeValidator.cs
- Sql8ConformanceChecker.cs
- ProfileSettings.cs
- UserPreferenceChangedEventArgs.cs
- CorrelationManager.cs
- ResXDataNode.cs
- TdsValueSetter.cs
- HtmlInputSubmit.cs
- Line.cs
- BuilderInfo.cs
- PeerIPHelper.cs
- RuntimeEnvironment.cs
- Dynamic.cs
- RelOps.cs
- DataExpression.cs
- ScriptingScriptResourceHandlerSection.cs
- ListViewAutomationPeer.cs
- ImageListImageEditor.cs
- TextBoxView.cs
- WebControl.cs
- StringValueConverter.cs
- CompiledQuery.cs
- EventTask.cs
- Hyperlink.cs
- ServiceChannel.cs
- TraceHwndHost.cs
- ErrorBehavior.cs
- BitmapEncoder.cs