繁体   English   中英

如何将变量从一个 class 传递到另一个?

[英]How do I pass variables from one class to another?

我正在尝试使用电源自动发布到 azure function 应用程序,然后可以 hash 带有正文的消息和 header 信息并发回值。 请参见下面的示例代码。

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Reflection; 
using System.Security.Cryptography;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    string responseMessage = string.IsNullOrEmpty(name)
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
}

  public class MyHmac
  {
    private string CreateToken(string message, string secret)
    {
        public void DoSomething()
    {
         string var = Task.name;
    }
       // message = Task.name;

      secret = secret ?? "";
      var encoding = new System.Text.ASCIIEncoding();
      byte[] keyByte = encoding.GetBytes(secret);
      byte[] messageBytes = encoding.GetBytes(message);
      using (var hmacsha256 = new HMACSHA256(keyByte))
      {
        byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
        return Convert.ToBase64String(hashmessage);
      }
    }
  }

第一部分接收消息,第二部分处理 hash。我对编码很陌生,无法锻炼如何使用第一个公共 class 中的变量?

假设您正在尝试将变量传递到 MyHmac class 并调用 CreateToken。

类是对象,在对象中你有一个公共的私有变量(其他对象)和函数(可以带参数)。 类中还有其他内容,但这就是解决此问题所需知道的全部内容。

我看到的一个问题是 CreateToken 是私有的 function(它应该是公共的)。 另一个问题是string var = Task.name; (不能使用“var”,因为它是保留的 object 类型)。

为了让您的Task<IActionResult> Run在 MyHmac 中调用 function,您首先需要在“Run”function 中创建一个 MyHmac 变量,然后让新的 MyHmac 变量调用您想要的 MyHmac class 中的任何 function。

前任:

Run(HttpRequest req, ILogger log){
MyHmac localVariable = new MyHmac();
string secret = "example secret string";
string tokenResponseMessage = localVariable.CreateToken("here's a message string", secret);
}
return new OkObjectResult(tokenResponseMessage);

值作为方法参数传递给方法。

将方法声明为public以便可以从其他类调用它, static以便可以在没有实例的情况下调用它:

  public class MyHmac
  {
    public static string CreateToken(string message, string secret)
    {
      secret = secret ?? "";
      var encoding = new System.Text.ASCIIEncoding();
      byte[] keyByte = encoding.GetBytes(secret);
      byte[] messageBytes = encoding.GetBytes(message);
      using (var hmacsha256 = new HMACSHA256(keyByte))
      {
        byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
        return Convert.ToBase64String(hashmessage);
      }
    }
  }
name = name ?? data?.name;
string token = MyHmac.CreateToken(name, "something secret");

在上面的最后一行代码中,您将name作为第一个参数的值传入意味着CreateToken将在其message参数中使用该值进行调用。

或者,您可以将其public但不是static ,并创建一个new MyHmac()实例来调用该方法,正如 Zeke 的回答所说。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM