Maps from a property-set-value parameter to the declaring property. - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Maps from a property-set-value parameter to the declaring property.

Demo Code

// Permission is hereby granted, free of charge, to any person
using System.Reflection;

public class Main{
        /// <summary>
        /// Maps from a property-set-value parameter to the declaring property.
        /// </summary>
        /// <param name="pi">Parameter to the property setter.</param>
        /// <param name="prop">The property info on which the setter is specified.</param>
        /// <returns>True if the parameter is a property setter.</returns>
        public static bool TryGetDeclaringProperty(this ParameterInfo pi, out PropertyInfo prop)
        {/* w w w  .  j  a  v  a  2  s  .  co  m*/
            var mi = pi.Member as MethodInfo;
            if (mi != null && mi.IsSpecialName && mi.Name.StartsWith("set_"))
            {
                prop = mi.DeclaringType.GetProperty(mi.Name.Substring(4));
                return true;
            }

            prop = null;
            return false;
        }
}

Related Tutorials