Generic Extensions - adyle5/ExtensionsDotNet GitHub Wiki
Description
Performs a deep copy of an object.
Object must be a class and must be serializable.
Parameters
None
Usage
string str1 = "test";
string str2 = str1.DeepCopyExt();
Will str2 will be a deep copy of str1 that do not reference the same location in memory.
Description
Maps to Convert.ToBoolean
Converts value and returns new boolean.
Parameters
None
Usage
string t = "true";
bool b = t.ToBooleanExt();
Description
Maps to Convert.ToByte
Converts value and returns a new 8-bit unsigned integer.
Parameters
None
Usage
string t = "1";
byte b = t.ToByteExt();
Description
Maps to Convert.ToChar
Converts value and returns a new unicode character.
Parameters
None
Usage
string t = "c";
char c = t.ToCharExt();
Description
Maps to Convert.ToDecimal
Converts value and returns a new decimal number.
Parameters
None
Usage
string t = "1.25";
decimal d = t.ToDecimalExt();
Description
Maps to Convert.ToDouble
Converts value and returns a new double-precision floating-point number.
Parameters
None
Usage
string t = "1.25";
double d = t.ToDoubleExt();
Description
Converts a string to a float or throws a format exception.
Parameters
None
Usage
string t = "1.25";
float f = t.ToFloatExt();
Description
Maps to Convert.ToInt16
Converts value and returns a new 16-bit signed integer
Parameters
None
Usage
string t = "1";
short s = t.ToInt16Ext();
Description
Maps to Convert.ToInt32
Converts value and returns a new 32-bit signed integer
Parameters
None
Usage
string t = "1";
int s = t.ToIntExt();
Description
Deprecated. Use ToIntExt.
This extension will be removed in a future release.
Description
Maps to Convert.ToInt64
Converts value and returns a new 64-bit signed integer
Parameters
None
Usage
string t = "1";
long l = t.ToInt64Ext();
Description
Converts a string to a long or throws a Format Exception
Parameters
None
Usage
string t = "100";
long l = t.ToLongExt();
Description
Maps to Convert.ToSByte
Converts value and returns a new 8-bit signed integer
Parameters
None
Usage
string t = "100";
sbyte sb = t.ToSByteExt();
Description
Converts a string to a short or throws a format exception
Parameters
None
Usage
string t = "100";
short s = t.ToShortExt();
Description
Maps to Convert.ToSingle
Converts value and returns a new single-precision floating-point number
Parameters
None
Usage
string t = "1.5";
float f = t.ToSingleExt();
Description
Maps to [Convert.ToUInt16]https://docs.microsoft.com/en-us/dotnet/api/system.convert.touint16)
Converts value and returns a new 16-bit unsigned integer
Parameters
None
Usage
string t = "1";
ushort s = t.ToUInt16Ext();
Description
Maps to [Convert.ToUInt32]https://docs.microsoft.com/en-us/dotnet/api/system.convert.touint32)
Converts value and returns a new 32-bit unsigned integer
Parameters
None
Usage
string t = "1";
uint i = t.ToUInt32Ext();
Description
Maps to [Convert.ToUInt64]https://docs.microsoft.com/en-us/dotnet/api/system.convert.touint64)
Converts value and returns a new 64-bit unsigned integer
Parameters
None
Usage
string t = "1";
ulong l = t.ToUInt64Ext();
Description
Converts a stream.
Generated stream needs to be disposed or wrapped in using statement.
Parameters
None
Usage
int[] arr = { 1, 2, 3, 4, 5 };
using (Stream stream = arr.ToStreamExt())
{
...
}
or
Stream stream = arr.ToStreamExt();
...
stream.Dispose();
Description
Converts to a KeyValuePair with the value of the extended type as the key and the parameter as the value.
Parameters
value (U)
Usage
int key = 1;
string value = "red";
var kvp = key.ToKeyValuePairExt(value);
Description
Converts to a KeyValuePair with the value of the extended type as the value and the parameter as the key.
Parameters
key (T)
Usage
int key = 1;
string value = "red";
var kvp = value.ToKeyValuePair2Ext(key);
Description
Creates a new IEnumerable of the same type as the extended type and repeats the value of the extended times based on the integral parameter.
Maps to Enumerable.Repeat
Parameters
numberOfTimes (int)
Usage
int num = 3;
int repeat = 5;
IEnumerable<int> repeated = num.RepeatExt(repeat);
Description
Write's the value of a string or the ToString method to the Trace Listener.
Map of Trace.Write
Parameters
None
Usage
string text = "test";
text.TraceExt();
Writes test to the registered trace listener.
Description
Write's the value of a string or the ToString method and category specified to the Trace Listener.
Map of Trace.Write
Parameters
category (string)
Usage
string text = "test";
string category = "debug";
text.TraceExt(category);
Writes test to the registered trace listener with category of debug.
Description
Write's the value of a string or the ToString method specified to the Trace Listener based on a condition.
Map of Trace.WriteIf
Parameters
condition (bool)
Usage
string text = "test";
text.TraceIfExt(true);
Description
Write's the value of a string or the ToString method and category specified to the Trace Listener based on a condition.
Map of Trace.WriteIf
Parameters
condition (bool)
category (string)
Usage
string text = "test";
text.TraceIfExt(true, category);
Description
Write's the value of a string or the ToString method to a new line in the Trace Listener.
Map of Trace.WriteLine
Parameters
None
Usage
string text = "test";
text.TraceLineExt();
Writes test to a new line the registered trace listener.
Description
Write's the value of a string or the ToString method and category specified to a new line in the Trace Listener.
Map of Trace.WriteLine
Parameters
category (string)
Usage
string text = "test";
string category = "debug";
text.TraceLineExt(category);
Writes test to a new line the registered trace listener with category debug.
Description
Write's the value of a string or the ToString method specified to a new line in the Trace Listener based on a condition.
Map of Trace.WriteLineIf
Parameters
condition (bool)
Usage
string text = "test";
text.TraceLineIfExt(true);
Description
Write's the value of a string or the ToString method and category specified to a new line in the Trace Listener based on a condition.
Map of Trace.WriteLineIf
Parameters
condition (bool)
category (string)
Usage
string text = "test";
text.TraceLineIfExt(true, category);
Description
Returns a StringBuilder where the extended object is the beginning of the StringBuilder and one or more objects parameters are appended to the StringBuilder.
All objects must be of type char, bool, ulong, uint, byte, string, float, ushort object, decimal, short, int, long, or double.
Parameters
appends (params object[])
Usage
string hello = "Hello";
string world = "World";
string space = " ";
string exclaim = "!";
StringBuilder sb = hello.ToStringBuilderExt(space, world, exclaim);
sb.ToString() OUTPUTS: "Hello World!"
Description
Returns true if the object returned from the database is of type DBNull, otherwise returns false.
Maps to Convert.IsDBNull
Parameters
None
Usage
using var ctx = new TestModelContext();
ctx.Database.OpenConnection();
using var command = ctx.Database.GetDbConnection().CreateCommand();
command.CommandText = @"SELECT Description FROM Table1 WHERE Id = 1;";
object description = command.ExecuteScalar();
if (description.IsDBNullExt())
{
// This is true!
}
ctx.Database.CloseConnection();
Description
Get value from a description attribute.
Parameters
None
Usage
// [Description("First Name")]
// public string FName { get; set; }
string description = FName.GetDescriptionExt();
OUTPUT: "First Name"
Description
Returns a string representation of the values in a generic list that implements IConvertible separated by a comma.
Parameters
None
Usage
var list = new List<string>() { "one", "two", "three" };
var stringFromList = list.ToStringExt();
OUTPUT: "one,two,three"
Description
Returns true if the extended object is of type T, otherwise returns false.
Parameters
None
Usage
object o = "This is a string";
bool isString = o.IsExt<string>();
OUTPUT: true