|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
namespace System.Core.Helpers
{
/// <summary>
/// Contains helper functions related to reflection
/// </summary>
public static class ReflectionHelper
{
/// <summary>
/// Searches for a property in the given property path
/// </summary>
/// <param name="rootType">The root/starting point to start searching</param>
/// <param name="propertyPath">The path to the property. Ex Customer.Address.City</param>
/// <returns>A <see cref="PropertyInfo"/> describing the property in the property path.</returns>
public static PropertyInfo GetPropertyFromPath(Type rootType,string propertyPath)
{
if (rootType == null)
throw new ArgumentNullException("rootType");
Type propertyType = rootType;
PropertyInfo propertyInfo = null;
string[] pathElements = propertyPath.Split(new char[1] { '.' });
for (int i = 0; i < pathElements.Length; i++)
{
propertyInfo = propertyType.GetProperty(pathElements[i], BindingFlags.Public | BindingFlags.Instance);
if (propertyInfo != null)
{
propertyType = propertyInfo.PropertyType;
}
else
{
throw new ArgumentOutOfRangeException("propertyPath",propertyPath,"Invalid property path");
}
}
return propertyInfo;
}
public static PropertyDescriptor GetPropertyDescriptorFromPath(Type rootType, string propertyPath)
{
string propertyName;
bool lastProperty = false;
if (rootType == null)
throw new ArgumentNullException("rootType");
if (propertyPath.Contains("."))
propertyName = propertyPath.Substring(0, propertyPath.IndexOf("."));
else
{
propertyName = propertyPath;
lastProperty = true;
}
PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(rootType)[propertyName];
if (propertyDescriptor == null)
throw new ArgumentOutOfRangeException("propertyPath", propertyPath, string.Format("Invalid property path for type '{0}' ",rootType.Name));
if (!lastProperty)
return GetPropertyDescriptorFromPath(propertyDescriptor.PropertyType, propertyPath.Substring(propertyPath.IndexOf(".") + 1));
else
return propertyDescriptor;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm a 39 year old software developer living in Norway.
I'm currently working for a software company making software for the retail industry.