日期:2014-05-17  浏览次数:20773 次

C# 泛型问题
protected void InitDepartment(DAL.Department dept, TreeNode root)
  {
  DAL.Department[] subDepartments = dept.GetEnabledSubDepartments();

  foreach (DAL.Department var in subDepartments)
  {
  TreeNode node = new TreeNode(var.Name, 1, 2);
  node.Tag = var;

  this.InitDepartment(var, node);

  root.Nodes.Add(node);
  }
  }

请问这个用泛型怎么写啊 dept是个类
谢谢大大们 请赐教

------解决方案--------------------
C# code
interface IClassHaveName
{
    string Name { get; set; }
}

protected void InitDepartment<T>(T dept, TreeNode root) where T : IClassHaveName
  {
  T[] subDepartments = dept.GetEnabledSubDepartments();

  foreach (T item in subDepartments)
  {
  TreeNode node = new TreeNode(item.Name, 1, 2);
  node.Tag = item;

  this.InitDepartment(item, node);

  root.Nodes.Add(node);
  }
  }
 
class DAL.Department : IClassHaveName
{
    ...
}