在.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() {...}求高人解答。
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() {...}求高人解答。
get {
return new T(index); // <-- 这一步该如何编写?
}
}
这步错了 应该是: public T this[int index] {
get {
return new T(items[index]); // <-- 这一步该如何编写?
}
}
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)
{}
}
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;
}
}