Example #1
0
        public bool IsInstanceOfType(object value)
        {
            if (_isInputType)
            {
                if (value is null)
                {
                    return(true);
                }

                if (ClrType.IsInstanceOfType(value))
                {
                    return(true);
                }

                Type elementType = DotNetTypeInfoFactory
                                   .GetInnerListType(value.GetType());

                if (elementType == null)
                {
                    return(false);
                }

                return(elementType == ElementType.ToClrType());
            }

            // TODO : resources
            throw new InvalidOperationException(
                      "The specified type is not an input type.");
        }
Example #2
0
        protected sealed override bool IsInstanceOfType(object value)
        {
            if (value is null)
            {
                return(true);
            }

            if (ClrType.IsInstanceOfType(value))
            {
                return(true);
            }

            Type elementType = DotNetTypeInfoFactory.GetInnerListType(value.GetType());

            if (elementType is null)
            {
                return(false);
            }

            Type clrType = InnerClrType;

            if (elementType == typeof(object))
            {
                return(value is IList l &&
                       (l.Count == 0 || clrType == l[0]?.GetType()));
            }

            return(elementType == clrType);
        }
Example #3
0
        private bool TryCreateImplicitSorting(
            PropertyInfo property,
            out SortOperationDefintion definition)
        {
            Type type = property.PropertyType;

            if (type.IsGenericType &&
                System.Nullable.GetUnderlyingType(type) is Type nullableType)
            {
                type = nullableType;
            }
            if (typeof(IComparable).IsAssignableFrom(type))
            {
                definition = SortOperationDescriptor
                             .CreateOperation(property, Context)
                             .CreateDefinition();
                return(true);
            }
            if (type.IsClass && !DotNetTypeInfoFactory.IsListType(type))
            {
                definition = SortObjectOperationDescriptor
                             .CreateOperation(property, Context)
                             .CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }
Example #4
0
        public void NotSupportedCases(Type nativeType)
        {
            // arrange
            DotNetTypeInfoFactory factory = new DotNetTypeInfoFactory();

            // act
            bool success = factory.TryCreate(nativeType, out TypeInfo typeInfo);

            // assert
            Assert.False(success);
        }
 public IClrTypeReference Compile()
 {
     if (IsTypeNullable.HasValue || IsElementTypeNullable.HasValue)
     {
         Type rewritten = DotNetTypeInfoFactory.Rewrite(
             Type,
             !(IsTypeNullable ?? false),
             !(IsElementTypeNullable ?? false));
         return(new ClrTypeReference(rewritten, Context));
     }
     return(this);
 }
Example #6
0
        public void CreateTypeInfoFromValueType(Type nativeType, string expectedTypeName)
        {
            // arrange
            DotNetTypeInfoFactory factory = new DotNetTypeInfoFactory();

            // act
            bool success = factory.TryCreate(nativeType, out TypeInfo typeInfo);

            // assert
            Assert.True(success);
            Assert.Equal(expectedTypeName, typeInfo.TypeFactory(new IntType()).Visualize());
        }
Example #7
0
 internal static void RewriteClrType(
     this FieldDescriptionBase fieldDescription,
     Func <Type, TypeReference> createContext)
 {
     if (fieldDescription.IsNullable.HasValue &&
         fieldDescription.TypeReference.IsClrTypeReference())
     {
         fieldDescription.TypeReference = createContext(
             DotNetTypeInfoFactory.Rewrite(
                 fieldDescription.TypeReference.ClrType,
                 !fieldDescription.IsNullable.Value,
                 !fieldDescription.IsElementNullable.Value));
     }
 }
        private bool TryGetTypeOfRuntimeType(
            Type runtimeType,
            [NotNullWhen(true)] out Type?type)
        {
            if (runtimeType.IsGenericType &&
                System.Nullable.GetUnderlyingType(runtimeType) is { } nullableType)
            {
                runtimeType = nullableType;
            }

            if (Bindings.TryGetValue(runtimeType, out type))
            {
                return(true);
            }

            if (DotNetTypeInfoFactory.IsListType(runtimeType))
            {
                if (!TypeInspector.Default.TryCreate(runtimeType, out Utilities.TypeInfo typeInfo))
                {
                    throw new ArgumentException(
                              string.Format("The type {0} is unknown", runtimeType.Name),
                              nameof(runtimeType));
                }

                if (TryGetTypeOfRuntimeType(typeInfo.ClrType, out Type? clrType))
                {
                    type = typeof(ListFilterInput <>).MakeGenericType(clrType);
                    return(true);
                }
            }

            if (runtimeType.IsEnum)
            {
                type = typeof(EnumOperationInput <>).MakeGenericType(runtimeType);
                return(true);
            }

            if (runtimeType.IsClass)
            {
                type = typeof(FilterInputType <>).MakeGenericType(runtimeType);
                return(true);
            }

            type = null;
            return(false);
        }
        protected override void VisitList(
            IList <object> list,
            DeserializationContext context)
        {
            if (context.Type.IsArray)
            {
                var array = Array.CreateInstance(
                    context.Type.GetElementType(),
                    list.Count);

                for (int i = 0; i < list.Count; i++)
                {
                    var valueContext = new DeserializationContext();
                    valueContext.Type = context.Type.GetElementType();
                    Visit(list[i], valueContext);

                    array.SetValue(valueContext.Object, i);
                }

                context.Object = array;
            }
            else
            {
                Type elementType = DotNetTypeInfoFactory.GetInnerListType(context.Type);
                if (elementType != null)
                {
                    Type  listType = typeof(List <>).MakeGenericType(elementType);
                    IList l        = (IList)Activator.CreateInstance(listType);

                    for (int i = 0; i < list.Count; i++)
                    {
                        var valueContext = new DeserializationContext();
                        valueContext.Type = context.Type.GetElementType();
                        Visit(list[i], valueContext);

                        list.Add(valueContext.Object);
                    }
                }
            }
        }
Example #10
0
        private static bool TryCreateArrayFilter(
            IDescriptorContext context,
            Type type,
            PropertyInfo property,
            IFilterConvention filterConventions,
            [NotNullWhen(true)] out FilterFieldDefintion?definition)
        {
            if (DotNetTypeInfoFactory.IsListType(type))
            {
                if (!TypeInspector.Default.TryCreate(type, out Utilities.TypeInfo typeInfo))
                {
                    throw new ArgumentException(
                              FilterResources.FilterArrayFieldDescriptor_InvalidType,
                              nameof(property));
                }

                Type elementType = typeInfo.ClrType;
                ArrayFilterFieldDescriptor field;

                if (elementType == typeof(string) ||
                    elementType == typeof(bool) ||
                    typeof(IComparable).IsAssignableFrom(elementType))
                {
                    elementType = typeof(ISingleFilter <>).MakeGenericType(elementType);
                }

                field = new ArrayFilterFieldDescriptor(context,
                                                       property, elementType, filterConventions);

                definition = field.CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }
Example #11
0
        private bool TryCreateImplicitFilter(
            PropertyInfo property,
            out FilterFieldDefintion definition)
        {
            Type type = property.PropertyType;

            if (type.IsGenericType && Nullable.GetUnderlyingType(type) is Type nullableType)
            {
                type = nullableType;
            }

            if (type == typeof(string))
            {
                var field = new StringFilterFieldDescriptor(Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (type == typeof(bool))
            {
                var field = new BooleanFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (IsComparable(property.PropertyType))
            {
                var field = new ComparableFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (DotNetTypeInfoFactory.IsListType(type))
            {
                if (!TypeInspector.Default.TryCreate(type, out Utilities.TypeInfo typeInfo))
                {
                    throw new ArgumentException(
                              FilterResources.FilterArrayFieldDescriptor_InvalidType,
                              nameof(property));
                }

                Type elementType = typeInfo.ClrType;
                ArrayFilterFieldDescriptor field;

                if (elementType == typeof(string) ||
                    elementType == typeof(bool) ||
                    typeof(IComparable).IsAssignableFrom(elementType))
                {
                    field = new ArrayFilterFieldDescriptor(
                        Context,
                        property,
                        typeof(ISingleFilter <>).MakeGenericType(elementType));
                }
                else
                {
                    field = new ArrayFilterFieldDescriptor(Context, property, elementType);
                }

                definition = field.CreateDefinition();
                return(true);
            }

            if (type.IsClass)
            {
                var field = new ObjectFilterFieldDescriptor(
                    Context, property, property.PropertyType);
                definition = field.CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }