NamespaceSystemLibrary.Common.Net.Extensions SystemLibrary.Common.Net.dll
This class contains extension methods on Object
For instance: AsEnum(), AsEnumArray(), etc...
ObjectExtensions
public static class ObjectExtensions
Inheritance
System.Object
ObjectExtensions
Examples
Arguments
X
Methods
Convert multiple objects of the same 'enum value' to an array of that enum type
public static TEnum[] AsEnumArray<TEnum>(this object[] objects) where TEnum : struct, IComparable, IFormattable, IConvertible
X
public enum Colors
{
Black, White, Red, Blue
}
var integers = new object[] { 1, 2, 3, 4 };
var colors = integers.AsEnumArray<Colors>();
// colors is now an array of 'Colors', with one of each of the values:
// colors[0] == Black
// colors[1] == White
// ...
// colors[3] == Blue
public enum Colors
{
Black, White, Red, Blue
}
var texts = new object[] { "Red", "Black", "White" };
var colors = texts.AsEnumArray<Colors>();
// colors[1] == Black
X
Generic types
Name | Description |
---|---|
TEnum |
Methods arguments
Type | Name | Description |
---|---|---|
System.Object[] | objects |
X
Type | Description |
---|---|
TEnum[] |
Convert object to json
Default options are:
- case insensitive
- allows trailing commas
- camel cased
Returns a json formatted string representation of the object or null if object is null
public static string Json(this object obj, JsonSerializerOptions options = null, bool translateUnicodeCodepoints = false)
X
class User {
public string FirstName { get;set; }
}
var user = new User();
user.FirstName = "Hello World";
var json = user.Json();
var contains = json.Contains("firstName") && json.Contains("Hello World");
// contains is True, note that Json() has formatted 'FirstName' to camelCasing
X
Methods arguments
Type | Name | Description |
---|---|---|
System.Object | obj | |
System.Text.Json.JsonSerializerOptions | options | |
System.Boolean | translateUnicodeCodepoints |
X
Type | Description |
---|---|
System.String |
Convert object to json with your custom json converters
Returns a json formatted string representation of the object or null if object is null
public static string Json(this object obj, JsonSerializerOptions options, params JsonConverter[] jsonConverters)
X
class User {
public string FirstName { get;set; }
}
class CustomConverter : JsonConverter...
var user = new User();
user.FirstName = "Hello World";
var json = user.Json(new CustomConverter());
var contains = json.Contains("firstName") && json.Contains("Hello World");
// contains is True, note that Json() has formatted 'FirstName' to camelCasing
X
Methods arguments
Type | Name | Description |
---|---|---|
System.Object | obj | |
System.Text.Json.JsonSerializerOptions | options | |
System.Text.Json.Serialization.JsonConverter[] | jsonConverters |
X
Type | Description |
---|---|
System.String |
Convert object to json with your custom json converters
Returns a json formatted string representation of the object or null if object is null
public static string Json(this object obj, params JsonConverter[] jsonConverters)
X
class User {
public string FirstName { get;set; }
}
class CustomConverter : JsonConverter...
var user = new User();
user.FirstName = "Hello World";
var json = user.Json(new CustomConverter());
var contains = json.Contains("firstName") && json.Contains("Hello World");
// contains is True, note that Json() has formatted 'FirstName' to camelCasing
X
Methods arguments
Type | Name | Description |
---|---|---|
System.Object | obj | |
System.Text.Json.Serialization.JsonConverter[] | jsonConverters |
X
Type | Description |
---|---|
System.String |
}