Release Notes More
- Symbol.Cloud 移植;
- Symbol.Cloud 微服务家族;
- Symbol.Cloud 多种实现方式;
- .net framework 2.0;
- .net framework 3.5;
- .net framework 4.0;
- .net framework 4.5;
- .net framework 4.5.2;
- .net framework 4.6;
- .net framework 4.6.1;
- .net framework 4.7;
- .net framework 4.8;
- .net standard 2.0
- .net core app 3.1;
- .net 5.0;
- .net 6.0;
- .net 7.0;
- .net 8.0;
- Symbol.dll
- Symbol.Data.dll
- Symbol.Net.dll
- Symbol.Web.dll
- Symbol.Drawing.dll
- Symbol.IO.Packing.TreePackage.dll
- Symbol.ImageRecognition.Verification.dll
We gladly accept community contributions.
- Issues: Please report bugs using the Issues section of GitHub
- Source Code Contributions:
- See C# Coding Style for reference on coding style.
TypeExtensions
public static bool IsNullableType(this Type type)
返回为true表示此类型为struct类型,并且采用的是Nullable<T>。
Console.WriteLine( typeof(bool).IsNullableType() );
Console.WriteLine( typeof(bool?).IsNullableType() );
Console.WriteLine( typeof(string).IsNullableType() );
/*
False
True
False
*/
public static Type GetNullableType(this Type type)
如果为非可为空类型,返回的就是它自己,反之而是被包装的类型。
Console.WriteLine( typeof(bool).GetNullableType().FullName );
Console.WriteLine( typeof(bool?).GetNullableType().FullName );
Console.WriteLine( typeof(string).GetNullableType().FullName );
/*
System.Boolean
System.Boolean
System.String
*/
public static bool IsAnonymousType(this object value)
public static bool IsAnonymousType(this Type type)
为null返回false,反之为匿名对象时返回true。
var builder = new StringBuilder();
var info = new { name = "test" };
Console.WriteLine( builder.IsAnonymousType() );
Console.WriteLine( info.IsAnonymousType() );
Console.WriteLine( typeof(string).IsAnonymousType() );
/*
False
True
True
*/
public static bool IsSystemBaseType(this Type type)
为true表示为基础类型,比如string int。
Console.WriteLine( typeof(int).IsSystemBaseType() );
Console.WriteLine( typeof(Program).IsSystemBaseType() );
/*
True
False
*/
public static bool IsNumbericType(this Type type)
为null时返回false,反之为数字类型时返回为true。
已知:byte,short,int,uint,long,ulong,float,decimal,double
byte?,short?,int?,uint?,long?,ulong?,float?,decimal?,double?
Console.WriteLine( typeof(int).IsNumbericType() );
Console.WriteLine( typeof(string).IsNumbericType() );
Console.WriteLine( "test".IsNumbericType() );
Console.WriteLine( (3.42F).IsNumbericType() );
/*
True
False
False
True
*/
public static bool IsInheritFrom(this Type type, Type parent)
支持接口、类,返回true表示有继承关系。
interface IBase{ }
interface IFly : IBase { }
class Birds: IFly { }
class Pigeon : Birds { }
class Fish : IBase { }
static void Main()
{
Console.WriteLine( typeof(Pigeon).IsInheritFrom( typeof(Birds) ) );
Console.WriteLine( typeof(Pigeon).IsInheritFrom( typeof(IFly) ) );
Console.WriteLine( typeof(Pigeon).IsInheritFrom( typeof(IBase) ) );
Console.WriteLine( typeof(Fish).IsInheritFrom( typeof(IBase) ) );
Console.WriteLine( typeof(Fish).IsInheritFrom( typeof(Birds) ) );
Console.WriteLine( typeof(IFly).IsInheritFrom( typeof(IBase) ) );
}
/*
True
True
True
True
False
True
*/
ConvertExtensions
public static T Convert(this object value, T defaultValue) where T : struct
返回需要转换的类型,如果转换不成功时采用的默认值。
public static T Convert(this object value)
返回需要转换的类型。
public static object Convert(this object value, Type type)
返回需要转换的类型,DBNull识别为null,支持数组转换。
int count = "3".Convert<int>(0); //完整书写
int count1 = "3".Convert(0); //简写(推荐)
int count2 = "aa".Convert(-1);
Console.WriteLine(count);
Console.WriteLine(count1);
Console.WriteLine(count2);
/*
3
3
-1
*/
string text = (1024).Convert<string>();
string value = DBNull.Value.Convert<string>();
int? age = "32".Convert<int?>();
int? age1 = "aaa".Convert<int?>();
Console.WriteLine(date);
Console.WriteLine(text);
Console.WriteLine(value);
Console.WriteLine(age);
Console.WriteLine(age1);
/*
1024
null
32
null
*/
Console.WriteLine( "true".Convert<bool>() );
Console.WriteLine( "ok".Convert<bool>() );
Console.WriteLine( "yes".Convert<bool>() );
Console.WriteLine( "good".Convert<bool>() );
Console.WriteLine( "t".Convert<bool>() );
Console.WriteLine( "True".Convert<bool>() );
Console.WriteLine( "TRUE".Convert<bool>() );
Console.WriteLine( "1".Convert<bool>() );
Console.WriteLine( 1.Convert<bool>() );
Console.WriteLine( "-1".Convert<bool>() );
Console.WriteLine( (-1).Convert<bool>() );
Console.WriteLine( "----" );
Console.WriteLine( "false".Convert<bool>() );
Console.WriteLine( "no".Convert<bool>() );
Console.WriteLine( "f".Convert<bool>() );
Console.WriteLine( "bad".Convert<bool>() );
Console.WriteLine( "not".Convert<bool>() );
Console.WriteLine( "False".Convert<bool>() );
Console.WriteLine( "FALSE".Convert<bool>() );
Console.WriteLine( "0".Convert<bool>() );
Console.WriteLine( 0.Convert<bool>() );
/*
True
True
True
True
True
True
True
True
True
True
True
----
False
False
False
False
False
False
False
False
False
*/
Console.WriteLine( "utf-8".Convert<Encoding>().WebName );
Console.WriteLine( Encoding.UTF8.Convert<string>() );
Console.WriteLine( (65001).Convert<Encoding>().CodePage );
Console.WriteLine( (65001L).Convert<Encoding>().CodePage );
Console.WriteLine( Encoding.UTF8.Convert<int>() );
Console.WriteLine( Encoding.UTF8.Convert<long>() );
Console.WriteLine( "xxxx".Convert<Encoding>() );
/*
utf-8
utf-8
65001
65001
65001
65001
null
*/
Console.WriteLine( "ReadOnly, System".Convert<FileAttributes>() == (FileAttributes.ReadOnly | FileAttributes.System) );
Console.WriteLine( "readOnly,system".Convert<FileAttributes>() == (FileAttributes.ReadOnly | FileAttributes.System) );
Console.WriteLine( "readOnly, System".Convert<FileAttributes>() == FileAttributes.ReadOnly | FileAttributes.System );
Console.WriteLine( (5).Convert<FileAttributes>() == (FileAttributes.ReadOnly | FileAttributes.System) );
Console.WriteLine( "xxxx".Convert<FileAttributes?>() );
Console.WriteLine( "----" );
Console.WriteLine( (FileAttributes.ReadOnly | FileAttributes.System).Convert<string>() == "ReadOnly, System" );
Console.WriteLine( (FileAttributes.ReadOnly | FileAttributes.System).Convert<int>() == 5 );
Console.WriteLine( (FileAttributes.ReadOnly | FileAttributes.System).Convert<long>() == 5L );
/*
True
True
True
True
null
----
True
True
True
*/
Guid guid = new Guid("{B625052B-8D72-42CB-B71D-0AA5D116EB37}");
Console.WriteLine( "B625052B-8D72-42CB-B71D-0AA5D116EB37".Convert<Guid>() == guid );
Console.WriteLine( "{B625052B-8D72-42CB-B71D-0AA5D116EB37}".Convert<Guid>() == guid );
Console.WriteLine( guid.Convert<string>() );
/*
True
True
b625052b-8d72-42cb-b71d-0aa5d116eb37
*/
Console.WriteLine( "01:02:03".Convert<TimeSpan>() == new TimeSpan(01, 02, 03) );
Console.WriteLine( "01:02:03.033".Convert<TimeSpan>() == new TimeSpan(00, 01, 02, 03, 33) );
Console.WriteLine( "5.01:02:03.033".Convert<TimeSpan>() == new TimeSpan(05, 01, 02, 03, 33) );
Console.WriteLine( "----" );
Console.WriteLine( new TimeSpan(00, 01, 02, 03, 33).Convert<string>() );
Console.WriteLine( new TimeSpan(00, 01, 02, 03).Convert<string>() );
Console.WriteLine( new TimeSpan(05, 01, 02, 03, 33).Convert<string>() );
Console.WriteLine( "----" );
Console.WriteLine( (1024).Convert<TimeSpan>() == new TimeSpan(1024) );
Console.WriteLine( (10240000L).Convert<TimeSpan>() == new TimeSpan(10240000L) );
Console.WriteLine( new TimeSpan(1024).Convert<int>() == 1024 );
Console.WriteLine( new TimeSpan(10240000L).Convert<long>() == 10240000L );
Console.WriteLine( "----" );
Console.WriteLine( (10240000D).Convert<TimeSpan>() == TimeSpan.FromMilliseconds(10240000D) );
Console.WriteLine( (10240000F).Convert<TimeSpan>() == TimeSpan.FromMilliseconds(10240000F) );
Console.WriteLine( (10240000M).Convert<TimeSpan>() == TimeSpan.FromMilliseconds((double)10240000M) );
Console.WriteLine( TimeSpan.FromMilliseconds(10240000D).Convert<double>() == 10240000D );
Console.WriteLine( TimeSpan.FromMilliseconds(10240000F).Convert<float>() == 10240000F );
Console.WriteLine( TimeSpan.FromMilliseconds((double)10240000M).Convert<decimal>() == 10240000M );
Console.WriteLine( "----" );
Console.WriteLine( "test".Convert<TimeSpan?>() );
Console.WriteLine( "99999.99:99:99.999999999999".Convert<TimeSpan?>() );
Console.WriteLine( "----" );
/*
True
True
True
----
01:02:03.0330000
01:02:03
5.01:02:03.0330000
----
True
True
True
True
----
True
True
True
True
True
True
----
null
null
----
*/
Console.WriteLine( "2007-12-20".Convert<DateTime>() == new DateTime(2007, 12, 20) );
int totalDays = (int)(new DateTime(2017, 02, 03) - DateTime.MinValue).TotalDays;
DateTime date = new DateTime(2017, 02, 03, 12, 30, 55, 99);
TimeSpan time = new TimeSpan(totalDays, 12, 30, 55, 99);
Console.WriteLine( date.Convert<TimeSpan>() == time );
Console.WriteLine( time.Convert<DateTime>() == date );
/*
True
True
True
*/
static void PrintArray<T>(T[] array)
{
for( int i =0; i < array.Length; i++ )
{
Console.WriteLine($"[{i}]{array[i]}");
}
Console.WriteLine("------------");
}
static void Main()
{
string[] array_source = new string[]
{
"1","1.5","3.2","999"
};
PrintArray( array_source.Convert<int?[]>() );
PrintArray( array_source.Convert<decimal[]>() );
PrintArray( new List<string>(array_source).Convert<int?[]>() );
PrintArray( array_source.GetEnumerator().Convert<decimal[]>() );
}
/*
[0]1
[1]null
[2]null
[3]999
------------
[0]1
[1]1.5
[2]3.2
[3]999
------------
[0]1
[1]null
[2]null
[3]999
------------
[0]1
[1]1.5
[2]3.2
[3]999
------------
*/
byte[] bytes = new byte[] { 5, 1, 0, 0 };
int number = 261;
int intValue = bytes.Convert<int>(); //将二进制转换为结构体
Console.WriteLine( number == intValue );
byte[] intToBytes = number.Convert<byte[]>(); //将结构体转换为二进制数组
bool eq = true;
for( int i =0; i < intToBytes.Length; i++ )
{
if ( intToBytes[i] != bytes[i] )
{
eq = false;
break;
}
}
Console.WriteLine( eq );
/*
True
True
*/
class CustomString
{
public string? Value { get; set; }
public CustomString() {
Value = null;
}
public CustomString(string? value)
{
Value = value;
}
public override string? ToString()
{
return Value;
}
//显式转换:需要手动显式的转换,例如 int age = (int)32L; 显式将一个long转换为int。
public static explicit operator string?(CustomString customString)
{
return customString?.Value;
}
//隐式转换:在使用的时候无感,就如同 long value = 32 ;是隐式将int转换为了long
public static implicit operator CustomString(string? value)
{
return new CustomString(value);
}
//重写的目的只是为了方便测试:使两者相等。
public override bool Equals(object? obj)
{
if (obj == null)
return string.IsNullOrEmpty(Value);
if(obj is string)
return string.Equals(Value, (string)obj);
if(obj is CustomString customString)
return customString.Value == Value;
return base.Equals(obj);
}
}
static void Main()
{
CustomString customString = new CustomString("test");
string text = "test";
Console.WriteLine( text.Convert<CustomString>() == customString ); //触发:隐式转换
Console.WriteLine( customString.Convert<string>() == text ); //触发:显式转换
}
/*
True
True
*/