Exemple #1
0
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            var memeber = _typeAccessor.Find(binder.Name, _flags);

            if (memeber == null)
            {
                return(SafeMode);
            }

            if (!memeber.HasGetter)
            {
                return(SafeMode);
            }

            var value = memeber.GetValue(_wrapped);

            if (value == null)
            {
                return(true);
            }

            result = new DynamicProxy(value, SafeMode);
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Searches for the property or field, using the specified binding constraints.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> to search for the property or field in.</param>
        /// <param name="name">The name of the property or field to find.</param>
        /// <param name="flags">A bitmask comprised of one or more <see cref="BindingFlags"/> that specify how the search is conducted.</param>
        /// <returns>
        /// An <see cref="IMemberAccessor"/> instance for the property or field if found; otherwise <c>null</c>.
        /// </returns>
        public static IMemberAccessor Find(Type type, string name, BindingFlags flags)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            TypeAccessor    typeAccessor   = TypeAccessor.GetAccessor(type);
            IMemberAccessor memberAccessor = typeAccessor.Find(name, flags);

            return(memberAccessor);
        }