in src/Serialization/ReflectionExtentions.cs [70:122]
public static MethodDelegate CreateMethod(this MethodInfo methodInfo, bool isStatic)
{
var parameters = methodInfo.GetParameters();
string delegateName;
Type[] genericTypes;
if (parameters.Length == 0)
{
if (methodInfo.ReturnType == typeof(void))
{
delegateName = "CreateMethodDelegate0";
genericTypes = new[] { methodInfo.DeclaringType };
}
else
{
delegateName = "CreateMethodDelegate1";
genericTypes = new[] { methodInfo.DeclaringType, methodInfo.ReturnType };
}
}
else if (parameters.Length == 1)
{
if (methodInfo.ReturnType == typeof(void))
{
delegateName = "CreateMethodDelegate3";
genericTypes = new[] { methodInfo.DeclaringType, parameters[0].ParameterType };
}
else
{
delegateName = "CreateMethodDelegate4";
genericTypes = new[] { methodInfo.DeclaringType, parameters[0].ParameterType, methodInfo.ReturnType };
}
}
else if (parameters.Length == 2)
{
// e.g. Dictionary.Add(key, value)
if (methodInfo.ReturnType == typeof(void))
{
delegateName = "CreateMethodDelegate5";
genericTypes = new[] { methodInfo.DeclaringType, parameters[0].ParameterType, parameters[1].ParameterType };
}
else
{
throw new NotSupportedException($"{methodInfo.Name}: 2 parameters with return type.");
}
}
else
{
throw new NotSupportedException($"{methodInfo.Name}: more than 2 parameters.");
}
MethodInfo m1 = typeof(ReflectionExtentions).GetMethod(delegateName, BindingFlags.Static | BindingFlags.NonPublic);
MethodInfo m2 = m1.MakeGenericMethod(genericTypes);
return (MethodDelegate)m2.Invoke(null, new object[] { methodInfo });
}