Class ObjectExtensions
Inheritance
ObjectExtensions
Assembly: Lett.Extensions.dll
Syntax
public static class ObjectExtensions
Methods
|
Improve this Doc
View Source
As<T>(Object)
Declaration
public static T As<T>(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Type |
Description |
T |
转换失败返回 default(T )
|
Type Parameters
Examples
var s = new ClassA();
var rs = s.As<BaseClass>(); // if failed, rs == default(BaseClass)
|
Improve this Doc
View Source
As<T>(Object, T)
Declaration
public static T As<T>(this object this, T defaultValue)
Parameters
Type |
Name |
Description |
Object |
this |
|
T |
defaultValue |
|
Returns
Type |
Description |
T |
转换失败返回 defaultValue
|
Type Parameters
Examples
var s = new ClassA();
s.As<BaseClass>();
|
Improve this Doc
View Source
As<T>(Object, Func<T>)
Declaration
public static T As<T>(this object this, Func<T> func)
Parameters
Returns
Type |
Description |
T |
转换失败返回 func
|
Type Parameters
Examples
var s = new ClassA();
s.As<BaseClass>(() => new BaseClass());
|
Improve this Doc
View Source
DeepClone<T>(T)
深复制
this
为空时, 返回 default<T>
Declaration
public static T DeepClone<T>(this T this)
Parameters
Type |
Name |
Description |
T |
this |
|
Returns
Type Parameters
Name |
Description |
T |
泛型约束 需要支持序列化 Serializable
|
Examples
[Serializable]
private class MyClass
{
public string Name { get; set; }
}
var obj = new MyClass{Name = "aa"};
var rs = obj.DeepClone();
// obj.Name == rs.Name
// obj != rs
Exceptions
|
Improve this Doc
View Source
GetTypeName(Object)
Declaration
public static string GetTypeName(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Examples
var obj1 = new MyClass();
obj1.GetTypeName(); // "MyClass"
var obj2 = "";
obj2.GetTypeName(); // "String"
object obj4 = null;
obj4.GetTypeName(); // string.Empty
Exceptions
|
Improve this Doc
View Source
In<T>(T, IEnumerable<T>)
Declaration
public static bool In<T>(this T this, IEnumerable<T> items)
Parameters
Type |
Name |
Description |
T |
this |
|
IEnumerable<T> |
items |
进行比较的集合
|
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
var dtItems = new[]
{
new DateTime(2018, 1, 1),
new DateTime(2019, 1, 1, 9, 10, 1),
DateTime.Today
};
var dt2 = DateTime.Today;
dt2.In(dtItems); // true
var stringItems = new[] {"a", "b", null};
var s = "a";
string s2 = null;
s.In(stringItems); // true
s2.In(stringItems); // true
string[] stringItems2 = null;
var s3 = "a";
s3.In(stringItems2); // false, stringItems2 is null always return false
Exceptions
|
Improve this Doc
View Source
In<T>(T, IEnumerable<T>, IEqualityComparer<T>)
Declaration
public static bool In<T>(this T this, IEnumerable<T> items, IEqualityComparer<T> comparer)
Parameters
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
public class MyComparer : IEqualityComparer<DateTime>
{
public bool Equals(DateTime x, DateTime y)
{
return x.Year.Equals(y.Year);
}
public int GetHashCode(DateTime obj)
{
return obj.GetHashCode();
}
}
var dtItems = new[]
{
new DateTime(2018, 1, 1),
new DateTime(2019, 1, 1, 9, 10, 1),
DateTime.Today
};
var dt = new DateTime(2019, 5, 25, 1, 1, 1);
dt.In(dtItems, new MyComparer()); // true
var strItems = new[] {"a", "A", "b"};
var s = "B";
s.In(strItems, StringComparer.CurrentCultureIgnoreCase); // true
s.In(strItems, StringComparer.CurrentCulture); // false
|
Improve this Doc
View Source
InParams<T>(T, T[])
Declaration
public static bool InParams<T>(this T this, params T[] items)
Parameters
Type |
Name |
Description |
T |
this |
|
T[] |
items |
params T []
|
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
"a".InParams("A", "a"); // true
string s = null;
s.InParams("a"); // false
|
Improve this Doc
View Source
InParams<T>(T, IEqualityComparer<T>, T[])
Declaration
public static bool InParams<T>(this T this, IEqualityComparer<T> comparer, params T[] items)
Parameters
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
"a".InParams(StringComparer.CurrentCultureIgnoreCase, "A", "B"); // true
"a".InParams(StringComparer.Ordinal, "A", "B"); // false
"a".InParams(StringComparer.Ordinal, null); // false
|
Improve this Doc
View Source
IsArray(Object)
Declaration
public static bool IsArray(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Type |
Description |
Boolean |
this 为 null ,返回 false
|
Examples
var obj1 = new[] {"1", "2"};
obj1.IsArray(); // true
var obj2 = new List<string> {"1", "2"};
obj2.IsArray(); // false
object obj3 = null;
obj3.IsArray(); // false
var obj4 = new MyClass();
obj4.IsArray(); // false
Exceptions
|
Improve this Doc
View Source
IsAssignableFrom(Object, Type)
Declaration
public static bool IsAssignableFrom(this object this, Type targetType)
Parameters
Type |
Name |
Description |
Object |
this |
|
Type |
targetType |
指定类型
|
Returns
|
Improve this Doc
View Source
IsClass(Object)
Declaration
public static bool IsClass(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Type |
Description |
Boolean |
this 为 null ,返回 false
|
Examples
var obj1 = new MyClass();
obj1.IsClass(); // true
var obj2 = 12;
obj2.IsClass(); // false
object obj3 = null;
obj3.IsClass(); // false
Exceptions
|
Improve this Doc
View Source
IsDBNull(Object)
Declaration
public static bool IsDBNull(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Examples
var value = DBNull.Value;
value.IsDBNull(); // true
|
Improve this Doc
View Source
IsEnum(Object)
Declaration
public static bool IsEnum(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Type |
Description |
Boolean |
this 为 null ,返回 false
|
Examples
private enum MyEnum
{
None
}
var obj1 = MyEnum.None;
obj1.IsEnum(); // true
Exceptions
|
Improve this Doc
View Source
IsNull(Object)
Declaration
public static bool IsNull(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
|
Improve this Doc
View Source
IsNullOrDbNull(Object)
Declaration
public static bool IsNullOrDbNull(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Examples
var v = DBNull.Value;
v.IsNullOrDbNull(); // true
var v2 = default(string); // null
v2.IsNullOrDbNull(); // true
|
Improve this Doc
View Source
IsSerializable(Object)
Declaration
public static bool IsSerializable(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Examples
var obj1 = new[] {"1", "2"};
obj1.IsSerializable(); // true
object obj3 = null;
obj3.IsSerializable(); // false
private class MyClass
{
public string Name { get; set; }
}
var obj4 = new MyClass();
obj4.IsSerializable(); // false
[Serializable]
private class MySerializableClass
{
public string Name { get; set; }
}
var obj5 = new MySerializableClass();
obj5.IsSerializable(); // true
Exceptions
|
Improve this Doc
View Source
IsValueType(Object)
Declaration
public static bool IsValueType(this object this)
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Type |
Description |
Boolean |
this 为 null ,返回 false
|
Examples
var obj1 = 12;
obj1.IsValueType(); // true
var obj2 = "";
obj2.IsValueType(); // false
var obj4 = new MyStruct();
obj4.IsValueType(); // true
var obj5 = new DateTime();
obj5.IsValueType(); // true
Exceptions
|
Improve this Doc
View Source
NotIn<T>(T, IEnumerable<T>)
Declaration
public static bool NotIn<T>(this T this, IEnumerable<T> items)
Parameters
Type |
Name |
Description |
T |
this |
|
IEnumerable<T> |
items |
进行比较的集合
|
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
var dtItems = new[]
{
new DateTime(2018, 1, 1),
new DateTime(2019, 1, 1, 9, 10, 1),
DateTime.Today
};
var dt2 = DateTime.Today;
dt2.NotIn(dtItems); // false
var stringItems = new[] {"a", "b", null};
var s = "a";
string s2 = null;
s.NotIn(stringItems); // false
s2.NotIn(stringItems); // false
string[] stringItems2 = null;
var s3 = "a";
s3.NotIn(stringItems2); // true
Exceptions
|
Improve this Doc
View Source
NotIn<T>(T, IEnumerable<T>, IEqualityComparer<T>)
Declaration
public static bool NotIn<T>(this T this, IEnumerable<T> items, IEqualityComparer<T> comparer)
Parameters
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
var dtItems = new[] {
new DateTime(2018, 1, 1),
new DateTime(2019, 1, 1, 9, 10, 1),
DateTime.Today
};
var dt = new DateTime(2019, 5, 25, 1, 1, 1);
dt.NotIn(dtItems, new MyComparer()); // false
|
Improve this Doc
View Source
NotInParams<T>(T, T[])
Declaration
public static bool NotInParams<T>(this T this, params T[] items)
Parameters
Type |
Name |
Description |
T |
this |
|
T[] |
items |
params T []
|
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
"a".NotInParams("A", "a"); // fase
string s = null;
s.InParams("a"); // false
|
Improve this Doc
View Source
NotInParams<T>(T, IEqualityComparer<T>, T[])
Declaration
public static bool NotInParams<T>(this T this, IEqualityComparer<T> comparer, params T[] items)
Parameters
Returns
Type |
Description |
Boolean |
items 为空 返回 false
|
Type Parameters
Examples
"a".NotInParams(StringComparer.CurrentCultureIgnoreCase, "A", "B"); // false
"a".NotInParams(StringComparer.Ordinal, "A", "B"); // true
"a".NotInParams(StringComparer.Ordinal, null); // true
|
Improve this Doc
View Source
Pipe<T>(T, Action<T>)
Declaration
public static T Pipe<T>(this T this, Action<T> action)
Parameters
Type |
Name |
Description |
T |
this |
|
Action<T> |
action |
|
Returns
Type Parameters
Examples
var obj1 = new MyClass();
var rs = obj1.Pipe(o => { o.Name += "pipe1";})
.Pipe(o => { o.Name += "_pipe2";})
.Pipe(o => { o.Name += "_pipe3";})
.Pipe(o => { o.Name += "_pipe4";})
.Pipe(o => { o.Name += "_pipe5";});
// rs.Name == "pipe1_pipe2_pipe3_pipe4_pipe5";
Exceptions
|
Improve this Doc
View Source
Pipe<TSource, TResult>(TSource, Func<TSource, TResult>)
Declaration
public static TResult Pipe<TSource, TResult>(this TSource this, Func<TSource, TResult> func)
Parameters
Type |
Name |
Description |
TSource |
this |
|
Func<TSource, TResult> |
func |
|
Returns
Type Parameters
Name |
Description |
TSource |
|
TResult |
|
Examples
var obj1 = new MyClass();
var rs2 = obj1.Pipe(o => { o.Name += "pipe1";})
.Pipe(o => { o.Name += "_pipe2";})
.Pipe(o => { o.Name += "_pipe3";})
.Pipe(o => { o.Name += "_pipe4";})
.Pipe(o => { o.Name += "_pipe5";})
.Pipe(o => o.Name.Replace("pipe", "")); // pipe func return: o.Name.Replace("pipe", "");
Assert.AreEqual(rs2, "1_2_3_4_5");
Exceptions
|
Improve this Doc
View Source
SaveAsFile<T>(T, String)
Declaration
public static void SaveAsFile<T>(this T this, string filePath)
Parameters
Type |
Name |
Description |
T |
this |
|
String |
filePath |
|
Type Parameters
|
Improve this Doc
View Source
SaveAsFile<T>(T, String, FileMode)
Declaration
public static void SaveAsFile<T>(this T this, string filePath, FileMode fileMode)
Parameters
Type |
Name |
Description |
T |
this |
|
String |
filePath |
文件路径
|
FileMode |
fileMode |
文件打开方式
|
Type Parameters
|
Improve this Doc
View Source
SaveAsFile<T>(T, String, FileMode, IFormatter)
Declaration
public static void SaveAsFile<T>(this T this, string filePath, FileMode fileMode, IFormatter formatter)
Parameters
Type Parameters
Exceptions
|
Improve this Doc
View Source
To<T>(Object)
Declaration
public static T To<T>(this object this)
where T : IConvertible
Parameters
Type |
Name |
Description |
Object |
this |
|
Returns
Type |
Description |
T |
转换失败则返回 default(T )
|
Type Parameters
Examples
var intVar = 11;
intVar.To<string>(); // "11"
var dateTimeStr = "2018-01-01 23:59:59";
var rs = dateTimeStr.To<DateTime>(); // rs = new DateTime(2018,1, 1, 23, 59, 59)
|
Improve this Doc
View Source
To<T>(Object, T)
Declaration
public static T To<T>(this object this, T defaultValue)
where T : IConvertible
Parameters
Type |
Name |
Description |
Object |
this |
|
T |
defaultValue |
|
Returns
Type |
Description |
T |
失败则返回 defaultValue
|
Type Parameters
Examples
var dateTimeStr = "2018-01-01 23:59:59xxxxxxxx"; // will be fail
var rs = dateTimeStr.To<DateTime>(new DateTime(2019, 4, 1)); // rs == new DateTime(2019, 4, 1)
|
Improve this Doc
View Source
To<T>(Object, Func<T>)
Declaration
public static T To<T>(this object this, Func<T> func)
where T : IConvertible
Parameters
Returns
Type |
Description |
T |
转换失败则返回 func
|
Type Parameters
Examples
var dateTimeStr = "2018-01-01 23:59:59xxxxxxxx"; // will be fail
var rs = dateTimeStr.To<DateTime>(() => new DateTime(2019,4,1)); // rs == new DateTime(2019, 4, 1)
var dateTimeStr = "2018-01-01 23:59:59"; // will be success
var rs = dateTimeStr.To<DateTime>(() => new DateTime(2019,4,1)); // rs == new DateTime(2018, 1, 1, 23, 59, 59)