sjk.Properties.Resources._1051 用反射Reflection.MemberInfo 试试看,我没试过。

解决方案 »

  1.   

    这样能行吗?image和string 这两个不是一类的哦。好像不能直接相互赋值吧?
      

  2.   

    “取字符串变量中的内容”?不是变量的名字? 那我自作多情了=取一下不可以么?或者ToString()一下呵呵
      

  3.   

    pictureBox1.Image   =str1.ToString();
    我试了也不行的.
      

  4.   

    不是,楼主你现在的问题很不明确,现在已有的是什么?sjk.Properties.Resources._1051 这是个什么类型的变量?你希望是获得这个变量的名字还是这个变量的内容?还是说你已经有了这个字符串“sjk.Properties.Resources._1051”,而你需要把这个字符串转换成这个名字的变量sjk.Properties.Resources._1051 ?光看问题我看不出你的需求来 
      

  5.   

    pictureBox1.Image   =sjk.Properties.Resources._1051(是一个图片的地址)
    直接这么使用没有问题,现在我想把sjk.Properties.Resources._1051送给一个字符串变量,
    string   str1   =   "sjk.Properties.Resources._1051"; 
    这样就比较灵活,比如我要用pictureBox1.Image来显示下一个图片,sjk.Properties.Resources._1052.
    我只需要改变这个变量str1="sjk.Properties.Resources._1052"; 
    但是把它送给控件
    pictureBox1.Image   =str1;(这一句调试通不过)
    应该如何改.
    为谢!
      

  6.   

    楼上的:
      我觉得楼主的问题很明确啊,sjk.Properties.Resources._1051 谁都能猜到是Image类型的(.ico,.jpg,.bmp等)只不过这个问题不知道如何在C#下解决。要是在VBScript或JavaScript中就比较简单,用Eval()函数就能实现。
      

  7.   

    你是要打开字符串指定的资源吧?
    如果你的资源是个Bitmap,用pictureBox1.Image   = new Bitmap("sjk.Properties.Resources._1051")就可以了
    如果是其它类型的资源,就生成相应实例就可以了。
      

  8.   

    说是反射可以实现,System.Relection,但是在这个例子中应该怎么用,我还是不懂.
      

  9.   

    我要就是希望能够将pictureBox1.Image =new Bitmap("sjk.Properties.Resources._1051")
    中的"sjk.Properties.Resources._1051"用一个字符串变量来代替.应该如何做.
    比如:str1="sjk.Properties.Resources._1051"那么:pictureBox1.Image =new Bitmap(str1),这么写却不对,应该如何写
      

  10.   

    new   Bitmap(str1),中的str1应该不是sjk.Properties.Resources._1051这样而是一个路径,比如c:\123.jpg等等
    sjk.Properties.Resources._1051是资源文件中的图吧,怎么找到他的绝对路径是问题所在,我感觉
      

  11.   

    还有你这个sjk是啥东西啊?你自定义的类?
      

  12.   

    sjk应该他的程序的命名空间(namespace)名称吧。
      

  13.   

    我的意思不是让你直接这么写,而是提供一个思路.如果你有那个资源的路径可以这么写._1051是sjk.Properties.Resources的一个成员吗?Resources里是你已经读入的资源吗?如果这样的话只能用反射来根据成员名取成员的值。
    查一下MSDN就可以了。
      

  14.   

    我在网上找了一些有关“C# 动态编译”的文章,按原理应该能实现楼主的要求,但做了些测试,就是取不到Properties这个对象。
      

  15.   

    如果前面的sjk.Properties.Resources已经确定的,变化的只有_1051的话,我想可能可以这样操作,将sjk.Properties.Resources里面的_1051之类都改成属性     
    class Program
        {
            static void Main(string[] args)
            {
                Type t = typeof(Resources);
                Resources r1 = new Resources();            System.Reflection.PropertyInfo pi = t.GetProperty("_1052");
                int w2=(int)pi.GetValue(r1,null);            Console.WriteLine(w2);
            }
        }    class Resources
        {
            public int _1051
            {
                get { return 1051; }
            }        public int _1052
            {
                get { return 1052; }
            }        public int _1053
            {
                get { return 1053; }
            }    }
     
     
    如果_1052什么的是Resources的静态static成员,可以参照下面的例子,我就不具体写了 http://topic.csdn.net/t/20041210/09/3632423.html 
     
      

  16.   

    这篇文章你看看对你有没有什么用?http://www.wangchao.net.cn/bbsdetail_75499.html
      

  17.   

    你不如这样做:在项目中新添一个资源文件ImageResource.resx,然后把要用到的图像添加到这个资源文件中。
    ...
    using System.Reflection;
    using System.Resources;namespace sjk
    {
       ...   private void button3_Click(object sender, EventArgs e)
       {
          string str1 = "_1052";  //在str1中只存放资源图像的索引名称
          this.pictureBox1.Image = GetResourceByKey("sjk.ImageResource", str1);
       }
            
       public static Image GetResourceByKey(string resourceName, string key)
       {
          // Create a resource manager to retrieve resources.
          ResourceManager rm = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());      rm.IgnoreCase = true;      // Retrieve the value of the string resource named 
          Image strValue = (Image)rm.GetObject(key);      rm = null;      return strValue;
       }}
      

  18.   

    太简单了吧?用反射就可以了。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;namespace FormTestA
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.Image = FormTestA.Properties.Resources.Image1;
                pictureBox1.Image = GetImageFromResource("Image1");
                pictureBox1.Image = GetImageFromResource("FormTestA.Properties.Resources.Image1");
                pictureBox1.Image = GetImageFromResource("FormTestA.Properties.Resources", "Image1");
                //pictureBox1.Image = GetImageFromResource("Properties.Resources", "Image1"); //错误, 需要类型的完全限定名称
            }        private Image GetImageFromResource(string fullname)
            {
                if (fullname == null) throw new ArgumentNullException("fullname");            int index;            return (index = fullname.LastIndexOf('.')) == -1 ? GetImageFromResource("FormTestA.Properties.Resources", fullname)
                    : GetImageFromResource(fullname.Substring(0, index), fullname.Substring(index + 1));
            }        private Image GetImageFromResource(string type, string resource)
            {
                return (Image)Type.GetType(type).GetProperty(resource, BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
            }
        }
    }
      

  19.   

    既然是地址,为什么不直接用ImageUrl来接收
      

  20.   

    你只有把_1051负值给str1
    在用 sjk.Properties.Resources.str1
    pictureBox1.Image   =sjk.Properties.Resources.str1
    你这样做试试。
      

  21.   

    用过的方法string pic="_1051";
    pictureBox1.Image =(Image)Properties.Resources.ResourceManager.GetObject(pic); 我们就是这样做的。。 给分吧  呵呵
      

  22.   

    string pic="_1051"; 
    pictureBox1.Image =(Image)Properties.Resources.ResourceManager.GetObject(pic);
    可以得