反射?或许吧,不过我没成功。 请教个关于资源使用的问题。
我现在有很多的图形资源,如何才能方便地调用呢?
这么写程序肯定是不好的。如果添加或删除资源的话还得修改case,很麻烦。
怎样做才能简化呢?Image 资源2Image(string 名称)
{
switch (名称)
{
case "中球乳白":
img = qsh.水晶球.Properties.Resources.中球乳白;
break;
......
case "中球咖啡":
img = global::qsh.水晶球.Properties.Resources.中球咖啡;
break;
}
}
我现在有很多的图形资源,如何才能方便地调用呢?
这么写程序肯定是不好的。如果添加或删除资源的话还得修改case,很麻烦。
怎样做才能简化呢?Image 资源2Image(string 名称)
{
switch (名称)
{
case "中球乳白":
img = qsh.水晶球.Properties.Resources.中球乳白;
break;
......
case "中球咖啡":
img = global::qsh.水晶球.Properties.Resources.中球咖啡;
break;
}
}
Image GetImage(string imageName)
{
Image image = null;
try
{
Type type = typeof(qsh.水晶球.Properties.Resources);
if (type != null)
{
PropertyInfo property = type.GetProperty(imageName,
BindingFlags.IgnoreCase |
BindingFlags.Static |
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.DeclaredOnly); if (property != null)
image = property.GetValue(null, null) as Image;
}
}
catch { }
return image;
}
//动态创建配置文件。
File.Create(this.configPath);
Configuration config = ConfigurationManager.OpenExeConfiguration(this.configPath);
config.AppSettings.Settings.Add("中球乳白", "中球乳白图片路径");
config.AppSettings.Settings.Add("中球咖啡", "中球咖啡图片路径");//读取配置文件
Configuration config = ConfigurationManager.OpenExeConfiguration(this.configPath);
parth1 = config.AppSettings.Settings["中球乳白"].Value ;
parth2 = config.AppSettings.Settings["中球咖啡"].Value ;