❔ How to remove extra fields during reflection?
I have following method:
the main problem:
I want to process the following class:
and get the result:
items: "parameters"
34 Replies
but got:
well sure
JObject contains the properties
EqualityComparer
and Parent
seems expected to me?yeah, like....
you wrote code to recursively walk properties
but you expect the result to be non-recursive?
what are we missing here?
(also the Contains check can be simplified)
I will explain what do I want to get
I have this class:
ReportOutputParametersDto
is a class with properties so method should be recursive
and this how filled object looks like
all I want is to represent this object as OpenApiSchema
but I can't do anything
my method return this:
reports_generate_arguments:
type: object
properties:
Items:
properties:
Capacity:
type: integer
format: int64
Item:
properties:
TemplateName:
type: string
properties:
Chars: { }
OutputParameters:
properties:
Ttl: { }
IsRequiredHttps:
type: boolean
Format: { }
Parameters:
properties:
Type: { }
looks awfulI find myself even more confused
what is
OpenApiSchema
?
what's wrong with the output you're getting?
do you want it to be recursive or not?I just want to get all properties with JsonProperty attribute
too much properties
I don’t need Chars: {} for example
This was never mentioned until now, and never even attempted in any code sent
So it was kinda impossible to know this
yes, I'm sorry
I can show one thing
But sure, just get the attributes on the property
propertyInfo.GetCustomAttributes<JsonProperty>()
And then check whether the return value is non-null
yes, I can do this:
if (propertyInfo.GetCustomAttribute<JsonPropertyAttribute>() == null)
continue;
but the problem is here:
I don't see any properties
the generated result doesn't look like what I need
and I don't even know why I get this... I just add:
https://gist.github.com/INTERNALINTERFERENCE/06691907f3413b51a2afffff9626a955
full code, you can check it if you want to
I'm too stupid to understand why it doesn't work as I want
Only thing I can think of is that the attributes aren't actually the same, or, of these are in different projects, the versions don't match
hm I checked that, versions are match
I think problem is here.. we just skip class
nono, that's not the
Items
property
that's the []
property
although, yeah, I see the issue
what you need is special logic to identify collections
because your JSON engine has the same special logic
List<T>
doesn't have any properties with [JsonProperty]
on ithm, it's correct
the JSON engine identifes properties to be serialized as those that have
[JsonProperty]
OR those that are (probably) IEnumerable<T>
so, that's what you need to do
I thought that get properties will give me
ReportGenerateDto
which will be combined with EntityItems.HashSet
oh.. sighthat's kinda what you have when you don't filter by
[JsonProperty]
but List<T>
doesn't have that
and you can't add itI'm sorry but I don't understand how to solve my problem..
what should I add to my code?
you added code to skip properties that don't have
[JsonProperty]
you need to adjust that to not skip it if the property is an IEnumerable<T>
ahh okay, thanks! I will try to do this
when it IS an
IEnumerable<T>
you need to extract the T
and then continue walking that type
also emit whatever it is that represents an array in OpenApiSchema
hm I don't think that I can extract the
T
maybe I should do smth like this:
what do you think?yes, that's what I mean
this DOES allow for the possibility of a class that implements multiple
IEnumerable<T>
s
but in practice, there shouldn't be anything like that in your model
except maybe a Dictionary<TKey, TValue>
?
which is another type that is specially-handled by JSON enginesthanks for helping me!
it'll also be true for
string
but it's probably fine because they already check for that separately
i would maybe just pass the type directly? you already have it in your recursive method after allbut.. will it work? now I try this:
and it doesn't work
I don't know what to come up with anymore
@ReactiveVeina did I write a correct version? what I missed?
like they mentioned, you probably want to return all implementations of the interface
a class could for example do
class C : IEnumerable<int>, IEnumerable<long>
probablyso hard
ok I will try this version, thanks
it works perfect now, thanks!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.