通用文件处理程序方法 [英] Generic filehandler method

查看:63
本文介绍了通用文件处理程序方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个通用的静态方法,它将从方法返回指定的

类型。我的问题是我需要返回说出来自

方法的图像...而且我很难理解如何做到这一点。


这就是我到目前为止......(新的图像现在是硬编码的...我会

喜欢将其抽象出来)


private T GetFileFromFileSystem< T>(字符串filePath,字符串

fileNameWithoutExtension)

{

string tempExtension ="" ;;
<如果(Directory.Exists(filePath))

如果(Path.GetFileNameWithoutExtension(filePath +" \\)

imageNameWithoutExtension).ToString()。Trim()。Lengt h 0)

{

tempExtension = Path.GetExtension(filePath +" \\" +

imageNameWithoutExtension);

return(T)(Object)new Bitmap(filePath +" \\" +

imageNameWithoutExtension +"。" + tempExtension);

}

else

{

返回默认值(T);

}

}

}


谢谢你你可能提供的任何帮助,

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

推荐答案

5月15日,14:23 ,罗恩 < rs_herh ... @ yahoo.comwrote:
On 15 May, 14:23, "Ron" <rs_herh...@yahoo.comwrote:

我正在尝试构建一个返回指定

类型的通用静态方法从一种方法。我的问题是我需要返回说出来自

方法的图像...而且我很难理解如何做到这一点。


这就是我到目前为止......(新的图像现在是硬编码的...我会

喜欢将其抽象出来)


private T GetFileFromFileSystem< T>(字符串filePath,字符串

fileNameWithoutExtension)

{

string tempExtension ="" ;;
<如果(Directory.Exists(filePath))

如果(Path.GetFileNameWithoutExtension(filePath +" \\)

imageNameWithoutExtension).ToString()。Trim()。Lengt h 0)

{

tempExtension = Path.GetExtension(filePath +" \\" +

imageNameWithoutExtension);

return(T)(Object)new Bitmap(filePath +" \\" +

imageNameWithoutExtension +"。" + tempExtension);

}

else

{

返回默认值(T);

}

}

}


感谢您提供的任何帮助,

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



你认为T是什么?

What do you envisage T as being?


谢谢你的回复,


文本文件,现在是一个图像,但我希望将来可以灵活地使用其他




谢谢,

Ron

< mr ********* @ googlemail.comwrote in message

news:48 * ********************************* @ d77g2000 hsb.googlegroups.com ...
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...

5月15日,14:23,Ron < rs_herh ... @ yahoo.comwrote:
On 15 May, 14:23, "Ron" <rs_herh...@yahoo.comwrote:

>我正在尝试构建一个通用的静态方法,它将返回一个指定的
类型方法。我的问题是我需要从
方法返回说出一张图片......而且我很难理解如何做到这一点。

这就是我的意思到目前为止......(新的图像现在是硬编码的...我想
想要抽象出来)

私人T GetFileFromFileSystem< T>(字符串filePath ,string
fileNameWithoutExtension)
{
string tempExtension ="" ;;

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

{
返回默认值(T);
}
}


感谢您的帮助能够提供,
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().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



你认为T是什么?


What do you envisage T as being?


2008年5月15日星期四06:23:27 -0700,Ron< rs ******** @ yahoo.comwrote:
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.



目前尚不清楚为什么要将此方法设为通用方法。无论你为T提供什么类型的资金,它都必须对你希望从文件中提取的数据类型有效。这意味着提前,您需要知道您期望的
类型。这意味着没有必要使用通用方法来获取
读取文件。只需使用现有的特定于类型的i / o机制。


Pete

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


这篇关于通用文件处理程序方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆