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

发布于 2024-09-16 07:10:17 字数 543 浏览 4 评论 0原文

好吧,我正在学习泛型,我正在尝试让这个东西运行,但它一直告诉我同样的错误。这是代码:

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;
}

Ok, so I'm learning about generics and I'm trying to make this thing run, but its keep saying me the same error. Here's the code:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-09-23 07:10:17

发生这种情况是因为 default(T) 返回 null,因为 T 表示引用类型。引用类型的默认值为null

你可以将你的方法更改为:

public static T Test<T>(MyClass myClass) where T : MyClass2, new()
{
    var result = new T();
    ...
}

然后它就会按照你想要的方式工作。当然,MyClass2 及其后代现在必须有一个无参数构造函数。

This happens because default(T) returns null because T represents a reference type. Default values for reference types are null.

You could change your method to:

public static T Test<T>(MyClass myClass) where T : MyClass2, new()
{
    var result = new T();
    ...
}

and then it will work as you want it to. Of course, MyClass2 and its descendants must have a parameterless constructor now.

美人迟暮 2024-09-23 07:10:17

这里的问题是 T 派生自 MyClass,因此是一个引用类型。因此表达式 default(T) 将返回值 null。以下对 SetValue 的调用操作的是 null 值,但该属性是实例属性,因此您会收到指定的消息。

您需要执行以下操作之一

  1. T 的真实实例传递给 Test 函数以设置属性值
  2. 仅设置类型的静态属性

The problem here is that T derives from MyClass and is hence a reference type. So the expression default(T) will return the value null. The following call to SetValue is operating an a null value but the property is an instance property hence you get the specified message.

You'll need to do one of the following

  1. Pass a real instance of T to the Test function to set the property values on
  2. Only set the static properties on the type
提赋 2024-09-23 07:10:17

而不是

propertyInfo.SetValue(result, fromProperty, null);

尝试:

foreach (var propertyInfo in toProperties)  
{ 
    propertyInfo.GetSetMethod().Invoke(MyClass2, new object[] 
    { 
        MyClass.GetType().GetProperty(propertyInfo.Name).
        GetGetMethod().Invoke(MyClass, null)
    });
}

Instead of

propertyInfo.SetValue(result, fromProperty, null);

try:

foreach (var propertyInfo in toProperties)  
{ 
    propertyInfo.GetSetMethod().Invoke(MyClass2, new object[] 
    { 
        MyClass.GetType().GetProperty(propertyInfo.Name).
        GetGetMethod().Invoke(MyClass, null)
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文