Extension methods for System.IO.File How?

I

Ilyas

Hi

I want to extend the System.IO.File type by having a file ToByteArray
method, however when I attempt to do this:

public static byte[] ToByteArray(this File fileObject, string
filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
fs.Close();
return data;
}


I get the error:static types cannot be used as parameters

Does this mean then I cant use the File type as a parameter, because
its a static type?

How can I extend thie type then, if at all?
 
A

Arne Vajhøj

Ilyas said:
I want to extend the System.IO.File type by having a file ToByteArray
method, however when I attempt to do this:

public static byte[] ToByteArray(this File fileObject, string
filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
fs.Close();
return data;
}


I get the error:static types cannot be used as parameters

Does this mean then I cant use the File type as a parameter, because
its a static type?
Yes.

How can I extend thie type then, if at all?

You can not.

Go after FileInfo instead.

Arne
 
J

Jeff Johnson

Now, all that said...

To the OP: why don't you just call File.ReadAllBytes() instead?

Agreed. Toxxx() methods are almost universally instance methods, not static
methods. (The Convert class is a notable exception.) So I for one would find
it odd to have to supply a file name to a ToByteArray() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top