匿名托管的DynamicMethods程序集:无法将类型'string'隐式转换为'int'

问题描述

我正在使用AZURE函数。该函数的目的是 计算模块的状态:失败,允许通过,通过或有区别通过。运行代码时出现此错误。由于是整数,因此在mark处出现错误

Executed 'Function1' (Failed,Id=90c68ac4-4bc1-4446-9ad4-2b15e8ea8a00,Duration=145230ms)
System.Private.CoreLib: Exception while executing function: Function1. Anonymously Hosted Dynamicmethods Assembly: Cannot implicitly convert type 'string' to 'int'.

课程:

class MarkEntry
{
    public string StudentNumber { get; set; }
    public string Name { get; set; }
    public int Mark { get; set; }
    public string Module { get; set; }
}

}

Azure功能接受4个输入,并检查学生是否具有通过的通行证:

    [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous,"get","post",Route = null)] HttpRequest req,ILogger log)
        {
            MarkEntry obj = new MarkEntry();
            string responseMessage ="";
            string respmaessage="";
            string result="";
            log.Loginformation("C# HTTP trigger function processed a request.");

            obj.StudentNumber = req.Query["studentnumber"];
           obj.Name = req.Query["name"];
           obj.Module = req.Query["module"];
            obj.Mark =Convert.ToInt32(req.Query["mark"]);


            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            obj.StudentNumber = obj.StudentNumber ?? data?.name;
            obj.Name = obj.Name ?? data?.name;
            obj.Module = obj.Module ?? data?.name;
           obj.Mark = obj.Mark.ToString() ?? data?.obj.Mark.Tostring();







            if (string.IsNullOrEmpty(obj.StudentNumber))
            {
                respmaessage = "Enter a student number";
                return new OkObjectResult(respmaessage);

            }


            if (string.IsNullOrEmpty(obj.Name))
            {
                respmaessage = "Enter a student Name";
                return new OkObjectResult(respmaessage);

            }
         
            
            if (string.IsNullOrEmpty(obj.Module))
            {
                respmaessage = "Enter Student Module";
                return new OkObjectResult(respmaessage);

            }


            if (obj.Mark<49)
            {
                result = "Condoned Pass";

            }

            responseMessage = string.Format($"Student Number: {obj.StudentNumber}" + "\n" +
$"Student Name: {obj.Name}" + "\n" + $"Student Mark: {obj.Mark}" + "\n" +
$"Student Module: {obj.Module}" + "\n" + $"Student Result: {result}"
                );
              

            return new OkObjectResult(responseMessage);

        }
    }

解决方法

MarkEntry类的Mark属性定义为整数类型。在azure函数中,我们实例化了此类。 ...

MarkEntry obj = new MarkEntry();

然后在函数的第13行左右...

obj.Mark = obj.Mark.ToString() ?? data?.obj.Mark.Tostring();

似乎我们正在尝试为该整数分配一个字符串值。 不确定这是否行得通?