[英]multiple constructors in C#
我有需要數據的類,它可以是字節或文件路徑。
此刻,我將文件讀入字節數組,然后設置類。 在另一個分隔符中,它直接根據作為參數傳遞的字節來設置類。
我希望第一個構造函數(文件路徑)調用第二個(字節),類似於:
public DImage(byte[] filebytes) : this()
{
MemoryStream filestream = null;
BinaryReader binReader = null;
if (filebytes != null && filebytes.Length > 0)
{
using (filestream = new MemoryStream(filebytes))
{
if (filestream != null && filestream.Length > 0 && filestream.CanSeek == true)
{
//do stuff
}
else
throw new Exception(@"Couldn't read file from disk.");
}
}
else
throw new Exception(@"Couldn't read file from disk.");
}
public DImage(string strFileName) : this()
{
// make sure the file exists
if (System.IO.File.Exists(strFileName) == true)
{
this.strFileName = strFileName;
byte[] filebytes = null;
// load the file as an array of bytes
filebytes = System.IO.File.ReadAllBytes(this.strFileName);
//somehow call the other constructor like
DImage(filebytes);
}
else
throw new Exception(@"Couldn't find file '" + strFileName);
}
那么如何從第二個調用第一個構造函數(以保存復制和粘貼代碼)呢?
您可以創建一個以byte[]
作為參數的私有方法,例如ProcessImage(byte[] myparam)
,這兩個構造函數都將調用該方法來處理您的字節。
旁注:您可能要考慮使用stream
而不是byte[]
。
快速示例:
public DImage(byte[] filebytes) : this() // Remove if no parameterless constructor
{
MemoryStream filestream = null;
BinaryReader binReader = null;
if (filebytes != null && filebytes.Length > 0)
{
using (filestream = new MemoryStream(filebytes))
{
this.ProcessStream(filestream);
}
}
else
throw new Exception(@"Couldn't read file from disk.");
}
public DImage(Stream stream) : this() // Remove if no parameterless constructor
{
this.ProcessStream(stream);
}
public DImage(string strFileName) : this() // Remove if no parameterless constructor
{
// make sure the file exists
if (System.IO.File.Exists(strFileName) == true)
{
this.strFileName = strFileName;
// process stream from file
this.ProcessStream(System.IO.File.Open(strFileName));
}
else
throw new Exception(@"Couldn't find file '" + strFileName);
}
...
private ProcessStream(Stream myStream)
{
if (filestream != null && filestream.Length > 0 && filestream.CanSeek == true)
{
//do stuff
}
else
throw new Exception(@"Couldn't read file from disk.");
}
我實際上建議公開兩個靜態方法:
public static DImage FromFile(string filename)
{
// Load image, then call constructor
}
public static DImage FromData(byte[] data)
{
// Do anything you need to, then call the constructor
}
構造函數的確切形式由您決定,但我可能會將其設為私有。 根據我的經驗,使用靜態工廠方法可以使代碼更清晰,這意味着您可以推遲調用實際的構造函數,直到您准備好實際工作為止。 這有助於將字段設置為只讀等。最大的缺點是缺少對繼承的支持。
我可以想象實現3個構造函數:
System.IO.StreamReader
訪問的System.Stream
System.IO.MemoryStream
並調用第一個構造函數。 System.IO.FileStream
加載它並調用第一個構造函數。 這是一個例子:
using System.IO;
// ...
public DImage(Stream Stream)
{
using (var reader = new StreamReader(Stream))
{
// Read the image.
}
}
public DImage(byte[] Bytes)
: this(new MemoryStream(Bytes))
{
}
public DImage(string FileName)
: this(new FileStream(FileName, FileMode.Open, FileAccess.Read))
{
}
這也使處理異常更加容易。 如果該文件不存在,則FileStream的構造方法將拋出System.IO.FileNotFoundException
,因此無論您在實例化DImage
類的DImage
位置,都可以對其進行處理:
try
{
var image = new DImage(@"C:\Test.img");
}
catch (System.IO.FileNotFoundException e)
{
// The image could not be found.
}
catch (Exception e)
{
// Something else happened.
}
此方法在構造函數后面使用關鍵字this
來將特殊構造案例委托給默認構造函數。 它有兩個優點:
System.Stream
提高可重用性。 客戶端可以調用構造函數從內存塊或文件系統條目中加載圖像,也可以實現自己的流以提供自定義數據源(如DB BLOB,NetworkStreams等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.