/// <summary> /// Add metadata provider /// </summary> /// <typeparam name="TMetadataView"></typeparam> /// <param name="provider"></param> #endregion // Documentation public static void RegisterProvider <TMetadataView>(IMetadataViewProvider <TMetadataView> provider) { var type = typeof(TMetadataView); if (s_providers.ContainsKey(type)) { s_providers.Remove(type); } s_providers.Add(type, provider); }
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] #region Documentation /// <summary> /// .NET compact framework does not use reflection in order of converting the metadata into typed instance /// only providers that previously registered are valid /// </summary> /// <typeparam name="TMetadataView"></typeparam> /// <param name="metadata"></param> /// <returns></returns> #endregion // Documentation public static TMetadataView GetMetadataView <TMetadataView>(IDictionary <string, object> metadata) { IMetadataViewProvider <TMetadataView> provider = null; if (s_providers.ContainsKey(typeof(TMetadataView))) { provider = (IMetadataViewProvider <TMetadataView>)s_providers[typeof(TMetadataView)]; } if (provider == null) { throw new CompositionContractMismatchException(string.Format(EXC__MISSING_PROVIDER, typeof(TMetadataView).FullName, typeof(TMetadataView).Name)); } return(provider.GetMetadataView(metadata)); //Type metadataViewType = typeof(TMetadataView); //// If the Metadata dictionary is cast compatible with the passed in type //if (metadataViewType.IsAssignableFrom(typeof(IDictionary<string, object>))) //{ // return (TMetadataView)metadata; //} //// otherwise is it a metadata view //else //{ // Type proxyType; // if (metadataViewType.IsInterface) // { // try // { // proxyType = MetadataViewGenerator.GenerateView(metadataViewType); // } // catch (TypeLoadException ex) // { // throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings.NotSupportedInterfaceMetadataView, metadataViewType.FullName), ex); // } // } // else // { // proxyType = metadataViewType; // } // // Now we have the type for the proxy create it // try // { // return (TMetadataView)Activator.CreateInstance(proxyType, new object[] { metadata }); // } // catch (MissingMethodException ex) // { // // Unable to create an Instance of the Metadata view '{0}' because a constructor could not be selected. Ensure that the type implements a constructor which takes an argument of type IDictionary<string, object>. // throw new CompositionContractMismatchException(string.Format(CultureInfo.CurrentCulture, // Strings.CompositionException_MetadataViewInvalidConstructor, // proxyType.AssemblyQualifiedName), ex); // } // catch (TargetInvocationException ex) // { // //Unwrap known failures that we want to present as CompositionContractMismatchException // if(metadataViewType.IsInterface) // { // if(ex.InnerException.GetType() == typeof(InvalidCastException)) // { // // Unable to create an Instance of the Metadata view {0} because the exporter exported the metadata for the item {1} with the value {2} as type {3} but the view imports it as type {4}. // throw new CompositionContractMismatchException(string.Format(CultureInfo.CurrentCulture, // Strings.ContractMismatch_InvalidCastOnMetadataField, // ex.InnerException.Data[MetadataViewGenerator.MetadataViewType], // ex.InnerException.Data[MetadataViewGenerator.MetadataItemKey], // ex.InnerException.Data[MetadataViewGenerator.MetadataItemValue], // ex.InnerException.Data[MetadataViewGenerator.MetadataItemSourceType], // ex.InnerException.Data[MetadataViewGenerator.MetadataItemTargetType]), ex); // } // else if (ex.InnerException.GetType() == typeof(NullReferenceException)) // { // // Unable to create an Instance of the Metadata view {0} because the exporter exported the metadata for the item {1} with a null value and null is not a valid value for type {2}. // throw new CompositionContractMismatchException(string.Format(CultureInfo.CurrentCulture, // Strings.ContractMismatch_NullReferenceOnMetadataField, // ex.InnerException.Data[MetadataViewGenerator.MetadataViewType], // ex.InnerException.Data[MetadataViewGenerator.MetadataItemKey], // ex.InnerException.Data[MetadataViewGenerator.MetadataItemTargetType]), ex); // } // } // throw; // } //} }