C
C#13mo ago
SWEETPONY

✅ How to get class structure with types?

I have following class:
public class ReportTemplatesListArguments
{
[JsonProperty( PropertyName = "pagination" )]
public Pagination Pagination { get; set; }
}
public class ReportTemplatesListArguments
{
[JsonProperty( PropertyName = "pagination" )]
public Pagination Pagination { get; set; }
}
public class Pagination
{
public Pagination()
{
}

public Pagination( int pageSize )
{
PageSize = pageSize;
}

public Pagination(
int pageSize,
int pageIndex )
{
PageSize = pageSize;
PageIndex = pageIndex;
}

[JsonProperty( PropertyName = "page_index" )]
public int PageIndex { get; set; }

[JsonProperty( PropertyName = "page_size" )]
[JsonRequired]
public int PageSize { get; set; }

[JsonIgnore]
public int Skip =>
PageSize * PageIndex;

[JsonIgnore]
public int Limit =>
PageSize;
}
public class Pagination
{
public Pagination()
{
}

public Pagination( int pageSize )
{
PageSize = pageSize;
}

public Pagination(
int pageSize,
int pageIndex )
{
PageSize = pageSize;
PageIndex = pageIndex;
}

[JsonProperty( PropertyName = "page_index" )]
public int PageIndex { get; set; }

[JsonProperty( PropertyName = "page_size" )]
[JsonRequired]
public int PageSize { get; set; }

[JsonIgnore]
public int Skip =>
PageSize * PageIndex;

[JsonIgnore]
public int Limit =>
PageSize;
}
I'd like to get following object:
Pagination
PageIndex, Type = int
PageSize, Type = int
Pagination
PageIndex, Type = int
PageSize, Type = int
how to do it?
4 Replies
ero
ero13mo ago
what's the question exactly...?
SWEETPONY
SWEETPONY13mo ago
the question is.. how to get all properties of class.. I'm trying to do this:
var properties = type
.GetProperties()
.Select(property => (
property.PropertyName(type),
property.PropertyType))
.ToList();
var properties = type
.GetProperties()
.Select(property => (
property.PropertyName(type),
property.PropertyType))
.ToList();
and all I get is (pagination, Pagination) I need to get nested properties too
phaseshift
phaseshift13mo ago
pagination is the only property on ReportTemplatesListArguments. If you want to dig into Pagination and get all its properties then you'll need to use some recursion or similar
Arch Leaders
Arch Leaders13mo ago
If you want a Dictionary<PropertyInfo, PropertyInfo[]> I'd do something like this:
Dictionary<PropertyInfo, PropertyInfo[]> = typeof(Pagination).GetProperties()
.Select(x => (
parent: x,
children: x.PropertyType.GetProperties())
.ToDictionary(
x => x.parent,
x => x.children);
Dictionary<PropertyInfo, PropertyInfo[]> = typeof(Pagination).GetProperties()
.Select(x => (
parent: x,
children: x.PropertyType.GetProperties())
.ToDictionary(
x => x.parent,
x => x.children);
You could also go one step further to get the names instead of property info, but that's the idea.
Want results from more Discord servers?
Add your server
More Posts