I have a generic method, I give it any object of type T and a list of properties and it will return the object with the properties defined by the list set to null
Here's my code
class Program
{
static void Main(string[] args)
{
var orderDto = new OrderDto();
orderDto.Nominal = "1";
orderDto.OrderId = "2";
orderDto.Type = "3";
var clean = FieldCleaner.Clean(orderDto, "OrderId");
}
}
public class FieldCleaner
{
public static T Clean<T>(T dto, params string[] properties) // I want in compilation time, have autocompletion that tell user the value of properties can only be a property name of the type T
{
var propertyInfos = dto.GetType().GetProperties();
foreach (var propertyInfo in propertyInfos)
{
foreach (var property in properties)
{
if (propertyInfo.Name == property)
{
propertyInfo.SetValue(dto, null);
}
}
}
return dto;
}
}
public class OrderDto
{
public string OrderId { get; set; }
public string Nominal { get; set; }
public string Type { get; set; }
}
My question is in the comment above in the code. I don't like the type string[], I want something like a keyof T in typescript
Im using last C# version with last .NET core