小编Vic*_*Gil的帖子

非静态方法需要PropertyInfo.SetValue中的目标

好的,所以我正在学习泛型,我正试图让这个东西运行,但它一直在说我同样的错误.这是代码:

public static T Test<T>(MyClass myClass) where T : MyClass2
{
    var result = default(T);
    var resultType = typeof(T);
    var fromClass = myClass.GetType();
    var toProperties = resultType.GetProperties();

    foreach (var propertyInfo in toProperties)
    {
        var fromProperty = fromClass.GetProperty(propertyInfo.Name);
        if (fromProperty != null)
            propertyInfo.SetValue(result, fromProperty, null );
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

c# generics propertyinfo setvalue visual-studio

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

generics ×1

propertyinfo ×1

setvalue ×1

visual-studio ×1