Code: Select all
public static class ListExtensions
{
public static IList AddRangeByKey(
this IList list, string keyName,
IEnumerable arrayElements) =>
list.AddRange(keyName, arrayElements);
// the other code is omitted for the brevity
}
Code: Select all
public static class UrlHelper
{
public static IEnumerable ToKeyValuePairs(
this object obj)
{
List keyValuePairs = new();
IEnumerable
props = obj.GetType().GetProperties()
.Where(p => p.GetValue(obj, null) != null);
foreach (PropertyInfo p in props)
{
object value = p.GetValue(obj, null);
IEnumerable enumerable = value as IEnumerable;
if (enumerable != null)
{
MethodInfo addRangeDef = typeof(ListExtensions).GetMethods()
.Where(x => x.Name == "AddRangeByKey" && x.IsGenericMethod)
.FirstOrDefault();
Type ienumerable = value.GetType()
.GetInterface("System.Collections.Generic.IEnumerable`1");
if (ienumerable != null)
{
MethodInfo addRange = addRangeDef
.MakeGenericMethod(ienumerable.GetGenericArguments());
addRange.Invoke(null, new object[] { p.Name, value }); // this
// line of code throws an exception 'addRange.Invoke(null, new
// object[] { p.Name, value })' threw an exception of type
// 'System.Reflection.TargetParameterCountException'.
// Message says `Parameter count mismatch`.
// I tried already like this:
// adRangeDef.Invoke(null, new object[] {
// typeof(UrlHelper), p.Name, value });
}
}
}
return keyValuePairs;
}
}
Code: Select all
object queryObject = new SearchUsers
{
Page = 1, PageSize = 10,
Filters = new List{new FilterParameter {
Field = "name",
Val = "Bob" } }
};
IEnumerable foo = queryObject.ToKeyValuePairs();
Code: Select all
'addRange.Invoke(null, new object[] { p.Name, value })' threw
an exception of type
'System.Reflection.TargetParameterCountException'.
Message says `Parameter count mismatch`.`
Code: Select all
addRangeDef.Invoke(null, new object[] { p.Name, value })
Code: Select all
Late bound operations cannot be performed on types or
methods for which ContainsGenericParameters is true.
Mobile version