// 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
{
///
/// Represents a bindable member of a type, which can be a property or a field.
///
public class BindingMemberInfo
{
PropertyInfo propertyInfo;
FieldInfo fieldInfo;
///
/// Gets a reference to the MemberInfo that this BindingMemberInfo represents. This can be a property or a field.
///
public MemberInfo MemberInfo
{
get { return this.propertyInfo ?? (MemberInfo)this.fieldInfo; }
}
///
/// Gets the name of the property or field represented by this BindingMemberInfo.
///
public string Name
{
get { return this.MemberInfo.Name; }
}
///
/// Gets the data type of the property or field represented by this BindingMemberInfo.
///
public Type PropertyType
{
get
{
if (this.propertyInfo != null)
{
return this.propertyInfo.PropertyType;
}
else
{
return this.fieldInfo.FieldType;
}
}
}
///
/// Constructs a BindingMemberInfo instance for a property.
///
/// The bindable property to represent.
public BindingMemberInfo(PropertyInfo propertyInfo)
{
if (propertyInfo == null)
{
throw new ArgumentNullException("propertyInfo");
}
this.propertyInfo = propertyInfo;
}
///
/// Constructs a BindingMemberInfo instance for a field.
///
/// The bindable field to represent.
public BindingMemberInfo(FieldInfo fieldInfo)
{
if (fieldInfo == null)
{
throw new ArgumentNullException("fieldInfo");
}
this.fieldInfo = fieldInfo;
}
///
/// Sets the value from a specified object associated with the property or field represented by this BindingMemberInfo.
///
/// The object whose property or field should be assigned.
/// The value to assign in the specified object to this BindingMemberInfo's property or field.
public void SetValue(object destinationObject, object newValue)
{
if (this.propertyInfo != null)
{
this.propertyInfo.SetValue(destinationObject, newValue, null);
}
else
{
this.fieldInfo.SetValue(destinationObject, newValue);
}
}
///
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
var other = obj as BindingMemberInfo;
if (other == null)
{
return false;
}
return this.MemberInfo.Equals(other.MemberInfo);
}
///
/// Compares two BindingMemberInfo's with eachother on their respective values rather then their reference
///
/// the other BindingMemberInfo
/// true when they are equal and false otherwise
public bool Equals(BindingMemberInfo obj)
{
if (obj == null)
{
return false;
}
return this.MemberInfo.Equals(obj.MemberInfo);
}
///
public override int GetHashCode()
{
return this.MemberInfo.GetHashCode();
}
///
/// Returns an enumerable sequence of bindable properties for the specified type.
///
/// The type to enumerate.
/// Bindable properties.
public static IEnumerable Collect()
{
return Collect(typeof(T));
}
///
/// Returns an enumerable sequence of bindable properties for the specified type.
///
/// The type to enumerate.
/// Bindable properties.
public static IEnumerable Collect(Type type)
{
var fromProperties = type
.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead && p.CanWrite)
.Where(property => !property.GetIndexParameters().Any())
.Select(property => new BindingMemberInfo(property));
var fromFields = type.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => !f.IsInitOnly)
.Select(field => new BindingMemberInfo(field));
return fromProperties.Concat(fromFields);
}
}
}