Ejemplo n.º 1
0
        /// <summary>
        /// When overridden in a derived class, populates a
        /// <see cref="T:System.Runtime.Serialization.SerializationInfo"/>
        /// with the data needed to serialize the target object.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
        /// <param name="context">The destination for this serialization.</param>
        protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            Precondition.Require(info, () => Error.ArgumentNull("info"));

            if (_source == null || _source.IsSerializable())
            {
                info.AddValue("serializable", true);
                info.AddValue("source", _source);
            }
            else
            {
                info.AddValue("serializable", false);

                Type declaringType = _source.Method.DeclaringType;
                if (declaringType == null)
                {
                    info.AddValue("dynamicMethod", true);
#pragma warning disable 0219
                    T value = Value;
#pragma warning restore 0219
                }
                else
                {
                    info.AddValue("dynamicMethod", false);
                    info.AddValue("sourceMethod", _source.Method);
                    foreach (FieldInfo field in declaringType.GetFields())
                    {
                        info.AddValue("source" + field.Name, field.GetValue(_source.Target));
                    }
                }
            }

            info.AddValue("value", _value);
            info.AddValue("hasAssignedValue", _hasAssignedValue);
            info.AddValue("hasLoadedValue", _hasLoadedValue);
            info.AddValue("tag", _tag);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Gets the typed value of a field supported by
 /// the specified object instance.
 /// </summary>
 /// <param name="instance">An object whose field value will be returned.</param>
 /// <typeparam name="T">The type of an instance.</typeparam>
 /// <typeparam name="V">The type of the field.</typeparam>
 public V GetValue <T, V>(T instance)
 {
     Precondition.Require(instance, () => Error.ArgumentNull("instance"));
     return((V)_accessor.GetBoxedValue(instance));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Gets the value of a field supported by
 /// the specified object instance
 /// </summary>
 /// <param name="instance">An object whose field value will be returned</param>
 public object GetValue(object instance)
 {
     Precondition.Require(instance, () => Error.ArgumentNull("instance"));
     return(_accessor.GetBoxedValue(instance));
 }
 private static Func <Type, string, object> CreateNamedLocator(Func <Type, object> locator)
 {
     Precondition.Require(locator, () => Error.ArgumentNull("locator"));
     return((type, name) => locator(type));
 }
 /// <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.º 6
0
 public static FieldAccessor CreateAccessor(this FieldInfo field)
 {
     Precondition.Require(field, () => Error.ArgumentNull("field"));
     return(_cache.GetAccessor(field));
 }
Ejemplo n.º 7
0
		/// <summary>
		/// Gets the typed value with the specified key.
		/// </summary>
		/// <typeparam name="TValue">The type of value.</typeparam>
		/// <param name="key">The key to find.</param>
		/// <param name="defaultValue">The default value of the variable.</param>
		public static TValue GetValue<TValue>(this IValueSet values, string name, TValue defaultValue)
		{
			Precondition.Require(values, () => Error.ArgumentNull("values"));
			return values.GetValue<TValue>(name, defaultValue, CultureInfo.CurrentCulture);
		}
Ejemplo n.º 8
0
		public static bool ContainsAny(this IValueSet values)
		{
			Precondition.Require(values, () => Error.ArgumentNull("values"));
			return values.Keys.Any();
		}
Ejemplo n.º 9
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.º 10
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>());
 }
 /// <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.º 12
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.º 13
0
 public MemberAccessorCache(Type type)
     : base()
 {
     Precondition.Require(type, () => Error.ArgumentNull("type"));
     _type = type;
 }
Ejemplo n.º 14
0
 public bool IsEmpty(IComparer <T> comparer)
 {
     Precondition.Require(comparer, () => Error.ArgumentNull("comparer"));
     return(comparer.Compare(_from, _to) == 0);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Sets the value of a field supported by
 /// the specified object instance
 /// </summary>
 /// <param name="instance">An object whose field value will be set</param>
 /// <param name="value">The value to assign to the field</param>
 public void SetValue(object instance, object value)
 {
     Precondition.Require(instance, () => Error.ArgumentNull("instance"));
     _accessor.SetBoxedValue(instance, value);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FieldAccessor"/> class
 /// </summary>
 /// <param name="field">The field to build accessor for</param>
 public FieldAccessor(FieldInfo field)
 {
     Precondition.Require(field, () => Error.ArgumentNull("field"));
     _field    = field;
     _accessor = CreateAccessor(field);
 }
Ejemplo n.º 17
0
 public static Type GetInterface(this Type t, Type type)
 {
     Precondition.Require(t, () => Error.ArgumentNull("t"));
     return(t.GetInterface(type.FullName));
 }
Ejemplo n.º 18
0
		public static bool ContainsAny(this IValueSet values, 
			IEqualityComparer<string> comparer, string key)
		{
			Precondition.Require(values, () => Error.ArgumentNull("values"));
			return values.Keys.Contains(key, comparer);
		}
Ejemplo n.º 19
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");
        }
Ejemplo n.º 20
0
		/// <summary>
		/// Gets the typed value with the specified key.
		/// </summary>
		/// <typeparam name="TValue">The type of value.</typeparam>
		/// <param name="key">The key to find.</param>
		/// <param name="provider">An <see cref="IFormatProvider" /> interface implementation that 
		/// supplies culture-specific formatting information.</param>
		public static TValue GetValue<TValue>(this IValueSet values, string name, IFormatProvider provider)
		{
			Precondition.Require(values, () => Error.ArgumentNull("values"));
			return values.GetValue<TValue>(name, default(TValue), provider);
		}
 public static PropertyAccessor CreateAccessor(this PropertyInfo property)
 {
     Precondition.Require(property, () => Error.ArgumentNull("property"));
     return(_cache.GetAccessor(property));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Converts a subset of an array of 8-bit unsigned integers to its equivalent <see cref="String" />
 /// representation encoded with hex digits.
 /// </summary>
 /// <param name="array">An array of 8-bit unsigned integers</param>
 /// <param name="lowerCase">If set to true, produces a lowercase string.</param>
 public static string ToBase16String(byte[] array, bool lowerCase)
 {
     Precondition.Require(array, () => Error.ArgumentNull("array"));
     return(ToBase16String(array, 0, array.Length, lowerCase));
 }
Ejemplo n.º 23
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));
 }