Generic filehandler method

R

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().Length > 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
 
M

mrshrinkray

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().Length > 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?
 
R

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
 
M

mrshrinkray

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
 
R

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
 
M

mrshrinkray

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?
 
R

Ron

Something like this:

<psuedo code>

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

- or -

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

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?
 
M

mrshrinkray

Something like this:

<psuedo code>

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

- or -

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

or

Image img = Image.LoadFromFile(...);
string str = File.OpenText(..)
 

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