I would like to create a dynamic method that populates a dropdownlist with any object based on the parameters. Here's my code:
PopulateDropDownList(ddl, GetList(), typeof(MyClass));
public void PopulateDropDownList(DropDownList ddl, IEnumerable list, Type type)
{
object obj = Activator.CreateInstance(type);
foreach (var item in list)
ddl.Items.Add(new ListItem(((obj)item).Name, ((obj)item).ID.ToString()));
}
Basically, what I want to do is to cast Name
and ID
to a type but I'm getting an error when I compile.
Aucun commentaire:
Enregistrer un commentaire