Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Reflection / Emit / AQNBuilder.cs / 4 / AQNBuilder.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== using System; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace System.Reflection.Emit { // TypeNameBuilder is NOT thread safe NOR hardened for OOM NOR reliable internal class TypeNameBuilder { internal enum Format { ToString, FullName, AssemblyQualifiedName, } #region FCalls [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern IntPtr _CreateTypeNameBuilder(); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _ReleaseTypeNameBuilder(IntPtr pAQN); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _OpenGenericArguments(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _CloseGenericArguments(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _OpenGenericArgument(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _CloseGenericArgument(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _AddName(IntPtr tnb, string name); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _AddPointer(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _AddByRef(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _AddSzArray(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _AddArray(IntPtr tnb, int rank); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _AddAssemblySpec(IntPtr tnb, string assemblySpec); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern string _ToString(IntPtr tnb); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void _Clear(IntPtr tnb); #endregion #region Static Members // TypeNameBuilder is NOT thread safe NOR hardened for OOM NOR reliable internal static string ToString(Type type, Format format) { if (format == Format.FullName || format == Format.AssemblyQualifiedName) { if (!type.IsGenericTypeDefinition && type.ContainsGenericParameters) return null; } TypeNameBuilder tnb = new TypeNameBuilder(_CreateTypeNameBuilder()); tnb.Clear(); tnb.ConstructAssemblyQualifiedNameWorker(type, format); string toString = tnb.ToString(); tnb.Dispose(); return toString; } #endregion #region Private Data Members private IntPtr m_typeNameBuilder; #endregion #region Constructor private TypeNameBuilder(IntPtr typeNameBuilder) { m_typeNameBuilder = typeNameBuilder; } internal void Dispose() { _ReleaseTypeNameBuilder(m_typeNameBuilder); } #endregion #region private Members private void AddElementType(Type elementType) { if (elementType.HasElementType) AddElementType(elementType.GetElementType()); if (elementType.IsPointer) AddPointer(); else if (elementType.IsByRef) AddByRef(); else if (elementType.IsSzArray) AddSzArray(); else if (elementType.IsArray) AddArray(elementType.GetArrayRank()); } private void ConstructAssemblyQualifiedNameWorker(Type type, Format format) { Type rootType = type; while (rootType.HasElementType) rootType = rootType.GetElementType(); // Append namespace + nesting + name Listnestings = new List (); for (Type t = rootType; t != null; t = t.IsGenericParameter ? null : t.DeclaringType) nestings.Add(t); for (int i = nestings.Count - 1; i >= 0; i--) { Type enclosingType = nestings[i]; string name = enclosingType.Name; if (i == nestings.Count - 1 && enclosingType.Namespace != null && enclosingType.Namespace.Length != 0) name = enclosingType.Namespace + "." + name; AddName(name); } // Append generic arguments if (rootType.IsGenericType && (!rootType.IsGenericTypeDefinition || format == Format.ToString)) { Type[] genericArguments = rootType.GetGenericArguments(); OpenGenericArguments(); for (int i = 0; i < genericArguments.Length; i++) { Format genericArgumentsFormat = format == Format.FullName ? Format.AssemblyQualifiedName : format; OpenGenericArgument(); ConstructAssemblyQualifiedNameWorker(genericArguments[i], genericArgumentsFormat); CloseGenericArgument(); } CloseGenericArguments(); } // Append pointer, byRef and array qualifiers AddElementType(type); if (format == Format.AssemblyQualifiedName) AddAssemblySpec(type.Module.Assembly.FullName); } private void OpenGenericArguments() { _OpenGenericArguments(m_typeNameBuilder); } private void CloseGenericArguments() { _CloseGenericArguments(m_typeNameBuilder); } private void OpenGenericArgument() { _OpenGenericArgument(m_typeNameBuilder); } private void CloseGenericArgument() { _CloseGenericArgument(m_typeNameBuilder); } private void AddName(string name) { _AddName(m_typeNameBuilder, name); } private void AddPointer() { _AddPointer(m_typeNameBuilder); } private void AddByRef() { _AddByRef(m_typeNameBuilder); } private void AddSzArray() { _AddSzArray(m_typeNameBuilder); } private void AddArray(int rank) { _AddArray(m_typeNameBuilder, rank); } private void AddAssemblySpec(string assemblySpec) { _AddAssemblySpec(m_typeNameBuilder, assemblySpec); } public override string ToString() { return _ToString(m_typeNameBuilder); } private void Clear() { _Clear(m_typeNameBuilder); } #endregion } } // 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
- WebPartConnectionsCloseVerb.cs
- CodeTypeReferenceExpression.cs
- BatchWriter.cs
- DataRelation.cs
- OdbcInfoMessageEvent.cs
- MonitoringDescriptionAttribute.cs
- RuntimeEnvironment.cs
- ReflectPropertyDescriptor.cs
- FormViewUpdateEventArgs.cs
- TransactionsSectionGroup.cs
- CheckedListBox.cs
- WindowClosedEventArgs.cs
- Color.cs
- EntityCommandExecutionException.cs
- ClientFormsIdentity.cs
- FileUpload.cs
- _SSPIWrapper.cs
- ArrayWithOffset.cs
- DiffuseMaterial.cs
- QuaternionRotation3D.cs
- HandleCollector.cs
- ModifiableIteratorCollection.cs
- GestureRecognitionResult.cs
- MergablePropertyAttribute.cs
- ImageSource.cs
- BindingFormattingDialog.cs
- KnownIds.cs
- InfoCardRSAOAEPKeyExchangeDeformatter.cs
- ResourceContainerWrapper.cs
- SqlUserDefinedAggregateAttribute.cs
- PaperSize.cs
- SecurityManager.cs
- PrintDialog.cs
- ButtonRenderer.cs
- MbpInfo.cs
- HtmlContainerControl.cs
- XmlEncoding.cs
- CodeDOMUtility.cs
- cookieexception.cs
- SamlSecurityToken.cs
- XamlInterfaces.cs
- ButtonChrome.cs
- Trace.cs
- SqlUnionizer.cs
- CursorConverter.cs
- ListViewDeleteEventArgs.cs
- Atom10FormatterFactory.cs
- StateManagedCollection.cs
- SqlDataSourceCommandEventArgs.cs
- Filter.cs
- DesignerSelectionListAdapter.cs
- FlowLayout.cs
- SourceFileBuildProvider.cs
- MDIControlStrip.cs
- MailBnfHelper.cs
- RootNamespaceAttribute.cs
- DataTransferEventArgs.cs
- BuildManagerHost.cs
- DataTemplateKey.cs
- ImpersonationContext.cs
- DataSourceCache.cs
- SiteOfOriginContainer.cs
- TableParagraph.cs
- ContractHandle.cs
- ControlHelper.cs
- XmlSchema.cs
- AllMembershipCondition.cs
- UniqueConstraint.cs
- AuthenticationModuleElement.cs
- ManagedWndProcTracker.cs
- PreservationFileReader.cs
- XmlLoader.cs
- TextTreeFixupNode.cs
- ConfigurationSectionGroup.cs
- TrackBarRenderer.cs
- HandlerFactoryWrapper.cs
- GridLengthConverter.cs
- ContentTextAutomationPeer.cs
- PointCollectionConverter.cs
- HtmlFormWrapper.cs
- DrawingGroupDrawingContext.cs
- ThemeDictionaryExtension.cs
- HostedTransportConfigurationBase.cs
- ResXResourceWriter.cs
- DataGridViewCellCancelEventArgs.cs
- PackWebRequest.cs
- CheckBoxFlatAdapter.cs
- ReferencedCollectionType.cs
- RemoteWebConfigurationHostStream.cs
- ProviderMetadata.cs
- RootCodeDomSerializer.cs
- WebPartCollection.cs
- PipelineModuleStepContainer.cs
- DocComment.cs
- ImportContext.cs
- DataViewListener.cs
- MethodCallExpression.cs
- DesignerOptionService.cs
- Clause.cs
- SchemaElementDecl.cs