コード例 #1
0
        // Private Methods (1) 

        private static R InvokeGetExportedValueMethod <R>(Expression <Func <CompositionContainer, R> > expr,
                                                          CompositionContainer container,
                                                          Type serviceType,
                                                          string key)
        {
            var method     = (expr.Body as MethodCallExpression).Method;
            var methodName = method.Name;

            var getExportedValueMethods = container.GetType()
                                          .GetMethods()
                                          .Where(m => m.Name == methodName &&
                                                 m.GetGenericArguments().Length == 1);

            object[] @params;
            if (key != null)
            {
                // with key
                @params = new object[] { key };
            }
            else
            {
                @params = new object[0];
            }

            return(GlobalConverter.Current
                   .ChangeType <R>(CollectionHelper.Single(getExportedValueMethods,
                                                           m => m.GetParameters().Length == @params.Length)
                                   .MakeGenericMethod(serviceType)
                                   .Invoke(container, @params)));
        }
コード例 #2
0
        public static dynamic GetExportedType(this CompositionContainer container, Type type)
        {
            try
            {
                if (type.IsGenericType)
                {
                    return
                        (container.GetType()
                         .GetMethod("GetExportedValueOrDefault", new Type[] {})
                         .MakeGenericMethod(type.GetGenericTypeDefinition()
                                            .MakeGenericType(type.GenericTypeArguments))
                         .Invoke(container, new object[] {})?
                         .GetType());
                }

                return(container.GetType().GetMethod("GetExportedValueOrDefault", new Type[] { }).MakeGenericMethod(type).Invoke(container, new object[] { })?.GetType());
            }
            catch (Exception)
            {
                throw;
            }
        }