我有需要数据的类,可以是字节或文件路径。
目前我将文件读入字节数组,然后设置类。在另一个分隔符中,它直接从作为参数传递的字节中设置类。
我希望第一个构造函数(文件路径)调用第二个(字节),例如:
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);
}
那么如何从第二个构造函数调用第一个构造函数(以保存复制和粘贴代码)?