471,354 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Generic filehandler method

Ron
I am trying to build a generic static method that will return a specified
type from a method. My problem is that I need to return say an image from
the method...and I'm having trouble understanding how to do this.

This is what I have so far...(The new Image is hardcoded right now...I would
like to abstract that out)

private T GetFileFromFileSystem<T>(string filePath, string
fileNameWithoutExtension)
{
string tempExtension = "";

if (Directory.Exists(filePath))
{
if (Path.GetFileNameWithoutExtension(filePath + "\\" +
imageNameWithoutExtension).ToString().Trim().Lengt h 0)
{
tempExtension = Path.GetExtension(filePath + "\\" +
imageNameWithoutExtension);
return (T)(Object)new Bitmap(filePath + "\\" +
imageNameWithoutExtension + "." + tempExtension);
}
else
{
return default(T);
}
}
}

Thanks for any help you might be able to provide,
Ron

Jun 27 '08 #1
14 2086
On 15 May, 14:23, "Ron" <rs_herh...@yahoo.comwrote:
I am trying to build a generic static method that will return a specified
type from a method. My problem is that I need to return say an image from
the method...and I'm having trouble understanding how to do this.

This is what I have so far...(The new Image is hardcoded right now...I would
like to abstract that out)

private T GetFileFromFileSystem<T>(string filePath, string
fileNameWithoutExtension)
{
string tempExtension = "";

if (Directory.Exists(filePath))
{
if (Path.GetFileNameWithoutExtension(filePath + "\\" +
imageNameWithoutExtension).ToString().Trim().Lengt h 0)
{
tempExtension = Path.GetExtension(filePath + "\\" +
imageNameWithoutExtension);
return (T)(Object)new Bitmap(filePath + "\\" +
imageNameWithoutExtension + "." + tempExtension);
}
else
{
return default(T);
}
}
}

Thanks for any help you might be able to provide,
Ron
What do you envisage T as being?
Jun 27 '08 #2
Ron
Thank you for the reply,

Text files, an image right now but I would like the flexibility for other
uses in the future.

Thanks,
Ron
<mr*********@googlemail.comwrote in message
news:48**********************************@d77g2000 hsb.googlegroups.com...
On 15 May, 14:23, "Ron" <rs_herh...@yahoo.comwrote:
>I am trying to build a generic static method that will return a specified
type from a method. My problem is that I need to return say an image
from
the method...and I'm having trouble understanding how to do this.

This is what I have so far...(The new Image is hardcoded right now...I
would
like to abstract that out)

private T GetFileFromFileSystem<T>(string filePath, string
fileNameWithoutExtension)
{
string tempExtension = "";

if (Directory.Exists(filePath))
{
if (Path.GetFileNameWithoutExtension(filePath + "\\" +
imageNameWithoutExtension).ToString().Trim().Leng th 0)
{
tempExtension = Path.GetExtension(filePath + "\\" +
imageNameWithoutExtension);
return (T)(Object)new Bitmap(filePath + "\\" +
imageNameWithoutExtension + "." + tempExtension);
}
else
{
return default(T);
}
}
}

Thanks for any help you might be able to provide,
Ron

What do you envisage T as being?
Jun 27 '08 #3
On Thu, 15 May 2008 06:23:27 -0700, Ron <rs********@yahoo.comwrote:
I am trying to build a generic static method that will return a
specified type from a method. My problem is that I need to return say
an image from the method...and I'm having trouble understanding how to
do this.
It is not clear why you want this method to be generic. Whatever type you
supply for T, it _must_ be valid for the type of data you expect to
extract from the file. That means that in advance, you need to know what
type you expect. That means that there's no need for a generic method to
read the file. Just use existing type-specific i/o mechanisms.

Pete
Jun 27 '08 #4
On 15 May, 16:15, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Thu, 15 May 2008 06:23:27 -0700, Ron <rs_herh...@yahoo.comwrote:
I am trying to build a generic static method that will return a
specified type from a method. My problem is that I need to return say
an image from the method...and I'm having trouble understanding how to
do this.

It is not clear why you want this method to be generic. Whatever type you
supply for T, it _must_ be valid for the type of data you expect to
extract from the file. That means that in advance, you need to know what
type you expect. That means that there's no need for a generic method to
read the file. Just use existing type-specific i/o mechanisms.

Pete
You would need to create your own wrapper classes around each type of
file (ImageLoader,TextFileLoader,BinaryFileLoad) which as pete said
you might as well just make a class with 4 methods to handle each one,
Image and FileStream have nothing in common besides Object so there's
no way of calling a Load method generically
Jun 27 '08 #5
Ron
The subtlety is that using this method the filetype is specified by the
programmer that is coding the caller operation, and I would imagine there is
some way to open a file as a type...is that not the case?

Thanks,
Ron

<mr*********@googlemail.comwrote in message
news:46**********************************@b64g2000 hsa.googlegroups.com...
On 15 May, 16:15, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
>On Thu, 15 May 2008 06:23:27 -0700, Ron <rs_herh...@yahoo.comwrote:
I am trying to build a generic static method that will return a
specified type from a method. My problem is that I need to return say
an image from the method...and I'm having trouble understanding how to
do this.

It is not clear why you want this method to be generic. Whatever type
you
supply for T, it _must_ be valid for the type of data you expect to
extract from the file. That means that in advance, you need to know what
type you expect. That means that there's no need for a generic method to
read the file. Just use existing type-specific i/o mechanisms.

Pete

You would need to create your own wrapper classes around each type of
file (ImageLoader,TextFileLoader,BinaryFileLoad) which as pete said
you might as well just make a class with 4 methods to handle each one,
Image and FileStream have nothing in common besides Object so there's
no way of calling a Load method generically
Jun 27 '08 #6
On 15 May, 16:32, "Ron" <rs_herh...@yahoo.comwrote:
The subtlety is that using this method the filetype is specified by the
programmer that is coding the caller operation, and I would imagine there is
some way to open a file as a type...is that not the case?

Thanks,
Ron
But how would your static method know which method to call on the
object to Load the file? And when you get T back, what common way
would you use a Bitmap or a string?
Jun 27 '08 #7
Ron
Something like this:

<psuedo code>

Image img = GetFileFromFileSystem<Image>("C:\\Images\\","Backg round.bmp");

- or -

String str= GetFileFromFileSystem<String>("C:\\Images\\","Disp layText.txt");


<mr*********@googlemail.comwrote in message
news:b5**********************************@x41g2000 hsb.googlegroups.com...
On 15 May, 16:32, "Ron" <rs_herh...@yahoo.comwrote:
>The subtlety is that using this method the filetype is specified by the
programmer that is coding the caller operation, and I would imagine there
is
some way to open a file as a type...is that not the case?

Thanks,
Ron

But how would your static method know which method to call on the
object to Load the file? And when you get T back, what common way
would you use a Bitmap or a string?
Jun 27 '08 #8
"Ron" <rs********@yahoo.comwrote:
The subtlety is that using this method the filetype is specified by
the programmer that is coding the caller operation, and I would
imagine there is some way to open a file as a type...is that not the
case?
That's not the case at all.

Eq.
Jun 27 '08 #9
On Thu, 15 May 2008 09:35:18 -0700, Ron <rs********@yahoo.comwrote:
Something like this:

<psuedo code>

Image img =
GetFileFromFileSystem<Image>("C:\\Images\\","Backg round.bmp");

- or -

String str=
GetFileFromFileSystem<String>("C:\\Images\\","Disp layText.txt");
But Image and String share absolutely nothing in common with respect to
how they might obtain an instance of said type from a file.

The point of generics is to allow you to write generic code that takes
advantage of commonalities in the type parameter, so that you don't have
to basically write the same code multiple times just to use it with
multiple types. But you're going to have very little code in your generic
method that is shared between the different types it will support, and
there's _no_ general-purpose way to turn an arbitrary file into a specific
type (other than serialization, which isn't what you're doing here), so
your "generic" method will necessarily have to contain special-case code
for every anticipated type parameter (and fail gracefully when used witha
type parameter that wasn't anticipated).

For Image, we already have Image.FromFile(). For String, we already have
StreamReader(string) (constructing a StreamReader for a specific file) and
StreamReader.ReadToEnd() (returns a string that represents the contents of
the file). You can easily wrap the StreamReader functionality in an
extension method you name FromFile() if you like, and likewise any other
specific type you anticipate needing to deal with.

I really just don't see how a generic method is helping you here.

Pete
Jun 27 '08 #10
Ron
Okay waht about returning a File object from the method? So I can do all my
file exists logic and if it exists then return a file and let the calling
code do the processing?
"Paul E Collins" <fi******************@CL4.orgwrote in message
news:2_*********************@bt.com...
"Ron" <rs********@yahoo.comwrote:
>The subtlety is that using this method the filetype is specified by the
programmer that is coding the caller operation, and I would imagine there
is some way to open a file as a type...is that not the case?

That's not the case at all.

Eq.

Jun 27 '08 #11
On Thu, 15 May 2008 10:03:46 -0700, Ron <rs********@yahoo.comwrote:
Okay waht about returning a File object from the method? So I can do
all my file exists logic and if it exists then return a file and let the
calling code do the processing?
All what "file exists logic"? What is there other than to call
File.Exists()?

Note: there's no such thing as a File object. That class is static. You
could return a FileInfo object, which would be useful if you expect to
perform multiple operations from that class using the same file. But
there's nothing about what you've written that suggests that might be the
case. All you really need is a filename, to use a file.

Pete
Jun 27 '08 #12
"Ron" <rs********@yahoo.comwrote:
Okay waht about returning a File object from the method? So I can do
all my file exists logic and if it exists then return a file and let
the calling code do the processing?
I think, rather than generics, inheritance might be a decent approach
here (because, as others have said, the different objects have nothing
in common and no direct mapping to a file type either).

You could have e.g. an abstract FileOpener base class (including the
common bits of logic, like checking for a file's existence) and then
override some method: object OpenFile(string path) so that the derived
classes can offer their own implementation (which might be
Image.FromFile, or File.ReadAllText, or whatever).

The main problem with this idea is that you have to return 'object', so
the caller has to cast the returned object to something useful (Image,
string, etc.). There are various tolerable workarounds, though.

Eq.
Jun 27 '08 #13
On May 15, 5:35 pm, "Ron" <rs_herh...@yahoo.comwrote:
Something like this:

<psuedo code>

Image img = GetFileFromFileSystem<Image>("C:\\Images\\","Backg round.bmp");

- or -

String str= GetFileFromFileSystem<String>("C:\\Images\\","Disp layText.txt");
or

Image img = Image.LoadFromFile(...);
string str = File.OpenText(..)
Jun 27 '08 #14
Ron
Peter,

Thanks. I implemented your suggestion and that will work just fine.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 15 May 2008 10:03:46 -0700, Ron <rs********@yahoo.comwrote:
>Okay waht about returning a File object from the method? So I can do
all my file exists logic and if it exists then return a file and let the
calling code do the processing?

All what "file exists logic"? What is there other than to call
File.Exists()?

Note: there's no such thing as a File object. That class is static. You
could return a FileInfo object, which would be useful if you expect to
perform multiple operations from that class using the same file. But
there's nothing about what you've written that suggests that might be the
case. All you really need is a filename, to use a file.

Pete
Jun 27 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Newton | last post: by
1 post views Thread by Jim | last post: by
1 post views Thread by Kenneth Love | last post: by
26 posts views Thread by raylopez99 | last post: by
reply views Thread by =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.