// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PropertyHelper.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Property helper class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Reflection;
using Catel.Properties;
using log4net;
namespace Catel.Reflection
{
/// <summary>
/// Property helper class.
/// </summary>
public static class PropertyHelper
{
#region Variables
/// <summary>
/// The <see cref="ILog">log</see> object.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#endregion
/// <summary>
/// Gets the property value of a specific object.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="property">The property.</param>
/// <returns>
/// The property value or <c>null</c> if no property can be found.
/// </returns>
/// <exception cref="ArgumentNullException">when <paramref name="obj"/> is <c>null</c>.</exception>
/// <exception cref="PropertyNotFoundException">when <paramref name="obj"/> is not found or not publicly available.</exception>
/// <exception cref="CannotGetPropertyValueException">when the property value cannot be read.</exception>
public static object GetPropertyValue(object obj, string property)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
Type type = obj.GetType();
PropertyInfo propertyInfo = type.GetProperty(property);
if (propertyInfo == null)
{
Log.Error(TraceMessages.PropertyNotFoundOnObjectProbablyInvalidMapping, property, type);
throw new PropertyNotFoundException(property);
}
// Return property value if available
if (!propertyInfo.CanRead)
{
Log.Error(TraceMessages.CannotReadProperty, type, property);
throw new CannotGetPropertyValueException(property);
}
try
{
return propertyInfo.GetValue(obj, null);
}
catch (MethodAccessException)
{
Log.Error(TraceMessages.CannotReadProperty, type, property);
throw new CannotGetPropertyValueException(property);
}
}
/// <summary>
/// Gets the property value of a specific object.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="obj">The object.</param>
/// <param name="property">The property.</param>
/// <returns>
/// The property value or <c>null</c> if no property can be found.
/// </returns>
/// <exception cref="ArgumentNullException">when <paramref name="obj"/> is <c>null</c>.</exception>
/// <exception cref="PropertyNotFoundException">when <paramref name="obj"/> is not found or not publicly available.</exception>
/// <exception cref="CannotGetPropertyValueException">when the property value cannot be read.</exception>
public static TValue GetPropertyValue<TValue>(object obj, string property)
{
return (TValue)GetPropertyValue(obj, property);
}
/// <summary>
/// Sets the property value of a specific object.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="property">The property.</param>
/// <param name="value">The value.</param>
/// <exception cref="ArgumentNullException">when <paramref name="obj"/> is <c>null</c>.</exception>
/// <exception cref="PropertyNotFoundException">when <paramref name="obj"/> is not found or not publicly available.</exception>
/// <exception cref="CannotSetPropertyValueException">when the property value cannot be written.</exception>
public static void SetPropertyValue(object obj, string property, object value)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
Type type = obj.GetType();
PropertyInfo propertyInfo = type.GetProperty(property);
if (propertyInfo == null)
{
Log.Error(TraceMessages.PropertyNotFoundOnObjectProbablyInvalidMapping, property, type);
throw new PropertyNotFoundException(property);
}
if (!propertyInfo.CanWrite)
{
Log.Error(TraceMessages.CannotWriteProperty, type, property);
throw new CannotSetPropertyValueException(property);
}
try
{
propertyInfo.SetValue(obj, value, null);
}
catch (MethodAccessException)
{
Log.Error(TraceMessages.CannotWriteProperty, type, property);
throw new CannotSetPropertyValueException(property);
}
}
}
}