想调用反射动态申请一个数组:
System.Activator.CreateInstance(Type.GetType("System.String[]"));
但总是报无构造函数,
有什么办法达到动态申明某一类型的变量吗?

解决方案 »

  1.   

    string[] test = new string[长度];
      

  2.   

    string[] s = System.Activator.CreateInstance(Type.GetType("System.String[]"), new object[] { 1 }) as string[];1表示长度
      

  3.   

    Activator.CreateInstance(elementType, new object[]{参数});

    Activator.CreateInstance(Type.GetType("System.String[]"), new object[] {2})
      

  4.   

    谢谢,合位
    ctivator.CreateInstance(Type.GetType("System.String[]"), new object[] {2})
    可解决
    之所以要动态申明,是因为想实现自动建某对象,然后再动态设置属性值但如果我想动态申明string这种类型变量,又该如何做呢?
      

  5.   

    //默认的方式,用一个现有字符数组创建
    string s = System.Activator.CreateInstance(Type.GetType("System.String"), new object[] { "初始化的字符串,可以为空".ToCharArray() }) as string;
    //使用一个字符+一个重复长度创建
    string s1 = System.Activator.CreateInstance(Type.GetType("System.String"), new object[] { 'a',50 }) as string;
      

  6.   

    根据CreateInstance(elementType, new object[]{参数});
    变化就行了
      

  7.   

    比如说你要申请一个位置长度的string数组。你可以通过泛型。如list<string> list=new List<string>();
    你把所有要的数据先放到list里,然后通过list.ToArray()他的返回值就是一个String数组了。