Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Runtime / Serialization / TypeName.cs / 1 / TypeName.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: TypeName
**
**
** Purpose: Provides some static methods to aid with type loading
** when LoadWithPartialName is used with generics.
**
**
============================================================*/
namespace System.Runtime.Serialization {
using System;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
class TypeName
{
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType((short)0x100), Guid("B81FF171-20F3-11D2-8DCC-00A0C9B00522")]
internal interface ITypeName
{
uint GetNameCount();
uint GetNames([In] uint count, IntPtr rgbszNamesArray);
uint GetTypeArgumentCount();
uint GetTypeArguments([In] uint count, IntPtr rgpArgumentsArray);
uint GetModifierLength();
uint GetModifiers([In] uint count, out uint rgModifiers);
[return: MarshalAs(UnmanagedType.BStr)]
string GetAssemblyName();
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType((short)0x100), Guid("B81FF171-20F3-11D2-8DCC-00A0C9B00521")]
internal interface ITypeNameFactory
{
[return: MarshalAs(UnmanagedType.Interface)]
ITypeName ParseTypeName([In, MarshalAs(UnmanagedType.LPWStr)] string szName, out int pError);
}
private TypeName()
{
}
internal static Type GetType(Assembly initialAssembly, string fullTypeName)
{
Type comType = Type.GetTypeFromCLSID(new Guid(0xB81FF171, 0x20F3, 0x11d2, 0x8d, 0xcc, 0x00, 0xa0, 0xc9, 0xb0, 0x05, 0x25));
ITypeNameFactory parser = (ITypeNameFactory)Activator.CreateInstance(comType);
int error;
ITypeName typeNameInfo = parser.ParseTypeName(fullTypeName, out error);
Type t = null;
if (error == -1)
{
t = LoadTypeWithPartialName(typeNameInfo, initialAssembly, fullTypeName);
}
return t;
}
static Type LoadTypeWithPartialName(ITypeName typeNameInfo, Assembly initialAssembly, string fullTypeName)
{
uint nameCount = typeNameInfo.GetNameCount();
uint argCount = typeNameInfo.GetTypeArgumentCount();
IntPtr[] names = new IntPtr[nameCount];
IntPtr[] args = new IntPtr[argCount];
try
{
Type baseType = null;
if (nameCount != 0)
{
GCHandle gch = GCHandle.Alloc(names, GCHandleType.Pinned);
nameCount = typeNameInfo.GetNames(nameCount, gch.AddrOfPinnedObject());
gch.Free();
string name = Marshal.PtrToStringBSTR(names[0]);
string asmName = typeNameInfo.GetAssemblyName();
if (!String.IsNullOrEmpty(asmName))
{
Assembly asm = Assembly.LoadWithPartialName(asmName);
if (asm == null)
{
asm = Assembly.LoadWithPartialName(new AssemblyName(asmName).Name);
}
baseType = asm.GetType(name);
}
else
{
if (initialAssembly != null)
{
baseType = initialAssembly.GetType(name);
}
else
baseType = Type.GetType(name);
}
if (baseType == null)
{
throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), fullTypeName));
}
for (int i = 1; i < nameCount; i++)
{
string nestedName = Marshal.PtrToStringBSTR(names[i]);
baseType = baseType.GetNestedType(nestedName, BindingFlags.Public | BindingFlags.NonPublic);
if (baseType == null)
throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), fullTypeName));
}
}
else
{
throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), fullTypeName));
}
if (argCount != 0)
{
GCHandle gch = GCHandle.Alloc(args, GCHandleType.Pinned);
argCount = typeNameInfo.GetTypeArguments(argCount, gch.AddrOfPinnedObject());
gch.Free();
Type[] typeArgs = new Type[argCount];
for (int i = 0; i < argCount; i++)
{
typeArgs[i] = LoadTypeWithPartialName((ITypeName)Marshal.GetObjectForIUnknown(args[i]), null, fullTypeName);
}
return baseType.MakeGenericType(typeArgs);
}
else
{
return baseType;
}
}
finally
{
for (int i = 0; i < names.Length; i++)
{
if (names[i] != null)
{
Marshal.FreeBSTR(names[i]);
}
}
for (int i = 0; i < args.Length; i++)
{
if (args[i] != null)
{
Marshal.Release(args[i]);
}
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: TypeName
**
**
** Purpose: Provides some static methods to aid with type loading
** when LoadWithPartialName is used with generics.
**
**
============================================================*/
namespace System.Runtime.Serialization {
using System;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
class TypeName
{
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType((short)0x100), Guid("B81FF171-20F3-11D2-8DCC-00A0C9B00522")]
internal interface ITypeName
{
uint GetNameCount();
uint GetNames([In] uint count, IntPtr rgbszNamesArray);
uint GetTypeArgumentCount();
uint GetTypeArguments([In] uint count, IntPtr rgpArgumentsArray);
uint GetModifierLength();
uint GetModifiers([In] uint count, out uint rgModifiers);
[return: MarshalAs(UnmanagedType.BStr)]
string GetAssemblyName();
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), TypeLibType((short)0x100), Guid("B81FF171-20F3-11D2-8DCC-00A0C9B00521")]
internal interface ITypeNameFactory
{
[return: MarshalAs(UnmanagedType.Interface)]
ITypeName ParseTypeName([In, MarshalAs(UnmanagedType.LPWStr)] string szName, out int pError);
}
private TypeName()
{
}
internal static Type GetType(Assembly initialAssembly, string fullTypeName)
{
Type comType = Type.GetTypeFromCLSID(new Guid(0xB81FF171, 0x20F3, 0x11d2, 0x8d, 0xcc, 0x00, 0xa0, 0xc9, 0xb0, 0x05, 0x25));
ITypeNameFactory parser = (ITypeNameFactory)Activator.CreateInstance(comType);
int error;
ITypeName typeNameInfo = parser.ParseTypeName(fullTypeName, out error);
Type t = null;
if (error == -1)
{
t = LoadTypeWithPartialName(typeNameInfo, initialAssembly, fullTypeName);
}
return t;
}
static Type LoadTypeWithPartialName(ITypeName typeNameInfo, Assembly initialAssembly, string fullTypeName)
{
uint nameCount = typeNameInfo.GetNameCount();
uint argCount = typeNameInfo.GetTypeArgumentCount();
IntPtr[] names = new IntPtr[nameCount];
IntPtr[] args = new IntPtr[argCount];
try
{
Type baseType = null;
if (nameCount != 0)
{
GCHandle gch = GCHandle.Alloc(names, GCHandleType.Pinned);
nameCount = typeNameInfo.GetNames(nameCount, gch.AddrOfPinnedObject());
gch.Free();
string name = Marshal.PtrToStringBSTR(names[0]);
string asmName = typeNameInfo.GetAssemblyName();
if (!String.IsNullOrEmpty(asmName))
{
Assembly asm = Assembly.LoadWithPartialName(asmName);
if (asm == null)
{
asm = Assembly.LoadWithPartialName(new AssemblyName(asmName).Name);
}
baseType = asm.GetType(name);
}
else
{
if (initialAssembly != null)
{
baseType = initialAssembly.GetType(name);
}
else
baseType = Type.GetType(name);
}
if (baseType == null)
{
throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), fullTypeName));
}
for (int i = 1; i < nameCount; i++)
{
string nestedName = Marshal.PtrToStringBSTR(names[i]);
baseType = baseType.GetNestedType(nestedName, BindingFlags.Public | BindingFlags.NonPublic);
if (baseType == null)
throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), fullTypeName));
}
}
else
{
throw new SerializationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), fullTypeName));
}
if (argCount != 0)
{
GCHandle gch = GCHandle.Alloc(args, GCHandleType.Pinned);
argCount = typeNameInfo.GetTypeArguments(argCount, gch.AddrOfPinnedObject());
gch.Free();
Type[] typeArgs = new Type[argCount];
for (int i = 0; i < argCount; i++)
{
typeArgs[i] = LoadTypeWithPartialName((ITypeName)Marshal.GetObjectForIUnknown(args[i]), null, fullTypeName);
}
return baseType.MakeGenericType(typeArgs);
}
else
{
return baseType;
}
}
finally
{
for (int i = 0; i < names.Length; i++)
{
if (names[i] != null)
{
Marshal.FreeBSTR(names[i]);
}
}
for (int i = 0; i < args.Length; i++)
{
if (args[i] != null)
{
Marshal.Release(args[i]);
}
}
}
}
}
}
// 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
- ApplicationException.cs
- ButtonBaseAdapter.cs
- ContextMenuStripActionList.cs
- CompilerWrapper.cs
- StylusPlugin.cs
- ReadOnlyHierarchicalDataSourceView.cs
- Dynamic.cs
- Parsers.cs
- MethodBody.cs
- DetailsViewRow.cs
- ToolBarButton.cs
- StateMachineSubscriptionManager.cs
- DocumentScope.cs
- KnownTypesProvider.cs
- ContentValidator.cs
- ViewKeyConstraint.cs
- DesignerCapabilities.cs
- BrowserCapabilitiesFactory.cs
- MailFileEditor.cs
- TableLayoutPanelCellPosition.cs
- XsltException.cs
- TextContainer.cs
- BrowserCapabilitiesFactoryBase.cs
- RootCodeDomSerializer.cs
- EntityContainerRelationshipSet.cs
- Triplet.cs
- DrawListViewSubItemEventArgs.cs
- prefixendpointaddressmessagefiltertable.cs
- SQLSingleStorage.cs
- DataGridViewCellMouseEventArgs.cs
- PanningMessageFilter.cs
- WindowsListView.cs
- Divide.cs
- DataGridViewCellValidatingEventArgs.cs
- TextEditorSpelling.cs
- PropertyEmitter.cs
- PropertyFilterAttribute.cs
- DES.cs
- AnimationClock.cs
- DataGridBoolColumn.cs
- LinqDataSource.cs
- ServiceSecurityAuditElement.cs
- Base64Encoder.cs
- dataprotectionpermission.cs
- BaseCodePageEncoding.cs
- DeferredTextReference.cs
- JapaneseLunisolarCalendar.cs
- QilChoice.cs
- Property.cs
- TimeStampChecker.cs
- FormatException.cs
- BindingExpressionUncommonField.cs
- Line.cs
- PagedDataSource.cs
- ScalarConstant.cs
- Section.cs
- MarshalDirectiveException.cs
- DataService.cs
- FixedSchema.cs
- SecurityState.cs
- ClusterSafeNativeMethods.cs
- SizeChangedInfo.cs
- SmiMetaData.cs
- ExplicitDiscriminatorMap.cs
- SerializerDescriptor.cs
- SwitchAttribute.cs
- ErrorHandlingReceiver.cs
- FontFamily.cs
- StringFreezingAttribute.cs
- AccessDataSource.cs
- unsafenativemethodsother.cs
- PaginationProgressEventArgs.cs
- QueryOpeningEnumerator.cs
- Matrix3DValueSerializer.cs
- VideoDrawing.cs
- connectionpool.cs
- _SSPISessionCache.cs
- _KerberosClient.cs
- DoubleUtil.cs
- ObjectStorage.cs
- SynchronizationScope.cs
- ListViewTableCell.cs
- ZipIOModeEnforcingStream.cs
- DataGridViewIntLinkedList.cs
- TreeViewCancelEvent.cs
- ResourceReader.cs
- EntityDataSourceDataSelection.cs
- SapiRecognizer.cs
- EntityCommand.cs
- AnonymousIdentificationSection.cs
- RangeValidator.cs
- InvalidEnumArgumentException.cs
- MailMessageEventArgs.cs
- StaticExtension.cs
- EntityParameterCollection.cs
- BasicDesignerLoader.cs
- XmlLoader.cs
- EventLogPermissionEntryCollection.cs
- MessagePartDescription.cs
- CapiSafeHandles.cs