我有一個回傳系結到網格的 List 的類。我的一個屬性是一個 byte[] 陣列,只有在有人雙擊帶有檔案的網格單元格中的單元格時才應該填充它。那時,檔案應該被下載并呈現給用戶。只是一個帶有檔案名和 Process.Start(...
因為我將串列系結到網格,所以它呼叫“檔案”屬性并填充值,賦予它一種急切型別的行為,而不是我正在尋找的惰性行為。
有什么方法可以阻止此屬性填充資料類(如下),而無需修改 UI 級別網格以明確不讀取檔案列?
public class Errors
{
//...
private bool hasFile;
public bool HasFile { get { return HasFile; } }
private byte[] file;
public byte[] File
{
get
{
if (HasFile) { file = FileHelper.DownloadAndDecompress(this.ID, "ErrorsDownloadFile"); }
return file;
}
set { file = value; }
}
public static List<Errors> FindByAppName(string AppName, DateTime StartDate, DateTime EndDate) {/*...*/}
//...
}
我做了一些閱讀,Lazy<T>但是,我正在努力讓它進入我的常規作業流程,因為當我實作它時,我無法將值分配給 setter 執行檔案上傳的值......
嘗試但我無法將 byte[] 分配回File屬性,因為它是只讀的...
public bool HasFile { get { return HasFile; } }
public Lazy<byte[]> File
{
get
{
if (HasFile) { return new Lazy<byte[]>(() => FileHelper.DownloadAndDecompress(this.ID, "ErrorsDownloadFile")); }
else { return null; };
}
set { }
}
Lazy<T>非常感謝有關如何使用或其他方法正確實作惰性屬性的任何提示。
uj5u.com熱心網友回復:
如果我理解你的意思,你想實作延遲下載,但渴望上傳
// Eager download (will be used in lazy download)
private byte[] DownLoad() {
// download here:
return FileHelper.DownloadAndDecompress(this.ID, "ErrorsDownloadFile");
}
// Lazy download ...
private Lazy<byte[]> m_File;
// ... which we assign in the constructor
public MyClass() {
...
m_File = new Lazy<byte[]>(Download);
...
}
// Our property
public byte[] File {
get {
// Lazy download
return m_File.Value;
}
set {
// we can't assign m_File.Value but we can recreate m_File
m_File = new Lazy<byte[]>(value);
//TODO: Upload here
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385761.html
上一篇:將八進制轉換為dex整數
