I have several data object classes which all have a member with the same name, I want to create a template class which keeps a List of a specific class as well as some additional information. In the constructor for this template, I want to be be able to iterate through the list and set a member in all of the items.
In C++ this would work because of "duck" typing in templates. How would you do this in C#, (or can you).
Example:
public class Thing1
{
public string Name {get; set;}
public string Id {get; set;}
public string GroupId {get; set;}
}
public class Thing2
{
public string Size {get; set;}
public string Id {get; set;}
public string GroupId {get; set;}
}
public class GroupOfThings<T>
{
public GroupOfThings(List<T> things, string groupID)
{
GroupID = groupID;
Items = things;
// This is the code that I would like to be able to have
// foreach(var i in Items)
// {
// i.GroupId = groupID;
// }
}
public List<T> Items;
public string GroupId;
}