473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Extension methods for System.IO.File How?

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?
Nov 11 '08 #1
3 4587
Ilyas wrote:
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
Nov 11 '08 #2
On Tue, 11 Nov 2008 12:10:13 -0800, Arne Vajhøj <ar**@vajhoej.dkwrote:
[...]
>How can I extend thie type then, if at all?

You can not.

Go after FileInfo instead.
Or, since even if you could extend a static type in C# you'd still always
have to type a typename when using your extension method, you could just
put your method in a static class of your own making and type that
typename instead. Extending a static class doesn't really provide the
OOP-like improvements that extending an actual object would anyway.

It's true that since FileInfo is a non-static type, one can write an
extension method for the type. But doing so would completely change the
semantics of the operation, resulting the newly added requirement to
instantiate an object for an operation that inherently needs no such
object.

Now, all that said...

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

Pete
Nov 11 '08 #3
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
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.
Nov 12 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Don: Bucknell Web Application Developer | last post by:
I've been scouring the Google Usenet archives, and I can find neither any article that addresses this question, nor any group that seems dead-on appropriate for asking it; I posted this in alt.php,...
8
by: A.M-SG | last post by:
Hi, I understand that we can install SOAP extensions through web.config at the server side. How do I install them at the client proxy side? Thank you, Alan
0
by: Robert Rotstein | last post by:
I want to write a SOAP extension that gets activated from both the client and the server. From whast I have read, this should be quite easy to do. Yet I can only activate it from the server. I...
0
by: Robert Rotstein | last post by:
I want to write a SOAP extension that gets activated from both the client and the server. But so far, I have only been able to activate from the server. I have a service (.asmx file) which...
2
by: Frederik Vanderhaegen | last post by:
Hi, I'm writing a simple soap extension for a webservice I developed (without the use of an extension the webservice works perfect). The extension is registered through the web.config files of...
5
by: pythoncurious | last post by:
Hi, I'm having a bit of trouble when writing a python extension. I can't seem to figure out what I did wrong. I tried to make a minimal example, but it's still quite a bit of code. It would be...
8
by: Steven Nagy | last post by:
Some simple yes/no questions. I tried googling first and couldn't find specific answers. These are related to .Net 3.5 extension methods 1) I found that I would like to add properties as well as...
4
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this:...
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
1
by: Simon Woods | last post by:
Hi I have 2 classes which are both instantiated by shared factory methods and both implement a common interface. Public Class Factory Public Shared Function CreateMyClass1() As MyClass1...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.