Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
8547807b
IoTSharp
项目概览
IoTSharp
/
IoTSharp
9 个月 前同步成功
通知
15
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
IoTSharp
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8547807b
编写于
11月 03, 2019
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"D:\GitHub\IoTSharp\IoTSharp\GitVersion.yml"
上级
0ba378b4
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
332 addition
and
193 deletion
+332
-193
IoTSharp.Extensions/DataExtension.cs
IoTSharp.Extensions/DataExtension.cs
+196
-0
IoTSharp.Extensions/FileExtension.cs
IoTSharp.Extensions/FileExtension.cs
+48
-0
IoTSharp.Extensions/IoTSharp.Extensions.csproj
IoTSharp.Extensions/IoTSharp.Extensions.csproj
+6
-9
IoTSharp.Extensions/JsonExtensions.cs
IoTSharp.Extensions/JsonExtensions.cs
+0
-55
IoTSharp.Extensions/MiscExtensions.cs
IoTSharp.Extensions/MiscExtensions.cs
+0
-129
IoTSharp.Extensions/StringExtension.cs
IoTSharp.Extensions/StringExtension.cs
+49
-0
IoTSharp.Extensions/TaskExtension.cs
IoTSharp.Extensions/TaskExtension.cs
+33
-0
未找到文件。
IoTSharp.Extensions/DataExtension.cs
0 → 100644
浏览文件 @
8547807b
using
Newtonsoft.Json.Linq
;
using
System
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Diagnostics
;
using
System.IO
;
using
System.Linq
;
using
System.Security.Cryptography
;
using
System.Text
;
namespace
Uixe.Extensions
{
public
static
class
DataExtension
{
public
static
string
ToISO8601
(
this
System
.
DateTime
time
)
=>
time
.
ToUniversalTime
().
ToString
(
"yyyy-MM-ddTHH:mm:sssZ"
);
public
static
T
ToJson
<
T
>(
this
IDataReader
dataReader
)
where
T
:
class
{
return
dataReader
.
ToJson
().
ToObject
<
T
>();
}
public
static
JArray
ToJson
(
this
IDataReader
dataReader
)
{
JArray
jArray
=
new
JArray
();
try
{
while
(
dataReader
.
Read
())
{
JObject
jObject
=
new
JObject
();
for
(
int
i
=
0
;
i
<
dataReader
.
FieldCount
;
i
++)
{
try
{
string
strKey
=
dataReader
.
GetName
(
i
);
if
(
dataReader
[
i
]
!=
DBNull
.
Value
)
{
object
obj
=
Convert
.
ChangeType
(
dataReader
[
i
],
dataReader
.
GetFieldType
(
i
));
jObject
.
Add
(
strKey
,
JToken
.
FromObject
(
obj
));
}
}
catch
(
Exception
)
{
}
}
jArray
.
Add
(
jObject
);
}
}
catch
(
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
return
jArray
;
}
public
static
JArray
ToJson
(
this
DataTable
dt
)
{
JArray
jArray
=
new
JArray
();
try
{
for
(
int
il
=
0
;
il
<
dt
.
Rows
.
Count
;
il
++)
{
JObject
jObject
=
new
JObject
();
for
(
int
i
=
0
;
i
<
dt
.
Columns
.
Count
;
i
++)
{
try
{
string
strKey
=
dt
.
Columns
[
i
].
ColumnName
;
if
(
dt
.
Rows
[
il
].
ItemArray
[
i
]
!=
DBNull
.
Value
)
{
object
obj
=
Convert
.
ChangeType
(
dt
.
Rows
[
il
].
ItemArray
[
i
],
dt
.
Columns
[
i
].
DataType
);
jObject
.
Add
(
strKey
,
JToken
.
FromObject
(
obj
));
}
}
catch
(
Exception
)
{
}
}
jArray
.
Add
(
jObject
);
}
}
catch
(
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
return
jArray
;
}
public
static
List
<
T
>
ToList
<
T
>(
this
DataTable
dt
)
where
T
:
class
{
List
<
T
>
jArray
=
new
List
<
T
>();
var
prs
=
typeof
(
T
).
GetProperties
();
try
{
for
(
int
il
=
0
;
il
<
dt
.
Rows
.
Count
;
il
++)
{
T
jObject
=
Activator
.
CreateInstance
<
T
>();
for
(
int
i
=
0
;
i
<
dt
.
Columns
.
Count
;
i
++)
{
try
{
string
strKey
=
dt
.
Columns
[
i
].
ColumnName
;
if
(
dt
.
Rows
[
il
].
ItemArray
[
i
]
!=
DBNull
.
Value
)
{
object
obj
=
Convert
.
ChangeType
(
dt
.
Rows
[
il
].
ItemArray
[
i
],
dt
.
Columns
[
i
].
DataType
);
var
p
=
prs
.
FirstOrDefault
(
px
=>
px
.
Name
.
ToLower
()
==
strKey
.
ToLower
());
if
(
p
!=
null
)
{
SetValue
(
jObject
,
dt
.
Columns
[
i
].
DataType
,
obj
,
p
);
}
}
}
catch
(
Exception
ex
)
{
Debug
.
WriteLine
(
ex
.
Message
);
}
}
jArray
.
Add
(
jObject
);
}
}
catch
(
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
return
jArray
;
}
public
static
List
<
T
>
ToList
<
T
>(
this
IDataReader
dataReader
)
where
T
:
class
{
List
<
T
>
jArray
=
new
List
<
T
>();
var
prs
=
typeof
(
T
).
GetProperties
();
try
{
while
(
dataReader
.
Read
())
{
T
jObject
=
Activator
.
CreateInstance
<
T
>();
for
(
int
i
=
0
;
i
<
dataReader
.
FieldCount
;
i
++)
{
try
{
string
strKey
=
dataReader
.
GetName
(
i
);
if
(
dataReader
[
i
]
!=
DBNull
.
Value
)
{
var
ft
=
dataReader
.
GetFieldType
(
i
);
var
_v
=
dataReader
[
i
];
object
obj
=
Convert
.
ChangeType
(
_v
,
ft
);
var
p
=
prs
.
FirstOrDefault
(
px
=>
px
.
Name
.
ToLower
()
==
strKey
.
ToLower
());
if
(
p
!=
null
)
{
SetValue
(
jObject
,
ft
,
obj
,
p
);
}
}
}
catch
(
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
}
jArray
.
Add
(
jObject
);
}
}
catch
(
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
return
jArray
;
}
private
static
void
SetValue
<
T
>(
T
jObject
,
Type
ft
,
object
obj
,
System
.
Reflection
.
PropertyInfo
p
)
where
T
:
class
{
if
(
p
.
PropertyType
==
ft
)
{
p
.
SetValue
(
jObject
,
obj
);
}
else
if
(
p
.
PropertyType
==
typeof
(
DateTime
)
&&
ft
==
typeof
(
string
))
{
if
(
DateTime
.
TryParse
((
string
)
obj
,
out
DateTime
dt
))
{
p
.
SetValue
(
jObject
,
dt
);
}
}
else
{
p
.
SetValue
(
jObject
,
Convert
.
ChangeType
(
obj
,
p
.
PropertyType
));
}
}
}
}
\ No newline at end of file
IoTSharp.Extensions/FileExtension.cs
0 → 100644
浏览文件 @
8547807b
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Security.Cryptography
;
using
System.Text
;
namespace
IoTSharp.Extensions
{
public
static
class
FileExtension
{
public
static
string
GetSHA1
(
this
FileInfo
s
)
{
string
result
=
string
.
Empty
;
SHA1
sha1
=
new
SHA1CryptoServiceProvider
();
byte
[]
retval
=
sha1
.
ComputeHash
(
s
.
ReadAllBytes
());
result
=
BitConverter
.
ToString
(
retval
).
Replace
(
"-"
,
""
);
return
result
;
}
public
static
string
GetMd5Sum
(
this
FileInfo
s
)
{
MD5CryptoServiceProvider
md5
=
new
MD5CryptoServiceProvider
();
string
t2
=
BitConverter
.
ToString
(
md5
.
ComputeHash
(
s
.
ReadAllBytes
()));
t2
=
t2
.
Replace
(
"-"
,
""
);
return
t2
;
}
public
static
byte
[]
ReadAllBytes
(
this
FileInfo
fi
)
=>
File
.
ReadAllBytes
(
fi
.
FullName
);
public
static
byte
[]
ReadBytes
(
this
FileInfo
fi
,
int
count
)
=>
ReadBytes
(
fi
,
0
,
count
);
public
static
byte
[]
ReadBytes
(
this
FileInfo
fi
,
int
offset
,
int
count
)
{
byte
[]
buffer
=
new
byte
[
count
];
using
(
var
fs
=
fi
.
OpenRead
())
{
fs
.
Read
(
buffer
,
offset
,
count
);
}
return
buffer
;
}
public
static
void
WriteAllText
(
this
FileInfo
fi
,
string
contents
)
=>
File
.
WriteAllText
(
fi
.
FullName
,
contents
);
public
static
bool
Exists
(
this
FileInfo
fi
)
=>
File
.
Exists
(
fi
.
FullName
);
public
static
void
Delete
(
this
FileInfo
fi
)
=>
File
.
Delete
(
fi
.
FullName
);
public
static
string
ReadAllText
(
this
FileInfo
fi
)
=>
File
.
ReadAllText
(
fi
.
FullName
);
public
static
void
WriteAllBytes
(
this
FileInfo
fi
,
byte
[]
bytes
)
=>
File
.
WriteAllBytes
(
fi
.
FullName
,
bytes
);
}
}
IoTSharp.Extensions/IoTSharp.Extensions.csproj
浏览文件 @
8547807b
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net
coreapp3.0
</TargetFramework>
<TargetFramework>net
standard2.1
</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/IoTSharp/IoTSharp/tree/master/IoTSharp.Extensions</PackageProjectUrl>
<RepositoryUrl>https://github.com/IoTSharp/IoTSharp</RepositoryUrl>
...
...
@@ -10,15 +10,12 @@
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.0.0" />
<PackageReference Include="GitVersionTask" Version="5.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.0.0" />
</ItemGroup>
</Project>
IoTSharp.Extensions/JsonExtensions.cs
已删除
100644 → 0
浏览文件 @
0ba378b4
using
Newtonsoft.Json.Linq
;
using
System
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Text
;
namespace
IoTSharp.Extensions
{
public
static
class
JsonExtensions
{
public
static
T
ToJson
<
T
>(
this
IDataReader
dataReader
)
where
T
:
class
{
return
dataReader
.
ToJson
().
ToObject
<
T
>();
}
public
static
List
<
T
>
ToList
<
T
>(
this
IDataReader
dataReader
)
where
T
:
class
{
return
dataReader
.
ToJson
().
ToObject
<
List
<
T
>>();
}
public
static
JArray
ToJson
(
this
IDataReader
dataReader
)
{
JArray
jArray
=
new
JArray
();
try
{
while
(
dataReader
.
Read
())
{
JObject
jObject
=
new
JObject
();
for
(
int
i
=
0
;
i
<
dataReader
.
FieldCount
;
i
++)
{
try
{
string
strKey
=
dataReader
.
GetName
(
i
);
if
(
dataReader
[
i
]
!=
DBNull
.
Value
)
{
object
obj
=
Convert
.
ChangeType
(
dataReader
[
i
],
dataReader
.
GetFieldType
(
i
));
jObject
.
Add
(
strKey
,
JToken
.
FromObject
(
obj
));
}
}
catch
(
Exception
)
{
}
}
jArray
.
Add
(
jObject
);
}
}
catch
(
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
return
jArray
;
}
}
}
IoTSharp.Extensions/MiscExtensions.cs
浏览文件 @
8547807b
...
...
@@ -2,150 +2,21 @@
using
Newtonsoft.Json.Linq
;
using
System
;
using
System.Collections.Generic
;
using
System.ComponentModel.DataAnnotations
;
using
System.Data
;
using
System.Diagnostics
;
using
System.Linq
;
using
System.Runtime.InteropServices
;
using
System.Text
;
using
System.Security.Cryptography
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.Hosting.WindowsServices
;
using
Microsoft.Extensions.Configuration
;
using
System.Threading.Tasks
;
using
System.Threading
;
using
Microsoft.Extensions.Hosting
;
using
Microsoft.Extensions.DependencyInjection
;
namespace
IoTSharp.Extensions
{
public
static
class
MiscExtensions
{
public
static
IWebHostBuilder
UseJsonToSettings
(
this
IWebHostBuilder
hostBuilder
,
string
filename
)
{
return
hostBuilder
.
ConfigureAppConfiguration
(
builder
=>
{
try
{
if
(
System
.
IO
.
File
.
Exists
(
filename
))
{
builder
.
AddJsonFile
(
filename
,
true
);
}
}
catch
(
System
.
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
});
}
public
static
IHostBuilder
ConfigureWindowsServices
(
this
IHostBuilder
hostBuilder
)
{
bool
IsWindowsService
=
false
;
Encoding
.
RegisterProvider
(
CodePagesEncodingProvider
.
Instance
);
if
(
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
Windows
))
{
using
(
var
process
=
GetParent
(
Process
.
GetCurrentProcess
()))
{
IsWindowsService
=
process
!=
null
&&
process
.
ProcessName
==
"services"
;
}
}
if
(
Environment
.
CommandLine
.
Contains
(
"--usebasedirectory"
)
||
(
RuntimeInformation
.
IsOSPlatform
(
OSPlatform
.
Windows
)
&&
IsWindowsService
))
{
hostBuilder
.
UseContentRoot
(
AppContext
.
BaseDirectory
);
System
.
IO
.
Directory
.
SetCurrentDirectory
(
AppContext
.
BaseDirectory
);
hostBuilder
.
UseWindowsService
();
}
return
hostBuilder
;
}
private
static
Process
GetParent
(
Process
child
)
{
var
parentId
=
0
;
var
handle
=
CreateToolhelp32Snapshot
(
TH32CS_SNAPPROCESS
,
0
);
if
(
handle
==
IntPtr
.
Zero
)
{
return
null
;
}
var
processInfo
=
new
PROCESSENTRY32
{
dwSize
=
(
uint
)
Marshal
.
SizeOf
(
typeof
(
PROCESSENTRY32
))
};
if
(!
Process32First
(
handle
,
ref
processInfo
))
{
return
null
;
}
do
{
if
(
child
.
Id
==
processInfo
.
th32ProcessID
)
{
parentId
=
(
int
)
processInfo
.
th32ParentProcessID
;
}
}
while
(
parentId
==
0
&&
Process32Next
(
handle
,
ref
processInfo
));
if
(
parentId
>
0
)
{
return
Process
.
GetProcessById
(
parentId
);
}
return
null
;
}
private
static
uint
TH32CS_SNAPPROCESS
=
2
;
[
DllImport
(
"kernel32.dll"
)]
public
static
extern
bool
Process32Next
(
IntPtr
hSnapshot
,
ref
PROCESSENTRY32
lppe
);
[
DllImport
(
"kernel32.dll"
,
SetLastError
=
true
)]
public
static
extern
IntPtr
CreateToolhelp32Snapshot
(
uint
dwFlags
,
uint
th32ProcessID
);
[
DllImport
(
"kernel32.dll"
)]
public
static
extern
bool
Process32First
(
IntPtr
hSnapshot
,
ref
PROCESSENTRY32
lppe
);
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
PROCESSENTRY32
{
public
uint
dwSize
;
public
uint
cntUsage
;
public
uint
th32ProcessID
;
public
IntPtr
th32DefaultHeapID
;
public
uint
th32ModuleID
;
public
uint
cntThreads
;
public
uint
th32ParentProcessID
;
public
int
pcPriClassBase
;
public
uint
dwFlags
;
[
MarshalAs
(
UnmanagedType
.
ByValTStr
,
SizeConst
=
260
)]
public
string
szExeFile
;
}
public
static
string
MD5Sum
(
this
string
text
)
=>
BitConverter
.
ToString
(
MD5
.
Create
().
ComputeHash
(
Encoding
.
UTF8
.
GetBytes
(
text
))).
Replace
(
"-"
,
""
);
public
static
Task
Forget
(
this
Task
task
)
{
return
Task
.
CompletedTask
;
}
public
static
Task
StartSTATask
(
this
TaskFactory
task
,
Action
action
)
{
var
tcs
=
new
TaskCompletionSource
<
object
>();
var
thread
=
new
Thread
(()
=>
{
try
{
action
();
tcs
.
SetResult
(
new
object
());
}
catch
(
Exception
e
)
{
tcs
.
SetException
(
e
);
}
});
thread
.
SetApartmentState
(
ApartmentState
.
STA
);
thread
.
Start
();
return
tcs
.
Task
;
}
public
static
T
GetRequiredService
<
T
>(
this
IServiceScopeFactory
scopeFactor
)
=>
scopeFactor
.
CreateScope
().
ServiceProvider
.
GetRequiredService
<
T
>();
...
...
IoTSharp.Extensions/StringExtension.cs
0 → 100644
浏览文件 @
8547807b
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
namespace
IoTSharp.Extensions
{
public
static
class
StringExtension
{
public
static
string
ToTitleCase
(
this
string
str
)
{
return
System
.
Threading
.
Thread
.
CurrentThread
.
CurrentCulture
.
TextInfo
.
ToTitleCase
(
str
.
ToLower
());
}
public
static
string
Left
(
this
string
str
,
int
length
)
{
str
=
(
str
??
string
.
Empty
);
return
str
.
Substring
(
0
,
Math
.
Min
(
length
,
str
.
Length
));
}
public
static
string
Right
(
this
string
str
,
int
length
)
{
str
=
(
str
??
string
.
Empty
);
return
(
str
.
Length
>=
length
)
?
str
.
Substring
(
str
.
Length
-
length
,
length
)
:
str
;
}
public
static
char
[]
Right
(
this
char
[]
str
,
int
length
)
{
return
str
.
Skip
(
str
.
Length
-
length
).
Take
(
length
).
ToArray
();
}
public
static
char
[]
Left
(
this
char
[]
str
,
int
length
)
{
return
str
.
Take
(
length
).
ToArray
();
}
public
static
byte
[]
Right
(
this
byte
[]
str
,
int
length
)
{
return
str
.
Skip
(
str
.
Length
-
length
).
Take
(
length
).
ToArray
();
}
public
static
byte
[]
Left
(
this
byte
[]
str
,
int
length
)
{
return
str
.
Take
(
length
).
ToArray
();
}
}
}
IoTSharp.Extensions/TaskExtension.cs
0 → 100644
浏览文件 @
8547807b
using
System
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
IoTSharp.Extensions
{
public
static
class
TaskExtension
{
public
static
Task
StartSTATask
(
this
TaskFactory
task
,
Action
action
)
{
var
tcs
=
new
TaskCompletionSource
<
object
>();
var
thread
=
new
Thread
(()
=>
{
try
{
action
();
tcs
.
SetResult
(
new
object
());
}
catch
(
Exception
e
)
{
tcs
.
SetException
(
e
);
}
});
thread
.
SetApartmentState
(
ApartmentState
.
STA
);
thread
.
Start
();
return
tcs
.
Task
;
}
public
static
Task
Forget
(
this
Task
task
)
{
return
Task
.
CompletedTask
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录