我目前做一个程序,使用.NET和SQL  SERVER,语言c#。
打算用数据驱动做一个运算类模型。
前台程序中有这样一个问题,假设存在多个数组,其中有一个
dooble[]  salary=new  double[20];在后台数据库中,我记录这两个数据的位置的情况大概如下:
字段说明  数组名  下标  运算关系  数组名   下标
字段类型  char     int   char      char     int
数值     salary     1     +      salary     2
它的意思是说,需要系统把salary 数组中1,2位置上的数据相加,即salary[1]+salary[2]。
从sql中读取这个信息后,只能这样获取信息
string str="salary";
int   nIndex1=1; 或者 string nIndex="1";
在程序中如何处理 str 字符串,使系统能够制定,我要调用数十个数组中 数组名称与str字符串相同的那个数组。并可以获得该数组某个位置的值:
如  double temp=(名称与str字符相同的数组)[1];

解决方案 »

  1.   

    随便写了个例子:using System;
    using System.Reflection;namespace ConsoleApplication2
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    string propertyName = "Numbers"; Class2 instance = new Class2(); Type type = typeof(ConsoleApplication2.Class2); PropertyInfo pi = type.GetProperty(propertyName,typeof(string[]));
    string[] num = (string[])pi.GetValue(instance,null); for(int i = 0;i < num.Length;i++)
    {
    Console.WriteLine(num[i].ToString());
    } Console.ReadLine();
    }
    }
    }
      

  2.   

    using System;namespace ConsoleApplication2
    {
    /// <summary>
    /// Summary description for Class2.
    /// </summary>
    public class Class2
    {
    string[] str1 = new string[]{"13","7"}; public string[] Numbers
    {
    get
    {
    return str1;
    }
    } public Class2()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    }
    }
      

  3.   

    我是一个c#的新手,呵呵,好多地方不太明白,还请多指教。
    我的程序在WebAppilcation中开发,能否给一个WebAppilcation的详细例子。万分感谢。
    假设,在WebAppilcation中,我已经定义了一些数组:
    double[] salary=new salary[20];
    double[,] age=new age[40,20];
    double[,] num=new double[10,20]; 
    注:以上数组内部的某些位置记录了一些我们需要的数据,数组未记录数据的地方为空。如果 我获取了一个字符串,string str="num";
    并且,要获取num[5,4]中的数据:就是说,nIndex1=5.nIndex2=4;
    通过str、nIndex1、nIndex2如何修改您给我的程序,获得num[5,4]这个数据。上次留言中这段程序:
    Class2 instance=new Class2(); 
    Type type =typeof(ConsoleApplication2.Class2);  
    PropertyInfo pi=type.GetProperty(propertyName,typeof(string[]));  
    string[]num=(string[])pi.GetValue(instance,null);  
    我有些不太会引用,对于我以上假设的情况,这个instance应该是什么啊?
    typeof(string[])指的是串propertyName的那个类型(string)吗?
      

  4.   

    class A
    {
    string a = "aa"; Type typeA = this.GetType();
    FieldInfo fiA = typeA.GetField("a",BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
    object objB = fiA.GetValue(objA);
    //objB的植为"aa"
    }
      

  5.   

    看着就头晕,为什么不用Class 或struct 或emnu我感觉哪个都比你用字符串数组好些如果非要用字符串数组,换这个看看是不是能达到你需要的
    system.Collections.Specialized.NameValueCollection NameValue= 
        new System.Collections.Specialzed.NameValueCollentions()NameValue.add ("char","salary");
    NameValue.add ("int","1");
    NameValue.add ("char","+");
    ......
    int result = (int) NameValue ("int") + (int) NameValue ("otherInt"); 
      

  6.   

    我c#不是很熟悉,能否在回复中给一些注释或者详细的说明。万分感谢!!!!
    我不是一定要使用字符串数组,我的本意是:
    在sql数据库中,记录数组名的字段是个char,所以读出来肯定是个字符串,我需要用这个串中记录的字符,找到一个和它相同字符的一个数组。
    就是说,如果sql中记录了salary这个串,并且额外字段记录了一个数值2,则在程序中需要找到double[] salary这个在系统中定义过的数组,并且要获得double类型数组salary[2]的值。系统中定义了很多double型的数组,我在sql数据库中会记录好多与数组名相同的字符串及系统中该数组的某个下标,以及各个数组各值间的运算关系符号。
    我需要让这些数组之间的数据进行运算,所以我必须要想办法获得以sql中记录的字符串命名的数组。
    程序执行过程大概如下:
    while(所需要的sql中数据没有读完)
    {
      1。读取sql中一条数据;
      2。从这条数据中获取所记录的两个数组名称,和数组的下标以及运算关系;//如salary,1,age,2,+ 五个信息;
      3。通过前四个信息,找到系统中定义过的数据类型的数组,如double[] salary, double[] age等;获取salary[1],age[2]的值,如salary[1]=220.5,age[2]=27.0;并进行运算:
    salary[1]+age[2]=220.5+27.0=247.5;
    }
    我就是不太清楚第3步怎么具体的实现,用class,struct或者emnu怎么实现啊?
    我在WebApplication中开发的,能否给一个详尽的带注释说明的例子。
    再次感谢大家了。