在.NET 2.0 C# 代码如下:
public class note {
    private string _value;
    
    public note(string entervalue) {
        _value = entervalue;
    }
    public string getvalue {
        get { return _value; }
    }
}public class notes<T> {
    string[] items = new string[2];
    
    items[0] = "1";
    items[1] = "2";    public T this[int index] {
        get {
            return new T(index); // <--   这一步该如何编写?
        }
    }
}在public class notes<T> 加入 where T : new() 这个能够调用默认的构造函数
但是无法加入参数的! public class notes<T> where T : new() {...}求高人解答。

解决方案 »

  1.   

    public T this[int index] {
            get {
                return new T(index); // <--   这一步该如何编写?
            }
        }
    这步错了 应该是:    public T this[int index] {
            get {
                return new T(items[index]); // <--   这一步该如何编写?
            }
        }
      

  2.   

    我没太深入的研究过2.0,不过我感觉这是错误的,因为无法保证T类型一定带有
    T( string str )的构造函数
    public class notes<T> {
        string[] items = new string[2];
        
        items[0] = "1";
        items[1] = "2";    public notes<T> this[int index] {
            get {
                return new notes<T>(index); // <--   这一步该如何编写?
            }
        }
        public notes(string)
        {}
    }
      

  3.   

    或者使用反射来发现t的代参数的构造函数
    public T this[..]
    {
      get{
        Type t = typeof(T);
    Type[] tp = new Type[1];
                tp[0] = typeof( string );
    System.Reflection.ConstructorInfo ci = t.GetConstructor( tp );
    object[] op = new object[1];
    op[0] = str[nIndex ];
    T to = (T)ci.Invoke(op );  
     return to;
     }
    }
      

  4.   

    OK ! 非常感谢hdt(倦怠) !已经解决!