C#C
C#3y ago
SWEETPONY

❔ How to remove extra fields during reflection?

I have following method:
private IDictionary<string, OpenApiSchema> GenerateProperties(Type arguments)
{
    var properties = new Dictionary<string, OpenApiSchema>();
    var processedTypes = new HashSet<Type>(); // Maintain a set of processed types
    GeneratePropertiesRecursive(arguments, properties, processedTypes);

    return properties;
}

private void GeneratePropertiesRecursive( Type type, IDictionary<string, OpenApiSchema> properties, HashSet<Type> processedTypes )
{
     var propertyInfos = type.GetProperties();
     foreach ( var propertyInfo in propertyInfos )
     {
        var propertyName = propertyInfo.Name;
        var propertyType = propertyInfo.PropertyType;

        var propertySchema = new OpenApiSchema();

        if ( processedTypes.Contains( propertyType ) )
        {
           // Skip processing if property type has already been processed
           // This prevents infinite recursion in case of circular references
           continue;
        }

        processedTypes.Add( propertyType );

        if ( propertyType.IsClass )
        {
            GeneratePropertiesRecursive( propertyType, propertySchema.Properties, processedTypes );
        }

        if ( propertyType == typeof( int ) )
        {
           propertySchema.Type = "integer";
           propertySchema.Format = "int64";
        }
        else if ( propertyType == typeof( string ) )
        {
           propertySchema.Type = "string";
        }
        else if ( propertyType == typeof( bool ) )
        {
           propertySchema.Type = "boolean";
        }

        properties.Add( propertyName, propertySchema );
      }
    }


the main problem:
I want to process the following class:
public class ReportsGenerateArguments
    : EntityItems<ReportGenerateDto>{}

public class ReportGenerateDto
{
    [JsonProperty( PropertyName = "parameters" )]
    public JObject Parameters { get; set; }}


and get the result:
items: "parameters"
Was this page helpful?