/// <summary> /// Sets the first property with the given <paramref name="name"/> on the given <paramref name="obj"/> object /// to the supplied <paramref name="value"/>. Returns true if a value was assigned to a property and false otherwise. /// Use the <paramref name="bindingFlags"/> parameter to limit the scope of the search. /// </summary> /// <param name="obj">The source object on which to find the property</param> /// <param name="name">The name of the property whose value should be retrieved</param> /// <param name="value">The value that should be assigned to the property</param> /// <param name="bindingFlags">A combination of Flags that define the scope of the search</param> /// <returns>True if the value was assigned to a property and false otherwise</returns> public static bool TrySetPropertyValue(this object obj, string name, object value, Flags bindingFlags) { try { obj.SetPropertyValue(name, value, bindingFlags); return(true); } catch (MissingMemberException) { return(false); } }
/// <summary> /// Sets the property specified by <param name="name"/> matching <param name="bindingFlags"/> /// on the given <param name="obj"/> to the specified <param name="value" />. /// </summary> /// <returns><paramref name="obj"/>.</returns> public static object SetPropertyValue(this object obj, string name, object value, Flags bindingFlags) { DelegateForSetPropertyValue(obj.GetTypeAdjusted(), name, bindingFlags)(obj, value); return(obj); }
/// <summary> /// Gets the value of the property specified by <param name="name"/> matching <param name="bindingFlags"/> /// on the given <param name="obj"/>. /// </summary> public static object GetPropertyValue(this object obj, string name, Flags bindingFlags) { return(DelegateForGetPropertyValue(obj.GetTypeAdjusted(), name, bindingFlags)(obj)); }
/// <summary> /// Sets the value of the indexer matching <paramref name="bindingFlags"/> of the given <paramref name="obj"/> /// </summary> /// <param name="obj">The object whose indexer is to be set.</param> /// <param name="bindingFlags">The binding flags used to lookup the indexer.</param> /// <param name="parameters">The list of the indexer parameters plus the value to be set to the indexer. /// The parameter types are determined from these parameters, therefore no parameter can be <c>null</c>. /// If any parameter is <c>null</c> (or you can't be sure of that, i.e. receive from a variable), /// use a different overload of this method.</param> /// <returns>The object whose indexer is to be set.</returns> /// <example> /// If the indexer is of type <c>string</c> and accepts one parameter of type <c>int</c>, this /// method should be invoked as follow: /// <code> /// obj.SetIndexer(new Type[]{typeof(int), typeof(string)}, new object[]{1, "a"}); /// </code> /// </example> public static object SetIndexer(this object obj, Flags bindingFlags, params object[] parameters) { DelegateForSetIndexer(obj.GetTypeAdjusted(), bindingFlags, parameters.ToTypeArray())(obj, parameters); return(obj); }
/// <summary> /// Gets the value of the indexer matching <paramref name="bindingFlags"/> of the given <paramref name="obj"/> /// </summary> /// <param name="obj">The object whose indexer is to be retrieved.</param> /// <param name="parameterTypes">The types of the indexer parameters (must be in the right order).</param> /// <param name="bindingFlags">The binding flags used to lookup the indexer.</param> /// <param name="parameters">The list of the indexer parameters.</param> /// <returns>The value returned by the indexer.</returns> public static object GetIndexer(this object obj, Type[] parameterTypes, Flags bindingFlags, params object[] parameters) { return(DelegateForGetIndexer(obj.GetTypeAdjusted(), bindingFlags, parameterTypes)(obj, parameters)); }
/// <summary> /// Returns a new Flags instance returns a new Flags instance with the values from <paramref name="flags"/> /// that were not in <paramref name="mask"/> if <paramref name="condition"/> is true, and otherwise returns /// the supplied <paramref name="flags"/>. /// </summary> public static Flags ClearIf(Flags flags, Flags mask, bool condition) { return(condition ? (Flags)(flags & ~mask) : flags); }
/// <summary> /// Creates a delegate which can get the value of the property specified by <param name="name"/> /// matching <param name="bindingFlags"/> on the given <param name="type"/>. /// </summary> public static MemberGetter DelegateForGetPropertyValue(this Type type, string name, Flags bindingFlags) { var callInfo = new CallInfo(type, null, bindingFlags, MemberTypes.Property, name, null, null, true); return((MemberGetter) new MemberGetEmitter(callInfo).GetDelegate()); }
/// <summary> /// Returns a new Flags instance with the union of the values from <paramref name="flags"/> and /// <paramref name="mask"/> if <paramref name="condition"/> is true, and otherwise returns a new /// Flags instance with the values from <paramref name="flags"/> that were not in <paramref name="mask"/>. /// </summary> public static Flags SetOnlyIf(Flags flags, Flags mask, bool condition) { return(condition ? flags | mask : (Flags)(flags & ~mask)); }
/// <summary> /// Returns a new Flags instance with the union of the values from <paramref name="flags"/> and /// <paramref name="mask"/> if <paramref name="condition"/> is true, and otherwise returns the /// supplied <paramref name="flags"/>. /// </summary> public static Flags SetIf(Flags flags, Flags mask, bool condition) { return(condition ? flags | mask : flags); }
/// <summary> /// Returns true if all values in the given <paramref name="mask"/> are not set in the current Flags instance. /// </summary> public bool IsNotSet(Flags mask) { return((flags & mask) == 0); }
/// <summary> /// Returns true if at least one of the values in the given <paramref name="mask"/> are set in the current Flags instance. /// </summary> public bool IsAnySet(Flags mask) { return((flags & mask) != 0); }
/// <summary> /// Returns true if all values in the given <paramref name="mask"/> are set in the current Flags instance. /// </summary> public bool IsSet(Flags mask) { return((flags & mask) == mask); }