예제 #1
0
        /// <summary>
        /// Gets the member's value on the specified object.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="memberName">Name of the member.</param>
        /// <param name="accessorFactory">The accessor factory.</param>
        /// <returns>The member's value</returns>
        public static object GetMember(object obj, string memberName, AccessorFactory accessorFactory)
        {
            try
            {
                object value = null;

                if (memberName.IndexOf("[") > -1)
                {
                    value = GetArrayMember(obj, memberName, accessorFactory);
                }
                else
                {
                    if (obj is IDictionary)
                    {
                        value = ((IDictionary)obj)[memberName];
                    }
                    else
                    {
                        IGetter getter = accessorFactory.CreateGetter(obj.GetType(), memberName);
                        if (getter == null)
                        {
                            throw new ProbeException("No Get method for member " + memberName + " on instance of " + obj.GetType().Name);
                        }
                        try
                        {
                            value = getter.GetValue(obj, null);
                        }
                        catch (Exception ae)
                        {
                            throw new ProbeException(ae);
                        }
                    }
                }
                return value;
            }
            catch (ProbeException pe)
            {
                throw pe;
            }
            catch (Exception e)
            {
                throw new ProbeException("Could not Set property '" + memberName + "' for " + obj.GetType().Name + ".  Cause: " + e.Message, e);
            }
        }