读书人

再问动态变量或者反射的有关问题

发布时间: 2012-01-03 22:16:06 作者: rapoo

再问动态变量或者反射的问题
原来问过一次,我问得乱,结果也看不出各位回答的是哪个问题了。。。。。散分,结贴,重新再问一下

问题再说一下:
public class a
{
public static string aa[3]={"0","1","2"};
}
public class b
{
public static string bb[5]={"3","4","5","6","7"};
}

请问如何在别的地方动态获得整个数组b的值呢?其中类型已经确定。

public class xx
{
public void xxx(string xxxx)
{
string[] c;
c = 这时要根据参数xxxx的值来确定c指向a.aa还是b.bb,就是不知道这个地方的代码怎么写。

//这里jeremyyang824的写法是
//c = ((string[])_a.GetType().GetField("b", BindingFlags.NonPublic ¦BindingFlags.Instance).GetValue(_a));
//petshop的写法好像应该是PetShop.DALFactory里的
//public static PetShop.IDAL.ICategory CreateCategory() {
// string className = path + ".Category";
// return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
//}
}

}

项目中只有一个class a,只有一个class xx。请不必考虑重复的问题。

有的朋友说
public class xx :a
但是这样能得到aa的值,不能得到b.bb的值。这样应该是不可行的吧。

[解决办法]
不知搂主是不是这个意思:

C# code
using System;using System.Collections.Generic;using System.Text;using System.Reflection;namespace ReflectionTest{    class Program    {        static void Main(string[] args)        {            xx.xxx("ReflectionTest.a");            show();            Console.WriteLine("-----");            xx.xxx("ReflectionTest.b");            show();            Console.ReadLine();        }        private static void show()        {            foreach (string x in xx.c)            {                Console.WriteLine(x);            }        }    }    public class a     {        public static string[] ss = new string[] { "0", "1", "2" };    }    public class b    {        public static string[] ss = new string[] { "3", "4", "5", "6", "7" };    }    public class xx    {        public static string[] c;        public static void xxx(string xxxx)        {            FieldInfo t = Assembly.Load("ReflectionTest").GetType(xxxx).GetField("ss");            c = (string[])t.GetValue(null);                    }    }} 

读书人网 >C#

热点推荐