0

I have this attribute

[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = false)]
public class IgnoreAttribute : Attribute
{
}

I want to add this attribute to some classes or properties to replace the value of that property or class with something like null or default() in a middleware. Like this

public class Login
{
    [Ignore]
    public string UserName { get; set; }

    [Ignore]
    public string Password { get; set; }
}

Based on https://stackoverflow.com/a/2282254/9995067 and https://stackoverflow.com/a/44500627/9995067 If I want to check the properties that have that specific attribute I need to know the type of the object and in the middleware I have access to HttpContext request and response body. The Request and Response model can be anything. How can I handle that?

Mohammad Taherian
  • 1,622
  • 1
  • 15
  • 34
  • Have you done any research at all? For example, do you know that if you reflect over a type, get the type's properties, and then use `Attribute.GetCustomAttributes` to find the attributes associated with those properties. By the way, if you are planning to do something like this, you probably want to limit the use of your `Ignore` attribute, changing it's `AttributeTarget` from `All` – Flydog57 Jul 28 '22 at 15:22
  • @Flydog57 Thank you for the comment. this attribute can be used on a class or interface as well. on the other hand, I have a Logging middleware that logs the httpcontext request and response body and that is the place I want to change the values. – Mohammad Taherian Jul 28 '22 at 15:28
  • Then you can OR the values together – Flydog57 Jul 28 '22 at 15:33
  • Re: "The Request and Response model can be anything. How can I handle that?" If you have an object, it has a type. You can get the type by calling `object.GetType()`. Once you have the type, you have the key to reflection – Flydog57 Jul 28 '22 at 15:34
  • [Write custom ASP.NET Core middleware](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-6.0) – Alexander Petrov Jul 28 '22 at 16:30

0 Answers0