我正在解决以下问题:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>JWTAuthenticationFilter</filter-name>
<filter-class>com.yourProject.JWTAuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>JWTAuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
想象一下,我有这个域名,我想复制 public class Unity
{
public Article Article {get;set;}
public string Text{get;set;}
public string Description{get;set;}
public int NumberOfPages{get;set;}
}
enter code here
public class Article
{
public int Id{get;set;}
public string Name{get;set;}
public Category SubCategory {get;set;}
public Model Model {get;set}
public string override ToString(){
return Name;
}
}
public class Category
{
public int Id{get;set;}
public string Name{get;set;}
public Category CategoryFather { get;set;}
}
public class Model{
public int Id {get;set;}
public string Name{get;set;}
public Model ModelFather{get;set;}
}
我所做的Name
,但我需要Article
Name
Model
}和Name
的{{1}}。
我有下一个要复制的代码:
Category
答案 0 :(得分:2)
正如vendettamit所提到的,你可以实现使用递归。以下代码实现了基本实现数据类型和类的基本实现。您必须根据对象结构处理各种代码流。
public static List<DataPropertyReport> GetPrimitiveProperties<T>(T entity, string heirarchyName = null)
{
List<DataPropertyReport> info = new List<DataPropertyReport>();
if (entity != null)
{
var properties = entity.GetType().GetProperties();
foreach (var property in properties)
{
Object value = property.GetValue(entity, null);
var name = property.Name;
var relatedHeirarchyName = string.IsNullOrEmpty(heirarchyName) ? name : string.Concat(heirarchyName, ".", name);
if (property.PropertyType != typeof(string) && property.PropertyType.IsClass)
{
var reports = GetPrimitiveProperties(value, relatedHeirarchyName);
info.AddRange(reports);
}
else
{
info.Add(new DataPropertyReport(relatedHeirarchyName, value != null ? value.ToString() : "", 1));
}
}
}
return info;
}
对于下方对象
Unity unity = new Unity()
{
NumberOfPages = 1,
Description = "Test Des",
Text = "Test Text",
Article = new Article()
{
Id = 1,
Name = "test Article",
Model = new Model()
{
Name = "Test Model",
Id = 2,
ModelFather = new Model()
{
Id = 3,
Name = "Test Father Model"
}
},
SubCategory = new Category()
{
Name = "Test Category",
Id = 4,
CategoryFather = new Category()
{
Id = 5,
Name = "Test Category Fathere"
}
}
}
};
输出
姓名:Article.Id价值:1
名称:Article.Name值:测试文章
名称:Article.SubCategory.Id值:4
名称:Article.SubCategory.Name值:测试类别
名称:Article.SubCategory.CategoryFather.Id值:5
名称:Article.SubCategory.CategoryFather.Name值:测试类别Fathere
名称:Article.Model.Id值:2
名称:Article.Model.Name值:测试模型
名称:Article.Model.ModelFather.Id值:3
名称:Article.Model.ModelFather.Name值:测试父模型
名称:文本值:测试文本
名称:描述值:测试Des
名称:NumberOfPages值:1
基于以下ToString实现DataPropertyReport
public override string ToString()
{
return $"Name : {this.Name} Value : {this.Value}";
}