I have to structs both having e.g. "Id":
public struct User
{
public int Id;
public string Email;
}
public struct Computer
{
public int Id;
public string Name;
}
I'd like to make a template method to rewrite Id from one IList of Computers, Users and such to another.
I've tried below, but VS complains T does not contain a definition for Id:
private static void RewriteIListIds<T>(ref IList<T> pre, IList<T> post)
{
if (post != null && post.Count > 0)
{
Assert.IsTrue(pre != null && pre.Count > 0);
for (int i = 0; i < post.Count; i++)
{
T preElement = pre[i];
T postElement = post[i];
preElement.Id = postElement.Id;
pre[i] = preElement;
}
}
}
EDIT: Interesting ideas but I probably should have mention I'm testing a service which I really don't want and most probably can't really change.
EDIT2:
Just for future references and to be more clear - I've probably made this problem more generic than it should be - User and Computer structs are what a Web Service (currently configured as SOAP) returns in an IList. [DataContract]
and [Data Member]
was removed from above example to make this problem a bit more generic.