Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / ComIntegration / TypeLibraryHelper.cs / 1 / TypeLibraryHelper.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.ComIntegration
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.Win32;
using System.Reflection;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using System.ServiceModel.Diagnostics;
internal sealed class TypeLibraryHelper
{
internal static Assembly GenerateAssemblyFromNativeTypeLibrary (Guid iid, Guid typeLibraryID, ITypeLib typeLibrary)
{
TypeLibraryHelper helper = GetHelperInstance ();
try
{
return helper.GenerateAssemblyFromNativeTypeLibInternal (iid, typeLibraryID, typeLibrary);
}
finally
{
ReleaseHelperInstance ();
}
}
private static object instanceLock = new object ();
private static TypeLibraryHelper instance;
private static int instanceCount = 0;
private static TypeLibraryHelper GetHelperInstance ()
{
lock (instanceLock)
{
if (instance == null)
{
TypeLibraryHelper tlhTemp = new TypeLibraryHelper();
Thread.MemoryBarrier();
instance = tlhTemp;
}
}
Interlocked.Increment (ref instanceCount);
return instance;
}
private static void ReleaseHelperInstance ()
{
if (0 == Interlocked.Decrement (ref instanceCount))
instance = null;
}
internal class ConversionEventHandler : ITypeLibImporterNotifySink
{
Guid iid;
Guid typeLibraryID;
public ConversionEventHandler (Guid iid, Guid typeLibraryID)
{
this.iid = iid;
this.typeLibraryID = typeLibraryID;
}
void ITypeLibImporterNotifySink.ReportEvent( ImporterEventKind eventKind, int eventCode, string eventMsg )
{
ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTLBImportConverterEvent,
SR.TraceCodeComIntegrationTLBImportConverterEvent, iid, typeLibraryID, eventKind, eventCode, eventMsg);
}
Assembly ITypeLibImporterNotifySink.ResolveRef( object typeLib )
{
ITypeLib tlb = typeLib as ITypeLib;
IntPtr ptr = IntPtr.Zero;
try
{
tlb.GetLibAttr(out ptr);
System.Runtime.InteropServices.ComTypes.TYPELIBATTR attr = (System.Runtime.InteropServices.ComTypes.TYPELIBATTR)Marshal.PtrToStructure(ptr, typeof(System.Runtime.InteropServices.ComTypes.TYPELIBATTR));
return TypeLibraryHelper.GenerateAssemblyFromNativeTypeLibrary (iid, attr.guid, typeLib as ITypeLib);
}
finally
{
if ((ptr != IntPtr.Zero) && (tlb != null))
tlb.ReleaseTLibAttr (ptr);
}
}
}
TypeLibConverter TypelibraryConverter = new TypeLibConverter();
Dictionary TypelibraryAssembly = new Dictionary ();
private string GetRandomName ()
{
Guid guid = Guid.NewGuid ();
String strGuid = guid.ToString ();
return strGuid.Replace ('-','_');
}
private Assembly GenerateAssemblyFromNativeTypeLibInternal (Guid iid, Guid typeLibraryID, ITypeLib typeLibrary)
{
Assembly asm = null;
try
{
lock (this)
{
TypelibraryAssembly.TryGetValue(typeLibraryID, out asm);
if (asm == null)
{
string assemblyName = "";
string notused1 = "";
string notused2 = "";
int notused3;
string namespaceName;
typeLibrary.GetDocumentation(-1, out namespaceName, out notused1, out notused3, out notused2);
if (String.IsNullOrEmpty(namespaceName))
{
DiagnosticUtility.DebugAssert("Assembly cannot be null");
throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(true);
}
assemblyName = String.Concat(namespaceName, GetRandomName(), ".dll");
asm = TypelibraryConverter.ConvertTypeLibToAssembly(typeLibrary, assemblyName, TypeLibImporterFlags.SerializableValueClasses, new ConversionEventHandler(iid, typeLibraryID), null, null, namespaceName, null);
TypelibraryAssembly[typeLibraryID] = asm;
}
}
}
catch (ReflectionTypeLoadException )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError (new InvalidOperationException (SR.GetString (SR.FailedToConvertTypelibraryToAssembly)));
}
if (asm == null)
{
DiagnosticUtility.DebugAssert("Assembly cannot be null");
throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(true);
}
return asm;
}
}
}
// 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
- XmlQuerySequence.cs
- TCEAdapterGenerator.cs
- BindableAttribute.cs
- UrlPropertyAttribute.cs
- ListViewPagedDataSource.cs
- TimeZone.cs
- ConnectionManagementSection.cs
- SmiEventSink_DeferedProcessing.cs
- SerializerDescriptor.cs
- ChtmlTextWriter.cs
- SqlNotificationEventArgs.cs
- RedistVersionInfo.cs
- InplaceBitmapMetadataWriter.cs
- SBCSCodePageEncoding.cs
- MatrixCamera.cs
- MimeReturn.cs
- ArgumentValueSerializer.cs
- MessageAction.cs
- X509Utils.cs
- RangeValidator.cs
- RectangleHotSpot.cs
- ServiceObjectContainer.cs
- ScopeCompiler.cs
- XmlDataSourceView.cs
- SelectedGridItemChangedEvent.cs
- ResourceExpressionBuilder.cs
- RowSpanVector.cs
- DiscoveryMessageSequence.cs
- InvokeSchedule.cs
- TypographyProperties.cs
- ParameterBuilder.cs
- Transform.cs
- DocumentApplicationJournalEntry.cs
- PolicyStatement.cs
- TextTreeTextBlock.cs
- IsolatedStorageFileStream.cs
- CompensationHandlingFilter.cs
- MSAAEventDispatcher.cs
- RoutedUICommand.cs
- ActivityTypeResolver.xaml.cs
- BaseTemplateBuildProvider.cs
- TextElement.cs
- List.cs
- GroupQuery.cs
- AnnotationStore.cs
- GetPageCompletedEventArgs.cs
- EventHandlingScope.cs
- TraceData.cs
- AmbientEnvironment.cs
- SourceLocationProvider.cs
- SystemFonts.cs
- ActivatableWorkflowsQueryResult.cs
- SerializationFieldInfo.cs
- TimelineClockCollection.cs
- RichTextBoxConstants.cs
- HTTP_SERVICE_CONFIG_URLACL_PARAM.cs
- SamlAssertionDirectKeyIdentifierClause.cs
- LowerCaseStringConverter.cs
- WriteableBitmap.cs
- LOSFormatter.cs
- EnumConverter.cs
- PropertyDescriptorCollection.cs
- BitmapImage.cs
- ObfuscateAssemblyAttribute.cs
- MatrixConverter.cs
- TableCell.cs
- Proxy.cs
- AuthenticationModuleElementCollection.cs
- RightsManagementEncryptionTransform.cs
- DataGridTable.cs
- WsdlParser.cs
- hebrewshape.cs
- ListViewInsertedEventArgs.cs
- MethodResolver.cs
- DrawListViewSubItemEventArgs.cs
- CreateUserErrorEventArgs.cs
- XmlAnyElementAttributes.cs
- XPathSingletonIterator.cs
- RuntimeWrappedException.cs
- VirtualPathProvider.cs
- XMLUtil.cs
- ScopelessEnumAttribute.cs
- ProgressiveCrcCalculatingStream.cs
- WebResourceAttribute.cs
- DataGridHelper.cs
- XmlSchemaComplexContentRestriction.cs
- RoleBoolean.cs
- XmlObjectSerializerWriteContextComplex.cs
- LinqDataSourceView.cs
- ListView.cs
- SaveWorkflowAsyncResult.cs
- XmlEntityReference.cs
- DistinctQueryOperator.cs
- CloudCollection.cs
- EntitySetDataBindingList.cs
- ClientScriptManager.cs
- XmlILAnnotation.cs
- SpeechSynthesizer.cs
- RelationshipConverter.cs
- StackSpiller.Bindings.cs