I'm debugging someone else's source code, and I'm not really sure how it works, so am a bit stuck.
This code:
List<string> source = new List<string>();
for (int ordinal = 0; ordinal < dbDataReader.FieldCount; ++ordinal)
source.Add(dbDataReader.GetName(ordinal));
var list = source.Select(n => new {
n = n,
prop = props.FirstOrDefault<DB.PropInfo<T>>((Func<DB.PropInfo<T>, bool>)(p => string.Equals(p.Name, n, StringComparison.Ordinal)))
?? props.FirstOrDefault<DB.PropInfo<T>>((Func<DB.PropInfo<T>, bool>)(p => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase)))
}
)
.Select(param0 => new { Name = param0.n, Property = param0.prop })
.ToList();
while (dbDataReader.Read())
{
T instance = Activator.CreateInstance<T>();
foreach (var data in list)
data.Property.Setter(instance, Convert.ChangeType(dbDataReader[data.Name], data.Property.Type)); // ERROR HERE
objList.Add(instance);
}
throws this exception:
ArgumentException: Static property requires null instance, non-static property requires non-null instance. Parameter name: property
The variables instance
and data
all have what look like valid values.
What is wrong here, please?
What about a quick dirty fix?
foreach (var data in list)
{
try
{
data.Property.Setter(instance, Convert.ChangeType(dbDataReader[data.Name], data.Property.Type));
}
catch(ArgumentException e)
{
data.Property.Setter(default(T), Convert.ChangeType(dbDataReader[data.Name], data.Property.Type));
}
}