Exemple #1
0
        /// <summary>
        /// Creates a getter for a property.
        /// </summary>
        /// <typeparam name="T">The expected type.</typeparam>
        /// <param name="simulation">The simulation.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="comparer">The property name comparer.</param>
        /// <param name="function">The function that will return the value of the property.</param>
        /// <returns>
        /// <c>true</c> if the getter was created successfully; otherwise <c>false</c>.
        /// </returns>
        public bool CreateExportMethod <T>(Simulation simulation, string propertyName, out Func <T> function, IEqualityComparer <string> comparer = null)
        {
            simulation.ThrowIfNull(nameof(simulation));

            // Find methods to create the export
            Func <T> result = null;

            foreach (var member in Reflection.GetNamedMembers(this, propertyName, comparer))
            {
                // Use methods
                if (member is MethodInfo mi)
                {
                    result = CreateGetterForMethod <T>(simulation, mi);
                }

                // Use properties
                else if (member is PropertyInfo pi)
                {
                    result = ParameterHelper.CreateGetterForProperty <T>(this, pi);
                }

                // Return
                if (result != null)
                {
                    function = result;
                    return(true);
                }
            }

            // Not found
            function = null;
            return(false);
        }