コード例 #1
0
ファイル: ExportServices.cs プロジェクト: pedrobsaila/runtime
        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 !);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: ImportingItem.cs プロジェクト: pmq20/mono_forked
        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);
        }