internal static T CastExportedValue <T>(ICompositionElement element, object?exportedValue) { bool succeeded = ContractServices.TryCast(typeof(T), exportedValue, out object?typedExportedValue); if (!succeeded) { throw new CompositionContractMismatchException(SR.Format( SR.ContractMismatch_ExportedValueCannotBeCastToT, element.DisplayName, typeof(T))); } return((T)typedExportedValue !); }
internal static T CastExportedValue <T>(ICompositionElement element, object exportedValue) { object typedExportedValue = null; bool succeeded = ContractServices.TryCast(typeof(T), exportedValue, out typedExportedValue); if (!succeeded) { throw new CompositionContractMismatchException(string.Format(CultureInfo.CurrentCulture, SR.ContractMismatch_ExportedValueCannotBeCastToT, element.DisplayName, typeof(T))); } return((T)typedExportedValue); }
private object?Cast(Type type, Export export) { object?value = export.Value; if (!ContractServices.TryCast(type, value, out object?result)) { throw new ComposablePartException( SR.Format( SR.ReflectionModel_ImportNotAssignableFromExport, export.ToElement().DisplayName, type.FullName), Definition.ToElement()); } return(result); }
internal static T GetExportedValueFromLazy <T>(Export export) { object exportedValue = export.Value; object typedExportedValue = null; bool succeeded = ContractServices.TryCast(typeof(T), exportedValue, out typedExportedValue); if (!succeeded) { throw new CompositionContractMismatchException(string.Format(CultureInfo.CurrentCulture, Strings.ContractMismatch_ExportedValueCannotBeCastToT, export.ToElement().DisplayName, typeof(T))); } return((T)typedExportedValue); }
private object Cast(Type type, Export export) { // TODO: Need to catch CompositionException to provide // additional information about what member we're setting // and the current dependency graph. object value = export.Value; object result; if (!ContractServices.TryCast(type, value, out result)) { throw new ComposablePartException( String.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_ImportNotAssignableFromExport, export.ToElement().DisplayName, type.FullName), this.Definition.ToElement()); } return(result); }