Exemplo n.º 1
0
        protected override object GetArgumentRquestAndConvert(Type parameterType, Type requestedType, ICustomAttributeProvider info, bool isOptional)
        {
            OnRequestByType(requestedType);
            var suggested     = info.Attribute2 <DefaultImplAttribute>();
            var suggestedType = suggested == null ? null : suggested.TargetType;

            if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(Lazy <>))
            {
                if (suggestedType != null)
                {
                    return(string.Format("new Lazy<{0}>(c.{3}Get<{1}, {2}>)", Unlazy(parameterType).CSharpTypeIdentifier(), requestedType.CSharpTypeIdentifier(), suggestedType.CSharpTypeIdentifier(), isOptional ? "Try" : ""));
                }
                return(string.Format("new Lazy<{0}>(c.{2}Get<{1}>)", Unlazy(parameterType).CSharpTypeIdentifier(), requestedType.CSharpTypeIdentifier(), isOptional ? "Try" : ""));
            }
            if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(Func <>))
            {
                if (suggestedType != null)
                {
                    return(string.Format("c.{2}Get<{0}, {1}>", requestedType.CSharpTypeIdentifier(), suggestedType.CSharpTypeIdentifier(), isOptional ? "Try" : ""));
                }
                return(string.Format("c.{1}Get<{0}>", requestedType.CSharpTypeIdentifier(), isOptional ? "Try" : ""));
            }
            if (suggestedType != null)
            {
                return(string.Format("c.{2}Get<{0}, {1}>()", requestedType.CSharpTypeIdentifier(), suggestedType.CSharpTypeIdentifier(), isOptional ? "Try" : ""));
            }
            return(string.Format("c.{1}Get<{0}>()", requestedType.CSharpTypeIdentifier(), isOptional ? "Try" : ""));
        }
Exemplo n.º 2
0
        protected override object GetArgumentRquestAndConvert(Type parameterType, Type requestedType, ICustomAttributeProvider info, bool isOptional)
        {
            var suggesstionAttribute = info.Attribute2 <DefaultImplAttribute>();
            var suggestedType        = suggesstionAttribute == null ? null : suggesstionAttribute.TargetType;

            if (suggestedType != null)
            {
                // TODO allow get with suggestion using API. As a result code will be reusable with static mining. And it is just useful feature.
                // TODO same in static mining
                var now = _provider.TryGetLazyCore(requestedType);
                if (now == null)
                {
                    _provider.Add(requestedType, suggestedType);
                }
            }

            Lazy <object> serviceLazy;

            if (isOptional)
            {
                // TODO same in static mining
                serviceLazy = _provider.TryGetLazyCore(requestedType);
                if (serviceLazy == null)
                {
                    return(null);
                }
            }
            else
            {
                serviceLazy = _provider.GetLazyCore(requestedType);
            }

            // convert to parameterType

            if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(Lazy <>))
            {
                _provider.ValidateReflectionPermissions();
                return(TypedLazyWrapper.Create(parameterType.GetGenericArguments()[0], serviceLazy));
            }

            if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(Func <>))
            {
                _provider.ValidateReflectionPermissions();
                return(TypedFuncWrapper.Create(parameterType.GetGenericArguments()[0], serviceLazy));
            }

            if (parameterType.IsInstanceOfType(serviceLazy))
            {
                return(serviceLazy);
            }
            var value = serviceLazy.Value;

            if (parameterType.IsInstanceOfType(value))
            {
                return(value);
            }
            else if (value != null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Can not provide value for {0}. {1} does not implement {0}", parameterType.Name, value.GetType().Name));
            }
            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Can not provide value for {0}", parameterType));
        }