The following call will fail because compiler expects method SetAll(PropertyInfo, int)
.
var infos = GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
var setters = infos.Select(SetAll); // no overload matches delegate.
private Action SetAll(PropertyInfo info, object obj) => () => info.SetValue(this, obj);
So that means compiler can't use this overload any way. it can't cast int
to object
.
Having this in mind, Why the following call is ambiguous?
var b = infos.Select(SetAll); // ambiguous between Select<PropertyInfo, int, Action>
// and Select<PropertyInfo, Action>
private Action SetAll(PropertyInfo info, object obj) => () => info.SetValue(this, obj);
private Action SetAll(PropertyInfo info) => () => info.SetValue(this, null);
If compiler can't use overload with object any way then why its struggling here?
Here is the actual code I'm having. I can deal with this problem easily but im just being curios.
var infos = GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (useDefaultsOnReset)
{
var defaults = infos.Select(GetAll);
_resetters = infos.Zip(defaults, SetAll).ToArray();
}
else
{
_resetters = infos.Select(SetAll).ToArray(); // error
}
private object GetAll(PropertyInfo info) => info.GetValue(this);
private Action SetAll(PropertyInfo info, object obj) => () => info.SetValue(this, obj);
private Action SetAll(PropertyInfo info) => () => info.SetValue(this, null);