Files
cefsharp/CefSharp/ModelBinding/ModelBindingExtensions.cs
Chris bef5dd37c4 Maintenance - Fixed formatting, file encoding, and copyright year (#2504)
* Applied formatting

* Fixed file encoding + Updated copyright year

* Updated License

* Fixed more file encoding

* Copyright notice now shows the year the file was created
2018-09-10 10:41:13 +10:00

54 lines
2.0 KiB
C#

// Copyright © 2016 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace CefSharp.ModelBinding
{
/// <summary>
/// Containing extensions for the <see cref="Type"/> object.
/// </summary>
internal static class ModelBindingExtensions
{
/// <summary>
/// Checks if a type is an array or not
/// </summary>
/// <param name="source">The type to check.</param>
/// <returns><see langword="true" /> if the type is an array, otherwise <see langword="false" />.</returns>
public static bool IsArray(this Type source)
{
return source.GetTypeInfo().BaseType == typeof(Array);
}
/// <summary>
/// Checks if a type is an collection or not
/// </summary>
/// <param name="source">The type to check.</param>
/// <returns><see langword="true" /> if the type is a collection, otherwise <see langword="false" />.</returns>
public static bool IsCollection(this Type source)
{
var collectionType = typeof(ICollection<>);
return source.GetTypeInfo().IsGenericType && source
.GetInterfaces()
.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == collectionType);
}
/// <summary>
/// Checks if a type is enumerable or not
/// </summary>
/// <param name="source">The type to check.</param>
/// <returns><see langword="true" /> if the type is an enumerable, otherwise <see langword="false" />.</returns>
public static bool IsEnumerable(this Type source)
{
var enumerableType = typeof(IEnumerable<>);
return source.GetTypeInfo().IsGenericType && source.GetGenericTypeDefinition() == enumerableType;
}
}
}