为对象增加ExpandProperties方法,该方法扩展对象的属性成为ExpandPropertyInfo枚举记录属性的名称、类型和值。
This commit is contained in:
parent
819a5657e8
commit
766ef83f4b
4
.editorconfig
Normal file
4
.editorconfig
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
[*.cs]
|
||||||
|
|
||||||
|
# CS8618: 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
|
||||||
|
dotnet_diagnostic.CS8618.severity = none
|
|
@ -51,6 +51,52 @@ namespace Falcon.SugarApi.Test
|
||||||
obj=null;
|
obj=null;
|
||||||
Assert.ThrowsException<ArgumentNullException>(() => obj.ThrowNullExceptionWhenNull());
|
Assert.ThrowsException<ArgumentNullException>(() => obj.ThrowNullExceptionWhenNull());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试对象属性扩展
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void ExtendPropertyTest() {
|
||||||
|
var obj = new ExtendPropertyTestClass {
|
||||||
|
Id=1,Name="Falcon",Sex=null,Count="count"
|
||||||
|
};
|
||||||
|
var ep = obj.ExpandProperties();
|
||||||
|
Assert.IsNotNull(ep);
|
||||||
|
Assert.IsTrue(ep.Count()==4);
|
||||||
|
Assert.IsTrue(ep.Select(m => m.Name).Distinct().Count()==4);
|
||||||
|
foreach(var p in ep) {
|
||||||
|
if(p.Name=="Id") {
|
||||||
|
Assert.IsTrue(p.Type==typeof(int));
|
||||||
|
Assert.IsTrue(p.Value.Equals(obj.Id));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(p.Name=="Name") {
|
||||||
|
Assert.IsTrue(p.Type==typeof(string));
|
||||||
|
Assert.IsTrue(p.Value.Equals(obj.Name));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(p.Name=="Sex") {
|
||||||
|
Assert.IsTrue(p.Type==typeof(string));
|
||||||
|
Assert.IsTrue(p.Value==null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(p.Name=="Count") {
|
||||||
|
Assert.IsTrue(p.Type==typeof(string));
|
||||||
|
Assert.IsTrue(p.Value=="count");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 扩展属性测试类
|
||||||
|
/// </summary>
|
||||||
|
public class ExtendPropertyTestClass
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string? Sex { get; set; }
|
||||||
|
public string Count { private get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SourceClass
|
public class SourceClass
|
||||||
|
|
|
@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Falcon.SugarApi.Test", "Fal
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{08995833-E4C9-4222-9084-36BA32B9490E}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{08995833-E4C9-4222-9084-36BA32B9490E}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
|
.editorconfig = .editorconfig
|
||||||
README.md = README.md
|
README.md = README.md
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
|
|
@ -10,6 +10,10 @@
|
||||||
<Version>1.0.0</Version>
|
<Version>1.0.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<!--<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />-->
|
<!--<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />-->
|
||||||
<PackageReference Include="JWT" Version="9.0.3" />
|
<PackageReference Include="JWT" Version="9.0.3" />
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Falcon.SugarApi
|
namespace Falcon.SugarApi
|
||||||
{
|
{
|
||||||
|
@ -23,7 +25,7 @@ namespace Falcon.SugarApi
|
||||||
join t in target.GetType().GetProperties() on s.Name equals t.Name
|
join t in target.GetType().GetProperties() on s.Name equals t.Name
|
||||||
select new { s,t };
|
select new { s,t };
|
||||||
foreach(var item in all) {
|
foreach(var item in all) {
|
||||||
//item.t.SetValue(target, Convert.ChangeType(item.s.GetValue(source), item.t.PropertyType));
|
//item.t.SetValue(target, Convert.ChangeType(item.s.GetValue(source), item.t.Type));
|
||||||
item.t.SetValue(target,item.s.GetValue(source).ChangeType(item.t.PropertyType));
|
item.t.SetValue(target,item.s.GetValue(source).ChangeType(item.t.PropertyType));
|
||||||
}
|
}
|
||||||
return source;
|
return source;
|
||||||
|
@ -73,5 +75,40 @@ namespace Falcon.SugarApi
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 扩展对象,获取属性枚举s
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">要扩展的对象</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IEnumerable<ExpandPropertyInfo> ExpandProperties(this object obj) {
|
||||||
|
foreach(PropertyInfo p in obj.GetType().GetProperties()) {
|
||||||
|
yield return new ExpandPropertyInfo {
|
||||||
|
Name=p.Name,
|
||||||
|
Type=p.PropertyType,
|
||||||
|
Value=p.CanRead ? p.GetValue(obj) : null,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 展开的属性信息
|
||||||
|
/// </summary>
|
||||||
|
public class ExpandPropertyInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 属性名
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 属性类型
|
||||||
|
/// </summary>
|
||||||
|
public Type Type { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 属性值
|
||||||
|
/// </summary>
|
||||||
|
public object? Value { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user