Repository: microsoft/onefuzz
File: src/ApiService/ApiService/onefuzzlib/orm/EntityConverter.cs (C#)

Code before:

    public static JsonSerializerOptions GetJsonSerializerOptions() {
        return _options;
    }

    internal static Func<object?[], object> BuildConstructerFrom(ConstructorInfo constructorInfo) {
        var constructorParameters = Expression.Parameter(typeof(object?[]));

        var parameterExpressions =
            constructorInfo.GetParameters().Select((parameterInfo, i) => {
                var ithIndex = Expression.Constant(i);
                var ithParameter = Expression.ArrayIndex(constructorParameters, ithIndex);
                var unboxedIthParameter = Expression.Convert(ithParameter, parameterInfo.ParameterType);
                return unboxedIthParameter;

            }).ToArray();

        NewExpression constructorCall = Expression.New(constructorInfo, parameterExpressions);

        Func<object?[], object> ctor = Expression.Lambda<Func<object?[], object>>(constructorCall, constructorParameters).Compile();
        return ctor;
    }

    private static IEnumerable<EntityProperty> GetEntityProperties<T>(ParameterInfo parameterInfo) {
        var name = parameterInfo.Name.EnsureNotNull($"Invalid paramater {parameterInfo}");
        var parameterType = parameterInfo.ParameterType.EnsureNotNull($"Invalid paramater {parameterInfo}");
        var isRowkey = parameterInfo.GetCustomAttribute(typeof(RowKeyAttribute)) != null;
        var isPartitionkey = parameterInfo.GetCustomAttribute(typeof(PartitionKeyAttribute)) != null;

        var discriminatorAttribute = typeof(T).GetProperty(name)?.GetCustomAttribute<TypeDiscrimnatorAttribute>();
        var defaultValueAttribute = parameterInfo.GetCustomAttribute<DefaultValueAttribute>();


        (TypeDiscrimnatorAttribute, ITypeProvider)? discriminator = null;
        if (discriminatorAttribute != null) {
            var t = (ITypeProvider)(Activator.CreateInstance(discriminatorAttribute.ConverterType) ?? throw new Exception("unable to retrieve the type provider"));
            discriminator = (discriminatorAttribute, t);
        }


        if (isPartitionkey) {
            yield return new EntityProperty(name, "PartitionKey", parameterType, EntityPropertyKind.PartitionKey, discriminator, defaultValueAttribute, parameterInfo);
        }

        if (isRowkey) {
            yield return new EntityProperty(name, "RowKey", parameterType, EntityPropertyKind.RowKey, discriminator, defaultValueAttribute, parameterInfo);
        }

        if (!isPartitionkey && !isRowkey) {
            var columnName = typeof(T).GetProperty(name)?.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name ?? CaseConverter.PascalToSnake(name);
            yield return new EntityProperty(name, columnName, parameterType, EntityPropertyKind.Column, discriminator, defaultValueAttribute, parameterInfo);

Review comment (suggestion):
```suggestion
        var parameterType = parameterInfo.ParameterType.EnsureNotNull($"Invalid parameter {parameterInfo}");
```

Code after:

    public static JsonSerializerOptions GetJsonSerializerOptions() {
        return _options;
    }

    internal static Func<object?[], object> BuildConstructerFrom(ConstructorInfo constructorInfo) {
        var constructorParameters = Expression.Parameter(typeof(object?[]));

        var parameterExpressions =
            constructorInfo.GetParameters().Select((parameterInfo, i) => {
                var ithIndex = Expression.Constant(i);
                var ithParameter = Expression.ArrayIndex(constructorParameters, ithIndex);
                var unboxedIthParameter = Expression.Convert(ithParameter, parameterInfo.ParameterType);
                return unboxedIthParameter;

            }).ToArray();

        NewExpression constructorCall = Expression.New(constructorInfo, parameterExpressions);

        Func<object?[], object> ctor = Expression.Lambda<Func<object?[], object>>(constructorCall, constructorParameters).Compile();
        return ctor;
    }

    private static IEnumerable<EntityProperty> GetEntityProperties<T>(ParameterInfo parameterInfo) {
        var name = parameterInfo.Name.EnsureNotNull($"Invalid parameter {parameterInfo}");
        var parameterType = parameterInfo.ParameterType.EnsureNotNull($"Invalid parameter {parameterInfo}");
        var isRowkey = parameterInfo.GetCustomAttribute(typeof(RowKeyAttribute)) != null;
        var isPartitionkey = parameterInfo.GetCustomAttribute(typeof(PartitionKeyAttribute)) != null;

        var discriminatorAttribute = typeof(T).GetProperty(name)?.GetCustomAttribute<TypeDiscrimnatorAttribute>();
        var defaultValueAttribute = parameterInfo.GetCustomAttribute<DefaultValueAttribute>();


        (TypeDiscrimnatorAttribute, ITypeProvider)? discriminator = null;
        if (discriminatorAttribute != null) {
            var t = (ITypeProvider)(Activator.CreateInstance(discriminatorAttribute.ConverterType) ?? throw new Exception("unable to retrieve the type provider"));
            discriminator = (discriminatorAttribute, t);
        }


        if (isPartitionkey) {
            yield return new EntityProperty(name, "PartitionKey", parameterType, EntityPropertyKind.PartitionKey, discriminator, defaultValueAttribute, parameterInfo);
        }

        if (isRowkey) {
            yield return new EntityProperty(name, "RowKey", parameterType, EntityPropertyKind.RowKey, discriminator, defaultValueAttribute, parameterInfo);
        }

        if (!isPartitionkey && !isRowkey) {
            var columnName = typeof(T).GetProperty(name)?.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name ?? CaseConverter.PascalToSnake(name);
            yield return new EntityProperty(name, columnName, parameterType, EntityPropertyKind.Column, discriminator, defaultValueAttribute, parameterInfo);