为对象增加ExpandProperties方法,该方法扩展对象的属性成为ExpandPropertyInfo枚举记录属性的名称、类型和值。

This commit is contained in:
falcon 2023-01-05 10:02:26 +08:00
parent 819a5657e8
commit 766ef83f4b
5 changed files with 117 additions and 25 deletions

4
.editorconfig Normal file
View File

@ -0,0 +1,4 @@
[*.cs]
# CS8618: 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
dotnet_diagnostic.CS8618.severity = none

View File

@ -15,22 +15,22 @@ namespace Falcon.SugarApi.Test
public void CloneToTest() { public void CloneToTest() {
var s = new SourceClass { }; var s = new SourceClass { };
var t = new TargetClass { }; var t = new TargetClass { };
t.ica.ItemA = "itemb"; t.ica.ItemA="itemb";
Assert.IsTrue(t.ica.ItemA == "itemb"); Assert.IsTrue(t.ica.ItemA=="itemb");
var r = s.CloneTo(t); var r = s.CloneTo(t);
Assert.IsNotNull(s); Assert.IsNotNull(s);
Assert.IsNotNull(r); Assert.IsNotNull(r);
Assert.IsNotNull(t); Assert.IsNotNull(t);
Assert.IsTrue(s.ia == t.ia, $"a.id:{s.ia},t.ia:{t.ia}"); Assert.IsTrue(s.ia==t.ia,$"a.id:{s.ia},t.ia:{t.ia}");
Assert.IsTrue(t.ia == 1); Assert.IsTrue(t.ia==1);
Assert.IsTrue(s.sa == t.sa); Assert.IsTrue(s.sa==t.sa);
Assert.IsTrue(s.sc == "sc"); Assert.IsTrue(s.sc=="sc");
Assert.IsTrue(t.sd == "sd"); Assert.IsTrue(t.sd=="sd");
Assert.IsTrue(s.ica.ItemA == "itema"); Assert.IsTrue(s.ica.ItemA=="itema");
Assert.IsTrue(t.ica.ItemA == "itema"); Assert.IsTrue(t.ica.ItemA=="itema");
Assert.IsTrue(t.ica.Equals(s.ica)); Assert.IsTrue(t.ica.Equals(s.ica));
} }
@ -48,9 +48,55 @@ namespace Falcon.SugarApi.Test
public void ThrowNullExceptionWhenNullTest() { public void ThrowNullExceptionWhenNullTest() {
var obj = new object(); var obj = new object();
obj.ThrowNullExceptionWhenNull(); obj.ThrowNullExceptionWhenNull();
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

View File

@ -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

View File

@ -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" />

View File

@ -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
{ {
@ -16,15 +18,15 @@ namespace Falcon.SugarApi
/// <typeparam name="TSource">原对象类型</typeparam> /// <typeparam name="TSource">原对象类型</typeparam>
/// <param name="source">原对象</param> /// <param name="source">原对象</param>
/// <param name="target">目标对象</param> /// <param name="target">目标对象</param>
public static TSource CloneTo<TSource>(this TSource source, [NotNull] object target) where TSource : class { public static TSource CloneTo<TSource>(this TSource source,[NotNull] object target) where TSource : class {
_ = source ?? throw new ArgumentNullException(nameof(source)); _=source??throw new ArgumentNullException(nameof(source));
_ = target ?? throw new ArgumentNullException(nameof(target)); _=target??throw new ArgumentNullException(nameof(target));
var all = from s in source.GetType().GetProperties() var all = from s in source.GetType().GetProperties()
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;
} }
@ -36,7 +38,7 @@ namespace Falcon.SugarApi
/// <param name="target">目标对象</param> /// <param name="target">目标对象</param>
/// <param name="source">原对象</param> /// <param name="source">原对象</param>
/// <returns>目标对象</returns> /// <returns>目标对象</returns>
public static Ttarget CloneFrom<Ttarget>(this Ttarget target, object source) where Ttarget : class { public static Ttarget CloneFrom<Ttarget>(this Ttarget target,object source) where Ttarget : class {
source.CloneTo(target); source.CloneTo(target);
return target; return target;
} }
@ -47,18 +49,18 @@ namespace Falcon.SugarApi
/// <param name="source">原对象</param> /// <param name="source">原对象</param>
/// <param name="targetType">目标类型</param> /// <param name="targetType">目标类型</param>
/// <returns>转换后的类型</returns> /// <returns>转换后的类型</returns>
public static object? ChangeType(this object? source, Type targetType) { public static object? ChangeType(this object? source,Type targetType) {
if (targetType == null) { if(targetType==null) {
throw new ArgumentNullException("targetType"); throw new ArgumentNullException("targetType");
} }
if (source == null) { if(source==null) {
return null; return null;
} }
if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { if(targetType.IsGenericType&&targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
NullableConverter nullableConverter = new NullableConverter(targetType); NullableConverter nullableConverter = new NullableConverter(targetType);
targetType = nullableConverter.UnderlyingType; targetType=nullableConverter.UnderlyingType;
} }
return Convert.ChangeType(source, targetType); return Convert.ChangeType(source,targetType);
} }
/// <summary> /// <summary>
@ -68,10 +70,45 @@ namespace Falcon.SugarApi
/// <returns>对象本身</returns> /// <returns>对象本身</returns>
/// <exception cref="ArgumentNullException">对象为null</exception> /// <exception cref="ArgumentNullException">对象为null</exception>
public static object? ThrowNullExceptionWhenNull(this object? obj) { public static object? ThrowNullExceptionWhenNull(this object? obj) {
if (obj == null) { if(obj==null) {
throw new ArgumentNullException(); throw new ArgumentNullException();
} }
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; }
}
} }