Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Tools / comsvcutil / ComplusTypeValidator.cs / 1305376 / ComplusTypeValidator.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace Microsoft.Tools.ServiceModel.ComSvcConfig { using System; using System.ServiceModel.Channels; using System.Globalization; using System.EnterpriseServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.ServiceModel; using System.Reflection; using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; using Microsoft.Tools.ServiceModel; using Microsoft.Tools.ServiceModel.SvcUtil; static class ComPlusTypeValidator { static Guid IID_Object = new Guid("{65074F7F-63C0-304E-AF0A-D51741CB4A8D}"); static Guid IID_IDisposable = new Guid("{805D7A98-D4AF-3F0F-967F-E5CF45312D2C}"); static Guid IID_IManagedObject = new Guid("{C3FCC19E-A970-11D2-8B5A-00A0C9B7C9C4}"); static Guid IID_IProcessInitializer = new Guid("{1113F52D-DC7F-4943-AED6-88D04027E32A}"); static Guid IID_IRemoteDispatch = new Guid("{6619A740-8154-43BE-A186-0319578E02DB}"); static Guid IID_IServicedComponentInfo = new Guid("{8165B19E-8D3A-4D0B-80C8-97DE310DB583}"); static Guid IID_IComponentRegistrar = new Guid ("{A817E7A2-43FA-11D0-9E44-00AA00B6770A}"); static internal bool DerivesFromServicedComponent(Guid iid, Guid clsid) { Type typeOfInterfaceResolver = typeof(Message).Assembly.GetType("System.ServiceModel.ComIntegration.TypeCacheManager"); object resolver = typeOfInterfaceResolver.InvokeMember("Provider", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty, null, null, null, CultureInfo.InvariantCulture); object[] args = new object[3] { iid, false, true }; Assembly asm = null; try { asm = typeOfInterfaceResolver.InvokeMember("ResolveAssemblyFromIID", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, resolver, args, CultureInfo.InvariantCulture) as Assembly; } catch (TargetInvocationException e) { if (e.GetBaseException() is System.IO.FileNotFoundException) throw; Console.WriteLine(iid + ", " + clsid); return false; } bool inherits = false; foreach (Type t in asm.GetTypes()) { if (t.GUID == clsid) { Type baseType = t.BaseType; while (null != baseType) { if (typeof(ServicedComponent) == baseType) { inherits = true; break; } baseType = baseType.BaseType; } } } return inherits; } static internal bool VerifyInterface(ComAdminInterfaceInfo interfaceInfo, bool allowReferences, Guid clsid) { return VerifyInterface(interfaceInfo, allowReferences, clsid, false); } static internal bool VerifyInterface(ComAdminInterfaceInfo interfaceInfo, bool allowReferences, Guid clsid, bool produceError) { if (IsInternalInterface (interfaceInfo.Iid)) { if(produceError) ToolConsole.WriteError(SR.GetString(SR.IsInternalInterfaceAndCannotBeExposedOverWebService, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name), ""); else ToolConsole.WriteWarning (SR.GetString (SR.IsInternalInterfaceAndCannotBeExposedOverWebService, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name)); return false; } Type typeOfInterfaceResolver = typeof(Message).Assembly.GetType("System.ServiceModel.ComIntegration.TypeCacheManager"); object resolver = typeOfInterfaceResolver.InvokeMember("Provider", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty, null, null, null, CultureInfo.InvariantCulture); object [] args = new object [1] { interfaceInfo.Iid}; Type typeOfInterface = null; try { typeOfInterface = typeOfInterfaceResolver.InvokeMember ("VerifyType", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, resolver, args, CultureInfo.InvariantCulture ) as Type; } catch (TargetInvocationException e) { if (e.GetBaseException() is System.IO.FileNotFoundException) throw CreateDescriptiveException((System.IO.FileNotFoundException)e.GetBaseException()); string exceptionMessage = SR.GetString(SR.TypeResolutionForInterfaceFailed, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, e.InnerException.Message); if (DerivesFromServicedComponent(interfaceInfo.Iid, clsid)) exceptionMessage += " " + SR.GetString(SR.ClassInterfacesNotSupported); if (produceError) ToolConsole.WriteError(exceptionMessage, ""); else ToolConsole.WriteNonVerboseWarning(exceptionMessage); return false; } MethodInfo [] methods = typeOfInterface.GetMethods (); if (methods.Length == 0) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.InterfaceHasNoMethods, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name), ""); else ToolConsole.WriteNonVerboseWarning(SR.GetString(SR.InterfaceHasNoMethods, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name)); return false; } string typeMismatchDetails; foreach (MethodInfo method in methods) { foreach (ParameterInfo parameter in method.GetParameters()) { Type typeOfParam = parameter.ParameterType; if (typeOfParam.IsByRef) typeOfParam = typeOfParam.GetElementType(); if (!IsValidParameter(typeOfParam, parameter, allowReferences, out typeMismatchDetails)) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.ParameterOfMethodInInterfaceHasANonCompliantType, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, method.Name, parameter.Name, typeMismatchDetails), ""); else ToolConsole.WriteNonVerboseWarning (SR.GetString(SR.ParameterOfMethodInInterfaceHasANonCompliantType, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, method.Name, parameter.Name, typeMismatchDetails)); return false; } } if (!IsValidParameter(method.ReturnType, method.ReturnTypeCustomAttributes, allowReferences, out typeMismatchDetails)) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.InvalidWebServiceReturnValue, method.ReturnType.Name, method.Name, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, typeMismatchDetails), ""); else ToolConsole.WriteNonVerboseWarning(SR.GetString(SR.InvalidWebServiceReturnValue, method.ReturnType.Name, method.Name, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, typeMismatchDetails)); return false; } } return true; } public static ListFetchAllMethodsForInterface(ComAdminInterfaceInfo interfaceInfo) { return FetchAllMethodsForInterface(interfaceInfo, true); } public static List FetchAllMethodsForInterface (ComAdminInterfaceInfo interfaceInfo, bool produceWarning) { Type typeOfInterfaceResolver = typeof(Message).Assembly.GetType("System.ServiceModel.ComIntegration.TypeCacheManager"); object resolver = typeOfInterfaceResolver.InvokeMember("Provider", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty, null, null, null, CultureInfo.InvariantCulture); object [] args = new object [1] { interfaceInfo.Iid}; Type typeOfInterface = null; try { typeOfInterface = typeOfInterfaceResolver.InvokeMember ("VerifyType", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, resolver, args, CultureInfo.InvariantCulture ) as Type; } catch (TargetInvocationException e) { if (e.GetBaseException() is System.IO.FileNotFoundException) throw CreateDescriptiveException((System.IO.FileNotFoundException)e.GetBaseException()); if(produceWarning) ToolConsole.WriteWarning (SR.GetString (SR.TypeResolutionForInterfaceFailed, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name, e.InnerException.Message)); return null; } MethodInfo [] methods = typeOfInterface.GetMethods (); if (methods.Length == 0) return null; else { List methodNames = new List (); foreach (MethodBase method in methods) methodNames.Add (method.Name); return methodNames; } } static Exception CreateDescriptiveException(System.IO.FileNotFoundException oldException) { return new Exception(oldException.Message + " " + SR.GetString(SR.InstallInGAC, oldException.FileName)); } public static bool VerifyInterfaceMethods(ComAdminInterfaceInfo interfaceInfo, IList methodNames, bool allowReferences, bool produceError) { if (IsInternalInterface (interfaceInfo.Iid)) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.IsInternalInterfaceAndCannotBeExposedOverWebService, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name), ""); else ToolConsole.WriteWarning (SR.GetString (SR.IsInternalInterfaceAndCannotBeExposedOverWebService, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name)); return false; } Type typeOfInterfaceResolver = typeof(Message).Assembly.GetType("System.ServiceModel.ComIntegration.TypeCacheManager"); object resolver = typeOfInterfaceResolver.InvokeMember("Provider", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty, null, null, null, CultureInfo.InvariantCulture); object [] args = new object [1] { interfaceInfo.Iid}; Type typeOfInterface = null; try { typeOfInterface = typeOfInterfaceResolver.InvokeMember ("VerifyType", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, resolver, args, CultureInfo.InvariantCulture ) as Type; } catch (TargetInvocationException e) { if (e.GetBaseException() is System.IO.FileNotFoundException) throw CreateDescriptiveException((System.IO.FileNotFoundException)e.GetBaseException()); if (produceError) ToolConsole.WriteError(SR.GetString(SR.TypeResolutionForInterfaceFailed, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, e.InnerException.Message), ""); else ToolConsole.WriteWarning (SR.GetString (SR.TypeResolutionForInterfaceFailed, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name, e.InnerException.Message)); return false; } MethodInfo [] methods = typeOfInterface.GetMethods (); if (methods.Length == 0) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.InterfaceHasNoMethods, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name), ""); else ToolConsole.WriteWarning (SR.GetString (SR.InterfaceHasNoMethods, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name)); return false; } string typeMismatchDetails; bool found = false; foreach (string methodName in methodNames) { found = false; foreach (MethodInfo method in methods) { if (methodName == method.Name) { found = true; foreach (ParameterInfo parameter in method.GetParameters ()) { Type typeOfParam = parameter.ParameterType; if (typeOfParam.IsByRef) typeOfParam = typeOfParam.GetElementType(); if (!IsValidParameter(typeOfParam, parameter, allowReferences, out typeMismatchDetails)) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.ParameterOfMethodInInterfaceHasANonCompliantType, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, method.Name, parameter.Name, typeMismatchDetails), ""); else ToolConsole.WriteWarning (SR.GetString(SR.ParameterOfMethodInInterfaceHasANonCompliantType, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name, method.Name, parameter.Name, typeMismatchDetails)); return false; } } if (!IsValidParameter(method.ReturnType, method.ReturnTypeCustomAttributes, allowReferences, out typeMismatchDetails)) { if (produceError) ToolConsole.WriteError(SR.GetString(SR.InvalidWebServiceReturnValue, method.ReturnType.Name, method.Name, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, typeMismatchDetails), ""); else ToolConsole.WriteWarning(SR.GetString(SR.InvalidWebServiceReturnValue, method.ReturnType.Name, method.Name, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString("B") : interfaceInfo.Name, typeMismatchDetails)); return false; } } } if (!found) { throw Tool.CreateException (SR.GetString(SR.MethodNotFoundOnInterface, Tool.Options.ShowGuids ? interfaceInfo.Iid.ToString ("B") : interfaceInfo.Name, methodName), null); } } return true; } public static bool IsInternalInterface (Guid iid) { if (iid == IID_Object || iid == IID_IDisposable || iid == IID_IManagedObject || iid == IID_IProcessInitializer || iid == IID_IRemoteDispatch || iid == IID_IServicedComponentInfo || iid == IID_IComponentRegistrar || iid.ToString("D").ToLowerInvariant().EndsWith("c000-000000000046", StringComparison.Ordinal)) //other ole/com standard interfaces { return true; } return false; } public static bool IsValidParameter(Type type, ICustomAttributeProvider attributeProvider, bool allowReferences, out string typeMismatchDetails) { typeMismatchDetails = type.ToString () + " " ; object[] attributes = attributeProvider.GetCustomAttributes(typeof(MarshalAsAttribute), true); foreach (MarshalAsAttribute attr in attributes) { UnmanagedType marshalAs = attr.Value; if (marshalAs == UnmanagedType.IDispatch || marshalAs == UnmanagedType.Interface || marshalAs == UnmanagedType.IUnknown) { if(!allowReferences) typeMismatchDetails += SR.GetString (SR.HasMarshalAsAttributeOfType, marshalAs); return allowReferences; } } XsdDataContractExporter exporter = new XsdDataContractExporter(); if (!exporter.CanExport(type)) { typeMismatchDetails += SR.GetString (SR.CannotBeExportedByDataContractExporter); return false; } return true; } } } // 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
- SqlParameterCollection.cs
- DesignSurface.cs
- HostedTransportConfigurationManager.cs
- Calendar.cs
- MapPathBasedVirtualPathProvider.cs
- MetadataProperty.cs
- CorrelationManager.cs
- DeleteCardRequest.cs
- GridItem.cs
- UserNamePasswordClientCredential.cs
- ContainerUIElement3D.cs
- ColumnResult.cs
- DomainUpDown.cs
- RequestTimeoutManager.cs
- TableLayoutStyle.cs
- LookupNode.cs
- CroppedBitmap.cs
- DataGridItemEventArgs.cs
- ComponentManagerBroker.cs
- ListView.cs
- PackageDigitalSignatureManager.cs
- ZipIOExtraFieldElement.cs
- ToolStripLabel.cs
- DrawingBrush.cs
- ActivationService.cs
- ExpressionBuilder.cs
- HtmlTextArea.cs
- WebPartTracker.cs
- TextDecorationLocationValidation.cs
- DbProviderManifest.cs
- ContextMenuStrip.cs
- LocatorPart.cs
- CreateParams.cs
- StatusCommandUI.cs
- QueryAccessibilityHelpEvent.cs
- WebConfigurationHostFileChange.cs
- FormView.cs
- UiaCoreProviderApi.cs
- SecurityElement.cs
- MenuBindingsEditorForm.cs
- AdapterUtil.cs
- messageonlyhwndwrapper.cs
- CrossAppDomainChannel.cs
- Variant.cs
- IntPtr.cs
- PrintControllerWithStatusDialog.cs
- ToolStripDropDownButton.cs
- Array.cs
- PropertyTabChangedEvent.cs
- GeometryValueSerializer.cs
- WindowInteropHelper.cs
- StructuralCache.cs
- XmlQueryRuntime.cs
- safelink.cs
- Opcode.cs
- CountdownEvent.cs
- MemoryPressure.cs
- ZoneLinkButton.cs
- LinkedResourceCollection.cs
- SqlDataSource.cs
- FormsAuthenticationTicket.cs
- GroupBoxRenderer.cs
- ListBase.cs
- HyperLinkColumn.cs
- PreviewKeyDownEventArgs.cs
- MailMessage.cs
- AvtEvent.cs
- Compiler.cs
- BevelBitmapEffect.cs
- XmlSchemaElement.cs
- Block.cs
- HtmlLink.cs
- ItemCheckEvent.cs
- Dynamic.cs
- DataGridAutoGeneratingColumnEventArgs.cs
- AffineTransform3D.cs
- CompoundFileStreamReference.cs
- AllMembershipCondition.cs
- UmAlQuraCalendar.cs
- RuleSetDialog.cs
- SetterBase.cs
- StrongNamePublicKeyBlob.cs
- DiscoveryUtility.cs
- AppModelKnownContentFactory.cs
- GetWinFXPath.cs
- UriSectionReader.cs
- DynamicILGenerator.cs
- Item.cs
- LinqDataView.cs
- WmpBitmapEncoder.cs
- SortDescription.cs
- SimpleType.cs
- XpsDocument.cs
- ThicknessAnimationUsingKeyFrames.cs
- ComNativeDescriptor.cs
- Transactions.cs
- CodeAccessPermission.cs
- ValueQuery.cs
- CaseStatementSlot.cs
- LinkLabel.cs