Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将动态对象从请求反序列化为强类型_C#_Asp.net_Asp.net Core - Fatal编程技术网

C# 将动态对象从请求反序列化为强类型

C# 将动态对象从请求反序列化为强类型,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,我正在尝试实现API端点,该端点应该允许在请求体中使用动态对象 主体1的示例: { "type": "CallStart", "attributes": { "Name": "Test", "Time": "09:15" } } 主体2的示例: { "type": "CallEnd&qu

我正在尝试实现API端点,该端点应该允许在请求体中使用动态对象

主体1的示例:

{
  "type": "CallStart",
  "attributes": {
      "Name": "Test",
      "Time": "09:15"
  }
}
主体2的示例:

{
  "type": "CallEnd",
  "attributes": {
      "Name": "Test",
      "SendFeedback": true
  }
}
所以,我创建了这个模型:

public class CommandRequest
{
    public CommandType Type { get; set; }
    public dynamic Attributes { get; set; }
}
然后我试着写下这样的东西

[HttpPost("id")]
public async Task<IActionResult> Commands([FromRoute] string id, [FromBody] CommandRequest requestBody)
{
    object command = null;
    switch (requestBody.Type)
    {
        case CommandType.CallStart:
            command = new Command<CallStartAttributes>
            {
                Type = requestBody.Type,
                Id = id,
                Attributes = JsonConvert.DeserializeObject<CallStartAttributes>(requestBody.Attributes)
            };
            break;
        case CommandType.CallEnd:
            command = new Command<CallEndAttributes>
            {
                Type = requestBody.Type,
                Id = id,
                Attributes = JsonConvert.DeserializeObject<CallEndAttributes>(requestBody.Attributes)
            };
            break;
        default: throw new NotImplementedException();
    }

    await _mediator.Send(command);
    return Ok();
}
对此

public string Attributes { get; set; }

既然反序列化对象总是需要字符串,为什么不将动态序列化为字符串呢。也许有点老套,但这是可行的:

JsonConvert.DeserializeObject<CallEndAttributes>(JsonConvert.SerializeObject(
requestBody.Attributes))

JsonConvert.DeserializeObject<CallStartAttributes>(JsonConvert.SerializeObject(
requestBody.Attributes))
JsonConvert.DeserializeObject(JsonConvert.SerializeObject(
requestBody.Attributes)
反序列化对象(JsonConvert.SerializeObject(
requestBody.Attributes)
我尝试过的问题的最小可复制代码:

using Newtonsoft.Json;
using System;
    
namespace SO
{
    public enum CommandType
    {
        CallStart, CallEnd
    }
    public class CommandRequest
    {
        public CommandType Type { get; set; }
        public dynamic Attributes { get; set; }
    }
    public class Command<TAttributes>
    {
        public string Id { get; set; }
        public CommandType Type { get; set; }
        public TAttributes Attributes { get; set; }
    }
    public class CallStartAttributes
    {
        public string Name { get; set; }
        public string Time { get; set; }
    }
    public class CallEndAttributes
    {
        public string Name { get; set; }
        public bool SendFeedback { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BuildCommand();
            Console.ReadLine();
        }

        public static void BuildCommand()
        {
            CommandRequest requestBody1 = new CommandRequest() { Type = CommandType.CallStart, Attributes = new { Name = "TestStart", Time = "some time" } };
            CommandRequest requestBody2 = new CommandRequest() { Type = CommandType.CallEnd, Attributes = new { Name = "TestEnd", SendFeedback = true } };


            var command1 = new Command<CallStartAttributes>
            {
                Id = "1",
                Type = requestBody1.Type,
                Attributes = JsonConvert.DeserializeObject<CallStartAttributes>(JsonConvert.SerializeObject(requestBody1.Attributes))
            };
            var command2 = new Command<CallEndAttributes>
            {
                Id = "2",
                Type = requestBody2.Type,
                Attributes = JsonConvert.DeserializeObject<CallEndAttributes>(JsonConvert.SerializeObject(requestBody2.Attributes))
            };

        }
    }
}
使用Newtonsoft.Json;
使用制度;
名称空间SO
{
公共枚举命令类型
{
呼叫开始,呼叫结束
}
公共类命令请求
{
公共命令类型类型{get;set;}
公共动态属性{get;set;}
}
公共类命令
{
公共字符串Id{get;set;}
公共命令类型类型{get;set;}
公共属性属性{get;set;}
}
公共类CallStartAttributes
{
公共字符串名称{get;set;}
公共字符串时间{get;set;}
}
公共类礼物
{
公共字符串名称{get;set;}
公共布尔发送反馈{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
BuildCommand();
Console.ReadLine();
}
公共静态void BuildCommand()
{
CommandRequestBody1=new CommandRequest(){Type=CommandType.CallStart,Attributes=new{Name=“TestStart”,Time=“some Time”};
CommandRequestBody2=newCommandRequest(){Type=CommandType.CallEnd,Attributes=new{Name=“TestEnd”,SendFeedback=true};
var command1=新命令
{
Id=“1”,
Type=requestBody1.Type,
Attributes=JsonConvert.DeserializeObject(JsonConvert.SerializeObject(requestBody1.Attributes))
};
var command2=新命令
{
Id=“2”,
Type=requestBody2.Type,
Attributes=JsonConvert.DeserializeObject(JsonConvert.SerializeObject(requestBody2.Attributes))
};
}
}
}

您可以使用Json.Net JObject属性。检查。然后获取您要查找的特定值。

Hmm。我希望我没有对您的输入(requestBody)做出任何错误的假设。我正在发布我在回答中尝试的代码。
JsonConvert.DeserializeObject<CallEndAttributes>(JsonConvert.SerializeObject(
requestBody.Attributes))

JsonConvert.DeserializeObject<CallStartAttributes>(JsonConvert.SerializeObject(
requestBody.Attributes))
using Newtonsoft.Json;
using System;
    
namespace SO
{
    public enum CommandType
    {
        CallStart, CallEnd
    }
    public class CommandRequest
    {
        public CommandType Type { get; set; }
        public dynamic Attributes { get; set; }
    }
    public class Command<TAttributes>
    {
        public string Id { get; set; }
        public CommandType Type { get; set; }
        public TAttributes Attributes { get; set; }
    }
    public class CallStartAttributes
    {
        public string Name { get; set; }
        public string Time { get; set; }
    }
    public class CallEndAttributes
    {
        public string Name { get; set; }
        public bool SendFeedback { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BuildCommand();
            Console.ReadLine();
        }

        public static void BuildCommand()
        {
            CommandRequest requestBody1 = new CommandRequest() { Type = CommandType.CallStart, Attributes = new { Name = "TestStart", Time = "some time" } };
            CommandRequest requestBody2 = new CommandRequest() { Type = CommandType.CallEnd, Attributes = new { Name = "TestEnd", SendFeedback = true } };


            var command1 = new Command<CallStartAttributes>
            {
                Id = "1",
                Type = requestBody1.Type,
                Attributes = JsonConvert.DeserializeObject<CallStartAttributes>(JsonConvert.SerializeObject(requestBody1.Attributes))
            };
            var command2 = new Command<CallEndAttributes>
            {
                Id = "2",
                Type = requestBody2.Type,
                Attributes = JsonConvert.DeserializeObject<CallEndAttributes>(JsonConvert.SerializeObject(requestBody2.Attributes))
            };

        }
    }
}