|
PeanutButter
|
Provides a set of convenience extensions on everything. More...
Static Public Member Functions | |
| static bool | DeepEquals (this object objSource, object objCompare, params string[] ignorePropertiesByName) |
| Runs a deep equality test between two objects, glossing over reference differences between class-types and comparing only primitive types. Use this when you'd like to essentially test whether the data in one object hierarchy matches that of another. | |
| static bool | ShapeEquals (this object objSource, object objCompare, params string[] ignorePropertiesByName) |
| Tests if two objects have the same "shape" – basically Deep Equality testing without actually testing final property values. | |
| static bool | ShapeSubEquals (this object objSource, object objCompare, params string[] ignorePropertiesByName) |
| Tests if a second object has at least the "shape" of a primary one. Basically a DeepSubEquals without testing final property values. without actually testing final property values. | |
| static bool | DeepEquals (this object objSource, object objCompare, ObjectComparisons comparison, params string[] ignorePropertiesByName) |
| Runs a deep equality test between two objects, glossing over reference differences between class-types and comparing only primitive types. Use this when you'd like to essentially test whether the data in one object hierarchy matches that of another. | |
| static bool | DeepEquals (this object objSource, object objCompare, Action< string > failureLogAction, params string[] ignorePropertiesByName) |
| Runs a deep equality test between two objects, ignoring reference differences wherever possible and logging failures with the provided action. Properties can be excluded by name with the ignorePropertiesByName params. | |
| static bool | DeepSubEquals (this object objSource, object objCompare, params string[] ignorePropertiesByName) |
| Runs a deep equality test between two objects, using the properties on objSource (and children) as the set of properties to match on. | |
| static bool | DeepIntersectionEquals (this object objSource, object objCompare, params string[] ignorePropertiesByName) |
| Runs a deep equality test between two objects, using the properties common to both sides of the comparison to match on. | |
| static bool | ContainsAtLeastOneDeepEqualTo< T1, T2 > (this IEnumerable< T1 > collection, T2 item, params string[] ignoreProperties) |
| Searches a collection for one or more objects which DeepEquals the provided reference item. | |
| static bool | ContainsOneDeepEqualTo< T1, T2 > (this IEnumerable< T1 > collection, T2 item, params string[] ignoreProperties) |
| Searches a collection for a single object which DeepEquals the provided reference item. | |
| static bool | ContainsOneIntersectionEqualTo< T1, T2 > (this IEnumerable< T1 > collection, T2 item, params string[] ignoreProperties) |
| Searches a collection for an object which IntersectionEquals the provided reference item. | |
| static bool | ContainsOnlyOneDeepEqualTo< T1, T2 > (this IEnumerable< T1 > collection, T2 item, params string[] ignoreProperties) |
| Searches a collection for an object which DeepEquals the provided reference item. | |
| static bool | ContainsOnlyOneIntersectionEqualTo< T1, T2 > (this IEnumerable< T1 > collection, T2 item, params string[] ignoreProperties) |
| Searches a collection for an object which IntersectionEquals the provided reference item. | |
| static bool | ContainsOnlyOneMatching< T1, T2 > (this IEnumerable< T1 > collection, T2 item, Func< T1, T2, bool > comparer) |
| Searches a collection for an object which matches the provided reference item, according to the provided matcher Func. | |
| static T | CopyPropertiesTo< T > (this object src, T dst) |
| Copies all public primitive property values of intersecting properties from the source object to the target object, ala poor-man's AutoMapper. | |
| static T | CopyPropertiesTo< T > (this object src, T dst, params string[] ignoreProperties) |
| Copies all public primitive property values of intersecting properties from the source object to the target object, ala poor-man's AutoMapper. | |
| static T | CopyPropertiesTo< T > (this object src, T dst, bool deep, params string[] ignoreProperties) |
| Copies all public primitive property values of intersecting properties from the source object to the target object, ala poor-man's AutoMapper. | |
| static T | DeepClone< T > (this T item) |
| Creates a deep clone of the provided item, as far as possible Works on properties which are: | |
| static T | Get< T > (this object src, string propertyPath) |
| Gets the value of a property on an object, specified by the property path, of the given Type. | |
| static object | Get (this object src, string propertyPath, Type type) |
| Provides a non-generic interface to Get<T>. | |
| static T | GetOrDefault< T > (this object src, string propertyPath) |
| Gets the value of a property on an object, specified by the property path, of the given Type or returns a default value when that property cannot be found by path and/or type. | |
| static T | GetOrDefault< T > (this object src, string propertyPath, T defaultValue) |
| Gets the value of a property on an object, specified by the property path, of the given Type or returns a default value when that property cannot be found by path and/or type. | |
| static T[] | AsArray< T > (this T input) |
| Fluency extension to wrap a single item in an array, eg: new SomeBusinessObject().AsArray().Union(SomeOtherCollection);. | |
| static T[] | InArray< T > (this T input) |
| Fluency extension to wrap a single item in an array, eg: new SomeBusinessObject().InArray().Union(SomeOtherCollection);. | |
| static List< T > | InList< T > (this T input) |
| Fluency extension to wrap a single item in a list, eg: new SomeBusinessObject().InList().Union(SomeOtherCollection);. | |
| static object | GetPropertyValue (this object src, string propertyPath) |
| Gets a property value by name from an object. | |
| static object | InvokeMethodWithResult (this object src, string methodName, params object[] args) |
| Invokes a method on an object, if available; otherwise explodes. | |
| static void | SetPropertyValue (this object src, string propertyPath, object newValue) |
| Attempts to set a property value on an object by property path. | |
| static void | Set< T > (this object src, string propertyPath, T newValue) |
| Attempts to set a property value on an object by property path. | |
| static bool | TrySet< T > (this object src, string propertyPath, T newValue) |
| attempts to set a property value on an object by path and value | |
| static T | GetPropertyValue< T > (this object src, string propertyPath) |
| Gets an immediate property value, cast to the specified type. | |
| static bool | IsAssignableTo< T > (this Type type) |
| Tests if a type is assignable to another type (inverse of IsAssignableFrom) | |
| static decimal | TruncateTo (this decimal value, int places) |
| Truncates a decimal value to a required number of places. | |
| static double | TruncateTo (this double value, int places) |
| Truncates a decimal value to a required number of places. | |
| static decimal | ToFixed (this decimal value, int places) |
| Provides a similar api to Javascript's .toFixed(), except returning a useful decimal! Note: this is different from .TruncateTo since that will truncate the value, whereas this will round. | |
| static double | ToFixed (this double value, int places) |
| Provides a similar api to Javascript's .toFixed(), except returning a useful double! Note: this is different from .TruncateTo since that will truncate the value, whereas this will round. | |
| static IEnumerable< T > | AsEnumerable< T > (this object src) |
| Attempts to convert any object to an IEnumerable<T>. | |
| static bool | TryChangeType< T > (this object input, out T output) |
| Analogous to TryParse methods, this will attempt to convert a value to the type T, returning true if it can, and populating the output parameter. | |
| static bool | TryChangeType (this object input, Type requiredType, out object output) |
| Analogous to TryParse methods, this will attempt to convert a value to the type requiredType, returning true if it can, and populating the output parameter. | |
| static bool | IsInstanceOf< T > (this object obj) |
| Tests if the given object is an instance of the type T. | |
| static bool | IsInstanceOf (this object obj, Type type) |
| Tests if the given object is an instance of the provided type. | |
| static bool | IsRuntimeType (this object obj) |
| Determines whether or not an arbitrary object is a RuntimeType. | |
| static bool | HasProperty (this object obj, string prop) |
| Tests if the object has the requested property by path. | |
| static bool | HasProperty< T > (this object obj, string prop) |
| Tests whether the object has the named property with the type T. | |
| static bool | HasProperty (this object obj, string prop, Type propertyType) |
| Tests if the object has the named property with the expected type. | |
| static bool | TryGet< T > (this object obj, string propertyPath, out T result) |
| Attempt to retrieve the named property with the given type off of the provided object. | |
| static bool | Is (this object original, object test) |
| Tests if two objects are the same reference in memory (ie, short-hand for object.ReferenceEquals(original, test);. | |
| static IDictionary< TKey, TValue > | AsDictionary< TKey, TValue > (this object obj) |
| Attempts to provide a dictionary representation for the provided object. If the provided object already implements IDictionary<TKey, TValue> then you'll get the same instance back - be careful to clone it if you don't want to mutate the original. | |
| static IDictionary< string, string > | AsDictionary< T > (this T value, Func< T, IEnumerable< string > > keyFetcher, Func< T, string, string > valueFetcher) |
| Produces a string-string dictionary from any value, given that value and two funcs: | |
| static T | DeserializeFromJson< T > (this string json) |
| Deserializes a json string to the type T, by first figuring out if T's properties are decorated for System.Text.Json or Newtonsoft.Json and then choosing the serializer accordingly. | |
| static string | SerializeToJson< T > (this T item) |
| Serializes an object of type T to json, by first figuring out if T's properties are decorated for System.Text.Json or Newtonsoft.Json and then choosing the serializer accordingly. | |
Provides a set of convenience extensions on everything.
|
static |
Fluency extension to wrap a single item in an array, eg: new SomeBusinessObject().AsArray().Union(SomeOtherCollection);.
| input | The item to wrap |
| T | The type of the object |
|
static |
Produces a string-string dictionary from any value, given that value and two funcs:
| value | |
| keyFetcher | |
| valueFetcher |
| T |
|
static |
Attempts to provide a dictionary representation for the provided object. If the provided object already implements IDictionary<TKey, TValue> then you'll get the same instance back - be careful to clone it if you don't want to mutate the original.
| obj |
| NotSupportedException |
|
static |
Attempts to convert any object to an IEnumerable<T>.
| src | Object to operate on |
| T | Desired collection element type |
|
static |
Searches a collection for one or more objects which DeepEquals the provided reference item.
| collection | Collection of objects to search |
| item | Item to find a match for |
| ignoreProperties | Optional params array of properties to ignore by name |
| T1 | Item type of the collection |
| T2 | Type of the comparison item (can be the same as or different from T1) |
|
static |
Searches a collection for a single object which DeepEquals the provided reference item.
| collection | Collection of objects to search |
| item | Item to find a match for |
| ignoreProperties | Optional params array of properties to ignore by name |
| T1 | Item type of the collection |
| T2 | Type of the comparison item (can be the same as or different from T1) |
|
static |
Searches a collection for an object which IntersectionEquals the provided reference item.
| collection | Collection of objects to search |
| item | Item to find a match for |
| ignoreProperties | Optional params array of properties to ignore by name |
| T1 | Item type of the collection |
| T2 | Type of the comparison item (can be the same as or different from T1) |
|
static |
Searches a collection for an object which DeepEquals the provided reference item.
| collection | Collection of objects to search |
| item | Item to find a match for |
| ignoreProperties | Optional params array of properties to ignore by name |
| T1 | Item type of the collection |
| T2 | Type of the comparison item (can be the same as or different from T1) |
|
static |
Searches a collection for an object which IntersectionEquals the provided reference item.
| collection | Collection of objects to search |
| item | Item to find a match for |
| ignoreProperties | Optional params array of properties to ignore by name |
| T1 | Item type of the collection |
| T2 | Type of the comparison item (can be the same as or different from T1) |
|
static |
Searches a collection for an object which matches the provided reference item, according to the provided matcher Func.
| collection | Collection of objects to search |
| item | Item to find a match for |
| comparer | Func to use to perform comparison |
| T1 | Item type of the collection |
| T2 | Type of the comparison item (can be the same as or different from T1) |
|
static |
Copies all public primitive property values of intersecting properties from the source object to the target object, ala poor-man's AutoMapper.
| src | Source object |
| dst | Target object |
|
static |
Copies all public primitive property values of intersecting properties from the source object to the target object, ala poor-man's AutoMapper.
| src | Source object |
| dst | Target object |
| deep | Flag: whether the process should copy deep (ie, traverse into child objects) |
| ignoreProperties |
|
static |
Copies all public primitive property values of intersecting properties from the source object to the target object, ala poor-man's AutoMapper.
| src | Source object |
| dst | Target object |
| ignoreProperties | Optional list of properties to ignore by name |
|
static |
Creates a deep clone of the provided item, as far as possible Works on properties which are:
| item | Item to clone |
| T | Type of the item to clone |
|
static |
Runs a deep equality test between two objects, ignoring reference differences wherever possible and logging failures with the provided action. Properties can be excluded by name with the ignorePropertiesByName params.
| objSource | |
| objCompare | |
| failureLogAction | |
| ignorePropertiesByName |
|
static |
Runs a deep equality test between two objects, glossing over reference differences between class-types and comparing only primitive types. Use this when you'd like to essentially test whether the data in one object hierarchy matches that of another.
| objSource | Object which is the source of truth |
| objCompare | Object to compare with |
| comparison | Method for comparison |
| ignorePropertiesByName | Params array of properties to ignore by name |
|
static |
Runs a deep equality test between two objects, glossing over reference differences between class-types and comparing only primitive types. Use this when you'd like to essentially test whether the data in one object hierarchy matches that of another.
| objSource | Object which is the source of truth |
| objCompare | Object to compare with |
| ignorePropertiesByName | Params array of properties to ignore by name |
|
static |
Runs a deep equality test between two objects, using the properties common to both sides of the comparison to match on.
| objSource | Source object to perform comparison against |
| objCompare | Comparison object to compare |
| ignorePropertiesByName | Optional params array of properties to ignore by name |
|
static |
Runs a deep equality test between two objects, using the properties on objSource (and children) as the set of properties to match on.
| objSource | Source object to perform comparison against |
| objCompare | Comparison object to compare |
| ignorePropertiesByName | Optional params array of properties to ignore by name |
|
static |
Deserializes a json string to the type T, by first figuring out if T's properties are decorated for System.Text.Json or Newtonsoft.Json and then choosing the serializer accordingly.
| json |
| T |
| NotImplementedException |
|
static |
Provides a non-generic interface to Get<T>.
| src | |
| propertyPath | |
| type |
|
static |
Gets the value of a property on an object, specified by the property path, of the given Type.
| src | Object to search for the required property |
| propertyPath | Path to the property: may be a property name or a dotted path down an object hierarchy, eg: Company.Name |
| T | Expected type of the property value |
|
static |
Gets the value of a property on an object, specified by the property path, of the given Type or returns a default value when that property cannot be found by path and/or type.
| src | |
| propertyPath |
| T |
|
static |
Gets the value of a property on an object, specified by the property path, of the given Type or returns a default value when that property cannot be found by path and/or type.
| src | |
| propertyPath | |
| defaultValue |
| T |
|
static |
Gets a property value by name from an object.
| src | Source object |
| propertyPath | Name of the property to search for |
| MemberNotFoundException | Thrown when the property is not found by name |
|
static |
Gets an immediate property value, cast to the specified type.
| src | Source object |
| propertyPath | Immediate property name |
| T | Required type |
|
static |
Tests if the object has the requested property by path.
| obj | |
| prop |
|
static |
Tests if the object has the named property with the expected type.
| obj | |
| prop | |
| propertyType |
|
static |
Tests whether the object has the named property with the type T.
| obj | |
| prop |
| T |
|
static |
Fluency extension to wrap a single item in an array, eg: new SomeBusinessObject().InArray().Union(SomeOtherCollection);.
| input | The item to wrap |
| T | The type of the object |
|
static |
Fluency extension to wrap a single item in a list, eg: new SomeBusinessObject().InList().Union(SomeOtherCollection);.
| input | The item to wrap |
| T | The type of the object |
|
static |
Invokes a method on an object, if available; otherwise explodes.
| src | Object to invoke the method on |
| methodName | Method to invoke, by name |
| args | Any parameters to give to the method |
|
static |
Tests if two objects are the same reference in memory (ie, short-hand for object.ReferenceEquals(original, test);.
| original | |
| test |
|
static |
Tests if a type is assignable to another type (inverse of IsAssignableFrom)
| type | Type to operate on |
| T | Type to check assignment possibility against |
|
static |
Tests if the given object is an instance of the provided type.
| obj | |
| type |
|
static |
Tests if the given object is an instance of the type T.
| obj |
| T |
|
static |
Determines whether or not an arbitrary object is a RuntimeType.
| obj |
|
static |
Serializes an object of type T to json, by first figuring out if T's properties are decorated for System.Text.Json or Newtonsoft.Json and then choosing the serializer accordingly.
| item |
| T |
| NotImplementedException |
|
static |
Attempts to set a property value on an object by property path.
| src | |
| propertyPath | |
| newValue |
| T |
|
static |
Attempts to set a property value on an object by property path.
| src | Source object to set property on |
| propertyPath | Path into the property: could be an immediate property name or something like "Company.Name" |
| newValue | New value to attempt to set the property to |
| MemberNotFoundException | Thrown when the property cannot be found |
|
static |
Tests if two objects have the same "shape" – basically Deep Equality testing without actually testing final property values.
| objSource | Source / Master object |
| objCompare | Comparison object |
| ignorePropertiesByName | Ignore these properties by name |
|
static |
Tests if a second object has at least the "shape" of a primary one. Basically a DeepSubEquals without testing final property values. without actually testing final property values.
| objSource | Source / Master object |
| objCompare | Comparison object |
| ignorePropertiesByName | Ignore these properties by name |
|
static |
Provides a similar api to Javascript's .toFixed(), except returning a useful decimal! Note: this is different from .TruncateTo since that will truncate the value, whereas this will round.
| value | Source decimal value |
| places | Number of decimal places required |
|
static |
Provides a similar api to Javascript's .toFixed(), except returning a useful double! Note: this is different from .TruncateTo since that will truncate the value, whereas this will round.
| value | Source double value |
| places | Number of double places required |
|
static |
Truncates a decimal value to a required number of places.
| value | Source decimal value |
| places | Number of decimal places required |
|
static |
Truncates a decimal value to a required number of places.
| value | Source decimal value |
| places | Number of decimal places required |
|
static |
Analogous to TryParse methods, this will attempt to convert a value to the type requiredType, returning true if it can, and populating the output parameter.
| input | Value to work on |
| requiredType | The required type |
| output | Output parameter to collect result |
|
static |
Analogous to TryParse methods, this will attempt to convert a value to the type T, returning true if it can, and populating the output parameter.
| input | Value to work on |
| output | Output parameter to collect result |
| T | Desired type |
|
static |
Attempt to retrieve the named property with the given type off of the provided object.
| obj | |
| propertyPath | |
| result |
| T |
|
static |
attempts to set a property value on an object by path and value
| src | |
| propertyPath | |
| newValue |
| T |