请教个 C# 的问题
public static C[] link_insert<P, C>(ref P parent, ref int count, string css)where P : Control
where C : Control, new()
{
C[] childs = new C[count];
for (int j = 0; j < count; j++)
{
childs[j] = new C();
childs[j].CssClass = css + j.ToString();
parent.Controls.Add(childs[j]);
}
return childs;
}
这段代码,错在哪里?
如果把C的约束换成其他具体控件 就可以调用cssclass属性
public static C[] link_insert<P, C>(ref P parent, ref int count, string css)
where P : Control
where C : Button, new()
{
C[] childs = new C[count];
for (int j = 0; j < count; j++)
{
childs[j] = new C();
childs[j].CssClass = css + j.ToString();
parent.Controls.Add(childs[j]);
}
return childs;
}
比如这样写 就可以调用cssclass属性,不过这样写就限制了我传入的泛型c的类型