Show / Hide Table of Contents

    Class ObjectExtensions

    Object 扩展方法

    Inheritance
    Object
    ObjectExtensions
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: Lett.Extensions
    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
    Name Description
    T

    目标类型

    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
    Name Description
    T

    目标类型

    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
    Type Name Description
    Object this
    Func<T> func
    Returns
    Type Description
    T

    转换失败返回 func

    Type Parameters
    Name Description
    T

    目标类型

    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 Description
    T
    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
    Type Condition
    ArgumentException

    目标类型 T 需要支持序列化

    | Improve this Doc View Source

    GetTypeName(Object)

    获取当前对象类型名称

    Declaration
    public static string GetTypeName(this object this)
    Parameters
    Type Name Description
    Object this
    Returns
    Type Description
    String

    this 为空 返回 Empty

    Examples
    var obj1 = new MyClass();
    obj1.GetTypeName(); // "MyClass"
    var obj2 = "";
    obj2.GetTypeName(); // "String"
    object obj4 = null;
    obj4.GetTypeName(); // string.Empty
    Exceptions
    Type Condition
    NullReferenceException
    | Improve this Doc View Source

    In<T>(T, IEnumerable<T>)

    当前对象是否存在于items集合内

    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
    Name Description
    T

    对象类型

    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
    Type Condition
    ArgumentNullException
    | Improve this Doc View Source

    In<T>(T, IEnumerable<T>, IEqualityComparer<T>)

    当前对象是否存在于items集合内

    Declaration
    public static bool In<T>(this T this, IEnumerable<T> items, IEqualityComparer<T> comparer)
    Parameters
    Type Name Description
    T this
    IEnumerable<T> items

    进行比较的集合

    IEqualityComparer<T> comparer

    比较器

    Returns
    Type Description
    Boolean

    items 为空 返回 false

    Type Parameters
    Name Description
    T

    对象类型

    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[])

    当前对象是否存在于 items 数组内

    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
    Name Description
    T
    Examples
    "a".InParams("A", "a"); // true
    string s = null;
    s.InParams("a"); // false
    | Improve this Doc View Source

    InParams<T>(T, IEqualityComparer<T>, T[])

    当前对象是否存在于 items 数组内

    Declaration
    public static bool InParams<T>(this T this, IEqualityComparer<T> comparer, params T[] items)
    Parameters
    Type Name Description
    T this
    IEqualityComparer<T> comparer

    比较器

    T[] items

    params T[]

    Returns
    Type Description
    Boolean

    items 为空 返回 false

    Type Parameters
    Name Description
    T
    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)

    当前对象类型是否 Array

    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
    Type Condition
    NullReferenceException
    | 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
    Type Description
    Boolean
    | Improve this Doc View Source

    IsClass(Object)

    当前对象类型是否 Class

    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
    Type Condition
    NullReferenceException
    | Improve this Doc View Source

    IsDBNull(Object)

    是否DBNull

    Declaration
    public static bool IsDBNull(this object this)
    Parameters
    Type Name Description
    Object this
    Returns
    Type Description
    Boolean
    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
    Type Condition
    NullReferenceException
    | Improve this Doc View Source

    IsNull(Object)

    是否 null

    Declaration
    public static bool IsNull(this object this)
    Parameters
    Type Name Description
    Object this
    Returns
    Type Description
    Boolean
    | Improve this Doc View Source

    IsNullOrDbNull(Object)

    是否 Null 或 DBNull

    Declaration
    public static bool IsNullOrDbNull(this object this)
    Parameters
    Type Name Description
    Object this
    Returns
    Type Description
    Boolean
    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
    Type Description
    Boolean
    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
    Type Condition
    NullReferenceException
    | 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
    Type Condition
    NullReferenceException
    | Improve this Doc View Source

    NotIn<T>(T, IEnumerable<T>)

    当前对象是否不存在于items集合内

    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
    Name Description
    T

    对象类型

    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
    Type Condition
    ArgumentNullException
    | Improve this Doc View Source

    NotIn<T>(T, IEnumerable<T>, IEqualityComparer<T>)

    当前对象是否不存在于items集合内

    Declaration
    public static bool NotIn<T>(this T this, IEnumerable<T> items, IEqualityComparer<T> comparer)
    Parameters
    Type Name Description
    T this
    IEnumerable<T> items

    进行比较的集合

    IEqualityComparer<T> comparer

    比较器

    Returns
    Type Description
    Boolean

    items 为空 返回 false

    Type Parameters
    Name Description
    T

    对象类型

    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[])

    当前对象是否不存在于 items 数组内

    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
    Name Description
    T
    Examples
    "a".NotInParams("A", "a"); // fase
    string s = null;
    s.InParams("a"); // false
    | Improve this Doc View Source

    NotInParams<T>(T, IEqualityComparer<T>, T[])

    当前对象是否不存在于 items 数组内

    Declaration
    public static bool NotInParams<T>(this T this, IEqualityComparer<T> comparer, params T[] items)
    Parameters
    Type Name Description
    T this
    IEqualityComparer<T> comparer

    比较器

    T[] items

    params T[]

    Returns
    Type Description
    Boolean

    items 为空 返回 false

    Type Parameters
    Name Description
    T
    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>)

    管道操作 - action

    Declaration
    public static T Pipe<T>(this T this, Action<T> action)
    Parameters
    Type Name Description
    T this
    Action<T> action
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T
    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
    Type Condition
    ArgumentNullException

    this is null

    | Improve this Doc View Source

    Pipe<TSource, TResult>(TSource, Func<TSource, TResult>)

    管道操作 - func

    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 Description
    TResult
    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
    Type Condition
    ArgumentNullException

    this is null

    | Improve this Doc View Source

    SaveAsFile<T>(T, String)

    保存为文件

    FileMode.Create | formatter: BinaryFormatter

    Declaration
    public static void SaveAsFile<T>(this T this, string filePath)
    Parameters
    Type Name Description
    T this
    String filePath
    Type Parameters
    Name Description
    T
    | Improve this Doc View Source

    SaveAsFile<T>(T, String, FileMode)

    保存为文件

    formatter: BinaryFormatter

    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
    Name Description
    T
    | 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 Name Description
    T this
    String filePath

    文件路径

    FileMode fileMode

    文件打开方式

    IFormatter formatter

    格式化器

    Type Parameters
    Name Description
    T

    需要 ISerializable

    Exceptions
    Type Condition
    SerializationException

    this is not marked as serializable

    | 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
    Name Description
    T

    泛型约束 IConvertible

    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
    Name Description
    T

    泛型约束 IConvertible

    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
    Type Name Description
    Object this
    Func<T> func
    Returns
    Type Description
    T

    转换失败则返回 func

    Type Parameters
    Name Description
    T

    泛型约束 IConvertible

    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)
    • Improve this Doc
    • View Source
    Back to top Copyright (c) 2019 viacooky. All rights reserved.