// 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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace CefSharp.ModelBinding
{
///
/// Default binder - used as a fallback when a specific modelbinder
/// is not available.
///
public class DefaultBinder : IBinder
{
private readonly static MethodInfo ToArrayMethodInfo = typeof(Enumerable).GetMethod("ToArray", BindingFlags.Public | BindingFlags.Static);
private readonly IFieldNameConverter fieldNameConverter;
///
/// List of property names to be ignored
///
public IEnumerable BlackListedPropertyNames { get; set; }
///
/// DefaultBinder constructor
///
/// used to convert field names to property names
public DefaultBinder(IFieldNameConverter fieldNameConverter)
{
if (fieldNameConverter == null)
{
throw new ArgumentNullException("fieldNameConverter");
}
this.fieldNameConverter = fieldNameConverter;
BlackListedPropertyNames = new List();
}
///
/// Bind to the given model type
///
/// object to be converted into a model
/// Model type to bind to
/// Bound model
public virtual object Bind(object obj, Type modelType)
{
if (obj == null)
{
return null;
}
var objType = obj.GetType();
// If the object can be directly assigned to the modelType then return immediately.
if (modelType.IsAssignableFrom(objType))
{
return obj;
}
if (modelType.IsEnum && modelType.IsEnumDefined(obj))
{
return Enum.ToObject(modelType, obj);
}
var typeConverter = TypeDescriptor.GetConverter(objType);
// If the object can be converted to the modelType (eg: double to int)
if (typeConverter.CanConvertTo(modelType))
{
return typeConverter.ConvertTo(obj, modelType);
}
Type genericType = null;
if (modelType.IsCollection() || modelType.IsArray() || modelType.IsEnumerable())
{
// Make sure it has a generic type
if (modelType.GetTypeInfo().IsGenericType)
{
genericType = modelType.GetGenericArguments().FirstOrDefault();
}
else
{
var ienumerable = modelType.GetInterfaces().Where(i => i.GetTypeInfo().IsGenericType).FirstOrDefault(i => i.GetGenericTypeDefinition() == typeof(IEnumerable<>));
genericType = ienumerable == null ? null : ienumerable.GetGenericArguments().FirstOrDefault();
}
if (genericType == null)
{
// If we don't have a generic type then just use object
genericType = typeof(object);
}
}
var bindingContext = this.CreateBindingContext(obj, modelType, genericType);
var destinationType = bindingContext.DestinationType;
if (destinationType.IsCollection() || destinationType.IsArray() || destinationType.IsEnumerable())
{
var model = (IList)bindingContext.Model;
var collection = obj as ICollection;
if (collection == null)
{
return null;
}
for (var i = 0; i < collection.Count; i++)
{
var val = GetValue(bindingContext, i);
//If the value is null then we'll add null to the collection,
if (val == null)
{
//For value types like int we'll create the default value and assign that as we cannot assign null
model.Add(genericType.IsValueType ? Activator.CreateInstance(genericType) : null);
}
else
{
var valueType = val.GetType();
//If the collection item is a list or dictionary then we'll attempt to bind it
if (typeof(IDictionary).IsAssignableFrom(valueType) ||
typeof(IList