从另一个类和方法中按需获取变量

huangapple go评论81阅读模式
英文:

Getting a variable from another class and method on demand

问题

我不知道是否可能,但我试图实现的目标是只在需要时从字段中获取敏感密码,然后清除它(而不是每次都使用自定义清除方法)。

所以我有这个类/方法来获取:

public class letterhead
{
    public static letterhead phpClass;
    public string letterheadres;

    void Start()
    {
        phpClass = this;
    }

    public void Letterhead()
    {
        StartCoroutine(LetterheadAsync());
    }

    IEnumerator LetterheadAsync()
    {
        UnityWebRequest www = UnityWebRequest.Get("https://mysite/letterhead1.php?dg=letterhead");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            var result = www.downloadHandler.text;
            result = www.downloadHandler.text;
            Debug.Log(result);
            letterheadres = result;
        }
    }
}

以及我需要letterheadres的这个类:

public class Password
{
    private string letterhead3 = letterhead.phpClass.letterheadres;
}

现在,正如您所看到的,我必须首先调用Letterhead()来获取letterheadres并手动清除它,我试图实现的是,当我需要来自Password类的letterhead3时,从letterhead类的Letterhead()获取它,然后自动清除它。

英文:

I don't know if this is possible but what I'm trying to achieve is to get a sensitive password from a field only when needed and then clear it (without custom clear methods every time).

so I have this class/method to get :

public class letterhead
{
    public static letterhead phpClass;
    public string letterheadres;

    void Start()
    {
        phpClass = this;
    }

    public void Letterhead()
    {
        StartCoroutine(LetterheadAsync());
    }
    
    IEnumerator LetterheadAsync()
    {
        UnityWebRequest www = UnityWebRequest.Get($"https://mysite/letterhead1.php?dg=letterhead");
        yield return www.SendWebRequest();


        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {

            var result = www.downloadHandler.text;
            result = www.downloadHandler.text;
            Debug.Log(result);
            letterheadres = result;                
        }
    }
}

and this Class that I need the letterheadres:

  public class Password{
    private string letterhead3 = letterhead.phpClass.letterheadres;
}

now as you can see I have to first call the Letterhead() to get the letterheadres and clear it myself, what I'm trying to achieve is that when I need the letterhead3 from Password class, to get it from Letterhead() from letterhead class and then clear it automatically.

答案1

得分: 1

public class Password {
public static string letterhead
{
get
{
string value = letterhead.phpClass.letterheadres; // Make a copy which you will return
// Write your clearing procedure here
// letterhead.phpClass.letterheadres = null;
return value;
}
}

// You can get the value from other class as
string value = Password.letterhead; // If there is a value, You will get it and it will be cleared automatically due to the getter.

}

Make sure you have fetched the data before you access it. Moreover, this will work once. If you need the data again, You have to make the web request again and get the value after it is fetched. If you get the correct value, It is guaranteed that the contents (In the letterhead class) will be cleared.

英文:

Are you looking for something like this?

public class Password {
    public static string letterhead
    {
        get
        {
            string value = letterhead.phpClass.letterheadres; // Make a copy which you will return
            // Write your clearing procedure here
            // letterhead.phpClass.letterheadres = null;
            return value;
        }
    }
}

// You can get the value from other class as
string value = Password.letterhead; // If there is a value, You will get it and it will be cleared automatically due to the getter.

Make sure you have fetched the data before you access it. Moreover, this will work once. If you need the data again, You have to make the web request again and get the value after it is fetched. If you get the correct value, It is guaranteed that the contents (In the letterhead class) will be cleared.

huangapple
  • 本文由 发表于 2020年1月3日 22:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定