Ejemplo n.º 1
0
 /// <summary>
 /// Sets the value of a property supported by
 /// the specified object instance
 /// </summary>
 /// <param name="instance">An object whose property value will be set</param>
 /// <param name="value">The value to assign to the property</param>
 public void SetValue(object instance, object value)
 {
     Precondition.Require(instance, () => Error.ArgumentNull("instance"));
     _accessor.SetBoxedValue(instance, value);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Gets the value of a property supported by
 /// the specified object instance
 /// </summary>
 /// <param name="instance">An object whose property value will be returned</param>
 public object GetValue(object instance)
 {
     Precondition.Require(instance, () => Error.ArgumentNull("instance"));
     return(_accessor.GetBoxedValue(instance));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Gets the typed value of a property supported by
 /// the specified object instance.
 /// </summary>
 /// <param name="instance">An object whose property value will be returned.</param>
 /// <typeparam name="T">The type of an instance.</typeparam>
 /// <typeparam name="V">The type of the property.</typeparam>
 public V GetValue <T, V>(T instance)
 {
     Precondition.Require(instance, () => Error.ArgumentNull("instance"));
     return((V)_accessor.GetBoxedValue(instance));
 }
Ejemplo n.º 4
0
        private static object GetIndexedPropertyValue(object container, string expr)
        {
            Precondition.Require(container, () => Error.ArgumentNull("container"));
            Precondition.Defined(expr, () => Error.BindingExpressionCannotBeEmpty("expr"));

            int start = expr.IndexOfAny(_indexExprStartChars);
            int end   = expr.IndexOfAny(_indexExprEndChars, start + 1);

            if (start < 0 || end < 0 || end > start)
            {
                throw Error.InvalidBindingExpressionFormat("expr");
            }

            string indexPart    = expr.Substring(start + 1, end - start - 1).Trim();
            string propertyPart = null;
            object indexValue   = null;
            bool   isIntIndex   = false;
            object instance;

            if (start != 0)
            {
                propertyPart = expr.Substring(0, start);
            }

            if (indexPart.Length != 0)
            {
                if (indexPart[0] == '\"' && indexPart[indexPart.Length - 1] == '\"' ||
                    indexPart[0] == '\'' && indexPart[indexPart.Length - 1] == '\'')
                {
                    indexValue = indexPart.Substring(1, indexPart.Length - 2);
                }

                else if (!char.IsDigit(indexPart[0]))
                {
                    indexValue = indexPart;
                }
                else
                {
                    int index;
                    if (isIntIndex = int.TryParse(indexPart, NumberStyles.Integer,
                                                  CultureInfo.InvariantCulture, out index))
                    {
                        indexValue = index;
                    }
                    else
                    {
                        indexValue = indexPart;
                    }
                }
            }

            if (indexValue == null)
            {
                throw Error.InvalidIndexerExpressionFormat("expr");
            }

            if (!String.IsNullOrEmpty(propertyPart))
            {
                instance = GetPropertyOrFieldValue(container, propertyPart);
            }
            else
            {
                instance = container;
            }

            if (instance == null)
            {
                return(indexValue);
            }

            Array array = (instance as Array);

            if (array != null && isIntIndex)
            {
                return(array.GetValue((int)indexValue));
            }

            if (instance is IList && isIntIndex)
            {
                return(((IList)instance)[(int)indexValue]);
            }

            PropertyInfo pi = instance.GetType().GetProperty("Item",
                                                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                             null, null, new Type[] { indexValue.GetType() }, null);

            if (pi != null)
            {
                return(pi.GetValue(instance, new object[] { indexValue }));
            }

            throw Error.IndexerNotFound(instance.GetType(), "expr");
        }
 public static PropertyAccessor CreateAccessor(this PropertyInfo property)
 {
     Precondition.Require(property, () => Error.ArgumentNull("property"));
     return(_cache.GetAccessor(property));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Helper method used to find attributes
 /// associated with the specified member.
 /// </summary>
 public static IEnumerable <Attribute> GetCustomAttributes(this MemberInfo member,
                                                           bool inherit, params Type[] types)
 {
     Precondition.Require(member, () => Error.ArgumentNull("member"));
     return(member.GetCustomAttributes(inherit).Cast <Attribute>().Where(m => types.Contains(m.GetType())));
 }
Ejemplo n.º 7
0
 public static Type GetInterface(this Type t, Type type)
 {
     Precondition.Require(t, () => Error.ArgumentNull("t"));
     return(t.GetInterface(type.FullName));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="Radischevo.Wahha.Core.ReadOnlyDictionary{TKey,TValue}"/> class
 /// that wraps the specified <see cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.
 /// </summary>
 /// <param name="dictionary">
 /// The <see cref="System.Collections.Generic.IDictionary{TKey,TValue}"/> to wrap.
 /// </param>
 public ReadOnlyDictionary(IDictionary <TKey, TValue> dictionary)
 {
     Precondition.Require(dictionary, () => Error.ArgumentNull("dictionary"));
     _contents = dictionary;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Helper method used to find attributes of type <typeparamref name="TAttribute"/>,
 /// associated with the specified member.
 /// </summary>
 public static IEnumerable <TAttribute> GetCustomAttributes <TAttribute>(this MemberInfo member,
                                                                         bool inherit)
 {
     Precondition.Require(member, () => Error.ArgumentNull("member"));
     return(member.GetCustomAttributes(typeof(TAttribute), inherit).OfType <TAttribute>());
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Dynamically invokes (using fire-and-forget strategy)
 /// the method represented by the current delegate in the seperate thread.
 /// </summary>
 /// <param name="d">The delegate containing the method to invoke.</param>
 /// <param name="args">The argument list of a method.</param>
 public static void InvokeAndForget(this Delegate d, params object[] args)
 {
     Precondition.Require(d, () => Error.ArgumentNull("d"));
     ThreadPool.QueueUserWorkItem(_invokeShim, new TargetInfo(d, args));
 }
Ejemplo n.º 11
0
 public MemberAccessorCache(Type type)
     : base()
 {
     Precondition.Require(type, () => Error.ArgumentNull("type"));
     _type = type;
 }
Ejemplo n.º 12
0
 public bool IsEmpty(IComparer <T> comparer)
 {
     Precondition.Require(comparer, () => Error.ArgumentNull("comparer"));
     return(comparer.Compare(_from, _to) == 0);
 }
 /// <summary>
 /// Helper method used to find attributes of type <typeparamref name="TAttribute"/>,
 /// associated with the specified member.
 /// </summary>
 public static IEnumerable <TAttribute> GetCustomAttributes <TAttribute>(this ParameterInfo parameter, bool inherit)
 {
     Precondition.Require(parameter, () => Error.ArgumentNull("parameter"));
     return(parameter.GetCustomAttributes(inherit).OfType <TAttribute>());
 }
Ejemplo n.º 14
0
 public static IDictionary <TKey, TValue> AsReadOnly <TKey, TValue> (this IDictionary <TKey, TValue> dictionary)
 {
     Precondition.Require(dictionary, () => Error.ArgumentNull("dictionary"));
     return(new ReadOnlyDictionary <TKey, TValue> (dictionary));
 }