异想天开?!动态指定属性名?难道需要用反射么?

xpnew 2008-07-18 04:01:26
中午没吃饭提了一个关于枚举的问题。下午发现还有更严重的问题:我似乎得研究动态指定ImageFormat的属性!

---------------代码简要:

context.Response.ContentType = "image/gif";
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

----------------------
我需要的是根据指定的string imgType = Request.QueryString["imgType"]。决定ContentType 和ImageFormat的属性。
我在网上看到的代码是用switch/select case的。(例如:http://blog.163.com/huang_ying_lu/blog/static/26999832008325111332567/)
能用string imgType指定ImageFormat的属性是Gif还是Jpeg么?
这似乎是一个异想天开的问题。
...全文
119 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuadam 2008-07-19
  • 打赏
  • 举报
回复

string pName = "Gif";
Type t = typeof(System.Drawing.Imaging.ImageFormat);
PropertyInfo p = t.GetProperty(pName, BindingFlags.Static | BindingFlags.Public);
System.Drawing.Imaging.ImageFormat format = (System.Drawing.Imaging.ImageFormat)p.GetValue(null, null);
xpnew 2008-07-19
  • 打赏
  • 举报
回复
三楼的,能给一个反射的代码么?
PS:我不懂反射,希望你能说得详细点,谢谢!
zhujiechang 2008-07-19
  • 打赏
  • 举报
回复
ContentType 属性仅仅是指定输出的流是图片而已,至于是不是还取决你最后的输入内容。
这个属性和反射不搭调
cheng_feng001 2008-07-18
  • 打赏
  • 举报
回复
1楼的,真相只有一个!
shadowgreen 2008-07-18
  • 打赏
  • 举报
回复
学习····
fuadam 2008-07-18
  • 打赏
  • 举报
回复
因为System.Drawing.Imaging.ImageFormat不是枚举类型的,所以你只能switch或者反射
cacar2008 2008-07-18
  • 打赏
  • 举报
回复
可以的
aaajedll 2008-07-18
  • 打赏
  • 举报
回复
學習

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧