Files
cefsharp/CefSharp/ModelBinding/DefaultBinder.cs
T
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

274 lines
10 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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace CefSharp.ModelBinding
{
/// <summary>
/// Default binder - used as a fallback when a specific modelbinder
/// is not available.
/// </summary>
public class DefaultBinder : IBinder
{
private readonly static MethodInfo ToArrayMethodInfo = typeof(Enumerable).GetMethod("ToArray", BindingFlags.Public | BindingFlags.Static);
private readonly IFieldNameConverter fieldNameConverter;
/// <summary>
/// List of property names to be ignored
/// </summary>
public IEnumerable<string> BlackListedPropertyNames { get; set; }
/// <summary>
/// DefaultBinder constructor
/// </summary>
/// <param name="fieldNameConverter">used to convert field names to property names</param>
public DefaultBinder(IFieldNameConverter fieldNameConverter)
{
if (fieldNameConverter == null)
{
throw new ArgumentNullException("fieldNameConverter");
}
this.fieldNameConverter = fieldNameConverter;
BlackListedPropertyNames = new List<string>();
}
/// <summary>
/// Bind to the given model type
/// </summary>
/// <param name="obj">object to be converted into a model</param>
/// <param name="modelType">Model type to bind to</param>
/// <returns>Bound model</returns>
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<string, object>).IsAssignableFrom(valueType) ||
typeof(IList<object>).IsAssignableFrom(valueType))
{
var subModel = Bind(val, genericType);
model.Add(subModel);
}
else
{
model.Add(val);
}
}
}
}
else
{
// If the object type is a dictionary (we're using ExpandoObject instead of Dictionary now)
// Then attempt to bind all the members
if (typeof(IDictionary<string, object>).IsAssignableFrom(bindingContext.Object.GetType()))
{
foreach (var modelProperty in bindingContext.ValidModelBindingMembers)
{
var val = GetValue(modelProperty.Name, bindingContext);
if (val != null)
{
BindValue(modelProperty, val, bindingContext);
}
}
}
}
if (modelType.IsArray())
{
var generictoArrayMethod = ToArrayMethodInfo.MakeGenericMethod(new[] { genericType });
return generictoArrayMethod.Invoke(null, new[] { bindingContext.Model });
}
return bindingContext.Model;
}
/// <summary>
/// CreateBindingContext - Can be overriden to change some binding context features
/// </summary>
/// <param name="obj">object</param>
/// <param name="modelType">model type</param>
/// <param name="genericType">generic type</param>
/// <returns>binding context</returns>
protected virtual BindingContext CreateBindingContext(object obj, Type modelType, Type genericType)
{
return new BindingContext
{
DestinationType = modelType,
Model = CreateModel(modelType, genericType),
ValidModelBindingMembers = GetBindingMembers(modelType, genericType).ToList(),
Object = obj,
GenericType = genericType
};
}
/// <summary>
/// BindValue
/// </summary>
/// <param name="modelProperty">model property</param>
/// <param name="obj">object</param>
/// <param name="context">context</param>
protected virtual void BindValue(BindingMemberInfo modelProperty, object obj, BindingContext context)
{
if (obj == null)
{
return;
}
if (modelProperty.PropertyType.IsAssignableFrom(obj.GetType()))
{
// Simply set the property
modelProperty.SetValue(context.Model, obj);
}
else
{
// Cannot directly set the property attempt to bind
var model = Bind(obj, modelProperty.PropertyType);
modelProperty.SetValue(context.Model, model);
}
}
/// <summary>
/// Get binding members
/// </summary>
/// <param name="modelType">model type</param>
/// <param name="genericType">generic type</param>
/// <returns>collection of binding member informations</returns>
protected virtual IEnumerable<BindingMemberInfo> GetBindingMembers(Type modelType, Type genericType)
{
var blackListHash = new HashSet<string>(BlackListedPropertyNames, StringComparer.Ordinal);
return BindingMemberInfo.Collect(genericType ?? modelType).Where(member => !blackListHash.Contains(member.Name));
}
/// <summary>
/// Create model based on type
/// </summary>
/// <param name="modelType">model type</param>
/// <param name="genericType">generic type</param>
/// <returns>a new instance of the object type</returns>
protected virtual object CreateModel(Type modelType, Type genericType)
{
if (modelType.IsCollection() || modelType.IsArray() || modelType.IsEnumerable())
{
// else just make a list
var listType = typeof(List<>).MakeGenericType(genericType);
return Activator.CreateInstance(listType);
}
return Activator.CreateInstance(modelType, true);
}
/// <summary>
/// Gets the value for the property name
/// </summary>
/// <param name="propertyName">property name</param>
/// <param name="context">context</param>
/// <returns>value or null</returns>
protected virtual object GetValue(string propertyName, BindingContext context)
{
var dictionary = (IDictionary<string, object>)context.Object;
if (dictionary.ContainsKey(propertyName))
{
return dictionary[propertyName];
}
return null;
}
/// <summary>
/// Gets the value based on the index, used to accessing objects
/// in a collection.
/// </summary>
/// <param name="context">binding context</param>
/// <param name="index">index</param>
/// <returns>element or null</returns>
protected virtual object GetValue(BindingContext context, int index)
{
var collection = context.Object as IList<object>;
return collection.ElementAtOrDefault(index);
}
}
}