I have a list of objects that I'm filling it in as follows:
var props = type.GetRuntimeProperties();
var obj = new T();
for (int i = 0; i < readerCache.Count; i++)
{
var prop = props.Single(x => x.GetColumnName().ToLower() == readerCache.ElementAt(i).Item1.ToLower());
if (prop.GetCustomAttribute<Column>().Type == ColumnType.FK)
{
var method = typeof(SelectExtension).GetMethod("SelectSingle");
var generic = method.MakeGenericMethod(prop.PropertyType);
prop.SetValue(obj, generic.Invoke(null, new object[] { dBManager, $"WHERE id = '{readerCache.ElementAt(i).Item2}'" }));
}
else
{
prop.SetValue(obj, readerCache.ElementAt(i).Item2);
}
if ((i + 1) % props.Count() == 0)
{
objList.Add(obj);
}
}
When I'm debugging, objects are being filled in correctly, but when the list finishes filling in, all elements are the same.
My suspicion is that you are passing the object by reference, not by value.
Why does this happen? How to fix?