0

This is my Custom attribute

 public class CaptureMetrics : ActionFilterAttribute
 {
    private readonly string _metricType;
    public CaptureMetrics(MetricTypes metricType)
    {
        _metricType = metricType.ToString();
    }
    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        try
        {
            string strJson;
            var reqPayload = context.HttpContext.Request.Body;
            using (var sr = new StreamReader(reqPayload))
            {
                strJson = await sr.ReadToEndAsync();
            }
            //Type t = Type.GetType(_metricType);
            //below I need to typecast the object to the equivalent DTO 
            var obj = JsonConvert.DeserializeObject(strJson); //line #
            // ToDo
        }
        catch (Exception)
        {
            //ToDo -> Log Exception
            throw;
        }
        finally
        {
            await next();
        }
    }
}

public enum MetricTypes
{
    ApiMetric
}

//DTO
public class ApiMetric
{
  //some properties
}

From the controller's action the above attribute is used as below

[CaptureMetrics(MetricTypes.ApiMetric)]
public async Task<IActionResult> Get([FromBody]ApiPayload payload)
{
}

On line #, I need to typecast the request payload to the equivalent DTO class.

For every entry in Enum (MetricType), I have a class with the same name (ex. ApiMetric)

How to typecast the object here?

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Use overload: [DeserializeObject(String, Type)](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_DeserializeObject_2.htm). `JsonConvert.DeserializeObject(strJson, t);` – Alexander Petrov Jan 28 '21 at 12:46
  • Does this answer your question? [Deserialize JSON referencing a type from string name in a type parameter](https://stackoverflow.com/questions/47102107/deserialize-json-referencing-a-type-from-string-name-in-a-type-parameter) – Christoph Lütjen Jan 28 '21 at 12:55
  • BTW: If you need some kind of pipeline but with types (instead of raw requests), MediatR might be an option: https://github.com/jbogard/MediatR – Christoph Lütjen Jan 28 '21 at 12:59
  • @ChristophLütjen, Thanks! but not really – Kgn-web Jan 28 '21 at 13:10
  • Not sure, if I understood the question correctly, let me try to summarize: You now know how to deserialize the object, but that's not known at compile time, so `obj` is of type `object` what means you cannot really work with it. Is this the problem? – Christoph Lütjen Jan 28 '21 at 14:46
  • Do you mean you want to get data with type ApiMetric.I think it cannot be get with string.As `Alexander Petrov` and `Christoph Lütjen` said,you can only use JsonConvert.DeserializeObject(strJson, t);and get data from the object. – Yiyi You Jan 29 '21 at 07:51
  • @ChristophLütjen Thanks! Yes you got it right – Kgn-web Jan 29 '21 at 11:05
  • If you want to go with the dictionary apporach, you'll need reflection afaik. Other options might be...: a) there might be a common interface you could use? b) you could switch on `obj` https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression – Christoph Lütjen Jan 29 '21 at 11:36

0 Answers0