Exemplo n.º 1
0
        public static void ValidateTupleReturnType(MethodInfo method)
        {
            if (method.ReturnsTuple())
            {
                Type returnType = method.ReturnType;
                Type tupleType  = TaskExtensions.UnwrapReturnType(returnType);

                if (!tupleType.IsValidTupleType())
                {
                    ThrowHelper.MethodReturnsInvalidValueTuple(method);
                }

                if (method.ReturnParameter.IsDefined(typeof(TupleElementNamesAttribute)))
                {
                    int tupleLength = tupleType.GetValueTupleLength();

                    TupleElementNamesAttribute attribute =
                        method.ReturnParameter.GetCustomAttribute <TupleElementNamesAttribute>();

                    IList <string> transformNames = attribute.TransformNames;

                    List <string> tupleNames =
                        transformNames.Take(tupleLength).ToList();

                    ValidateTupleReturnType(method, tupleNames);
                    ValidateTupleReturnTypeWithOutRefParameters(method, tupleNames);
                }
            }
        }
Exemplo n.º 2
0
        private static void ValidateTupleReturnTypeOfProgressiveMethod(MethodInfo method, ParameterInfo lastParameter)
        {
            bool methodHasAttribute     = method.ReturnParameter.IsDefined(typeof(TupleElementNamesAttribute));
            bool parameterHasAttributte = lastParameter.IsDefined(typeof(TupleElementNamesAttribute));

            bool attributesMatch = methodHasAttribute == parameterHasAttributte;

            if (methodHasAttribute && parameterHasAttributte)
            {
                TupleElementNamesAttribute methodAttribute =
                    method.ReturnParameter.GetCustomAttribute <TupleElementNamesAttribute>();

                TupleElementNamesAttribute parameterAttribute =
                    lastParameter.GetCustomAttribute <TupleElementNamesAttribute>();

                IList <string> methodTransformNames    = methodAttribute.TransformNames;
                IList <string> parameterTransformNames = parameterAttribute.TransformNames;

                attributesMatch = methodTransformNames.SequenceEqual(parameterTransformNames);
            }

            if (!attributesMatch)
            {
                ThrowHelper.ProgressiveParameterTupleMismatch(method);
            }
        }
 public JsonValueTupleBinder(TupleElementNamesAttribute attr, Type modelType, bool isCaseSensitive = true) : base(isCaseSensitive)
 {
     _attr            = attr;
     _modelType       = modelType;
     _isCaseSensitive = isCaseSensitive;
     _valueTypes      = modelType.GetGenericArguments();
 }
Exemplo n.º 4
0
    public static void DefaultConstructor()
    {
        var attribute = new TupleElementNamesAttribute();

        Assert.NotNull(attribute.TransformNames);
        Assert.Equal(0, attribute.TransformNames.Count);
    }
Exemplo n.º 5
0
    public static void Constructor()
    {
        var attribute = new TupleElementNamesAttribute(new string[] { "name1", "name2" });

        Assert.NotNull(attribute.TransformNames);
        Assert.Equal(new string[] { "name1", "name2" }, attribute.TransformNames);

        Assert.Throws <ArgumentNullException>(() => new TupleElementNamesAttribute(null));
    }
Exemplo n.º 6
0
        public NameIndexTable(TupleElementNamesAttribute att)
        {
            var names = att.TransformNames;

            _table = new Dictionary <Utf8String, int>();
            for (int i = 0; i < names.Count; i++)
            {
                _table.Add(new Utf8String(names[i]), i);
            }
        }
        public static object ParseTupleFromModelAttributes(string body, TupleElementNamesAttribute tupleAttr, Type tupleType)
        {
            var jobj       = JObject.Parse(body);
            var parameters = tupleAttr.TransformNames.Zip(tupleType.GetConstructors()
                                                          .Single()
                                                          .GetParameters())
                             .Select(x => GetValue(jobj, x.First, x.Second))
                             .ToArray();

            object tuple = Activator.CreateInstance(tupleType, parameters);

            return(tuple);
        }
        internal static string[] GetTupleElementNames(TupleElementNamesAttribute attr)
        {
            if (attr == null)
            {
                return(null);
            }

            if (!attr.TransformNames.Any())
            {
                return(Array.Empty <string> ());
            }

            return(attr.TransformNames.ToArray());
        }
Exemplo n.º 9
0
        private IList <string> GetTransformNames(Type converterType)
        {
            IList <string> transformNames = null;

            if (converterType.IsDefined(typeof(TupleElementNamesAttribute), true))
            {
                TupleElementNamesAttribute attribute =
                    converterType.GetCustomAttribute <TupleElementNamesAttribute>();

                transformNames = attribute.TransformNames;
            }

            return(transformNames);
        }
        private static ArgumentUnpacker GetTupleArgumentUnpacker(MethodInfo method)
        {
            Type tupleType = TaskExtensions.UnwrapReturnType(method.ReturnType);

            IEnumerable <string> transformNames = null;

            if (method.ReturnParameter.IsDefined(typeof(TupleElementNamesAttribute)))
            {
                TupleElementNamesAttribute attribute =
                    method.ReturnParameter.GetCustomAttribute <TupleElementNamesAttribute>();

                transformNames = attribute.TransformNames;
            }

            return(ArgumentUnpackerHelper.GetValueTupleArgumentUnpacker
                       (tupleType, transformNames));
        }
Exemplo n.º 11
0
        public static string GetFormattedReturnType(MethodInfo method)
        {
            if (!method.ReturnsTuple())
            {
                return(FormatType(method.ReturnType));
            }
            else
            {
                Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);

                IEnumerable <Type> tupleElementTypes =
                    returnType.GetValueTupleElementTypes();

                TupleElementNamesAttribute tupleElementNamesAttribute =
                    method.ReturnParameter.GetCustomAttribute <TupleElementNamesAttribute>();

                IEnumerable <string> tupleElementsIdentifiers;

                IEnumerable <string> tupleElementTypesIdentifiers = tupleElementTypes
                                                                    .Select(x => FormatType(x));

                if (tupleElementNamesAttribute == null)
                {
                    tupleElementsIdentifiers = tupleElementTypesIdentifiers;
                }
                else
                {
                    tupleElementsIdentifiers =
                        tupleElementTypesIdentifiers
                        .Zip(tupleElementNamesAttribute.TransformNames,
                             (type, name) => $"{type} {name}");
                }

                string tupleIdentifierContent = string.Join(", ", tupleElementsIdentifiers);

                if (typeof(Task).IsAssignableFrom(method.ReturnType))
                {
                    return($"Task<({tupleIdentifierContent})>");
                }
                else
                {
                    return($"({tupleIdentifierContent})");
                }
            }
        }
Exemplo n.º 12
0
        public static IWampResultExtractor GetValueTupleResultExtractor(MethodInfo method)
        {
            Type tupleType = TaskExtensions.UnwrapReturnType(method.ReturnType);

            IWampResultExtractor result = new PositionalTupleExtractor(tupleType);

            TupleElementNamesAttribute attribute =
                method.ReturnParameter.GetCustomAttribute <TupleElementNamesAttribute>();

            if (attribute != null)
            {
                int valueTupleLength = tupleType.GetValueTupleLength();

                // If the tuple is named, return a named tuple extractor
                if (attribute.TransformNames.Take(valueTupleLength).All(x => x != null))
                {
                    result = new NamedTupleExtractor(tupleType, attribute.TransformNames);
                }
            }

            return(result);
        }