我尝试在此博客文章中发布此问题的 Jon Skeet解决方案,以使用委托的非反射方法替换该方法.SetValue
从解决方案的差异博客文章就是SetValue就是void,我也得到了The type 'System.Void' may not be used as a type argument.在该行例外MethodInfo miConstructedHelper = miGenericHelper.MakeGenericMethod(typeof(G), pMethod.GetParameters()[0].ParameterType, pMethod.ReturnType);.
这是我的实现MagicMethod:
public class Instantiator<T> where T : new()
{
private T instance;
private IDictionary<string, PropertyInfo> properties;
private Func<PropertyInfo, object, object> _fncSetValue;
public Instantiator()
{
Type type = typeof(T);
properties = type.GetProperties().GroupBy(p => p.Name).ToDictionary(g => g.Key, g => g.ToList().First());
MethodInfo miSetValue = typeof(PropertyInfo).GetMethod("SetValue", new Type[] { …Run Code Online (Sandbox Code Playgroud)