propertyinfo.getvalue" Object与目标类型不匹配“ [英] propertyinfo.getvalue "Object Does Not match target type"

查看:424
本文介绍了propertyinfo.getvalue" Object与目标类型不匹配“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PropertyInfo中的GetValue但继续获取对象与目标类型不匹配。我尝试过使用一个字符串变量,它说我没有在其他帮助中看到我的识别。



  For  每个 logentry  As  LogEntry  _lr 
对于 每个 _pi logentry。 GetType .GetProperties()

Dim _val = _pi.GetValue(_pi。 GetType (), Nothing
控制台。 WriteLine( String .Format( {0}:{ 1},_ pi.Name,_val))
下一步
下一步

解决方案

您正在尝试使用此方法: http://msdn.microsoft.com/en-us/library/b05d59ty%28v=vs.110%29.aspx [ ^ ]。



你做错了,参数错误。第一个参数应该是对象的某个实例。获得 _pi.GetType 是完全没有意义的,这种类型是已知的, PropertyInfo 。第二个参数仅对索引属性非空。



您只需要思考一下。你编写了代码而没有任何理解你需要从哪个对象得到什么对象。这很容易,只要想一想。我不知道获取属性值的对象在哪里,因为你从它的类型开始,你可能有也可能没有它。第一个参数可以为null,非常自然,这意味着这是一个静态属性。您可以使用适当的 BindingFlags 从非静态中排除静态属性。



-SA

I'm trying to use GetValue from PropertyInfo but keep getting "Object Does Not match target type". I have tried using a string variable and it says Me is not recongized which I saw in other help.

For Each logentry As LogEntry In _lr
            For Each _pi In logentry.GetType.GetProperties()

                Dim _val = _pi.GetValue(_pi.GetType(), Nothing)
                Console.WriteLine(String.Format("{0}:  {1}", _pi.Name, _val))
            Next
        Next

解决方案

You are trying to use this method: http://msdn.microsoft.com/en-us/library/b05d59ty%28v=vs.110%29.aspx[^].

You do it completely wrong, with wrong parameters. First parameter should be some instance of the object. It's totally pointless to get _pi.GetType, this type is known, PropertyInfo. Second parameter will be non-null only for indexed properties.

All you need is to think a bit. You wrote you code without any understanding of what object would you need to get and from what object. This is easy, just think about it. I don't know where is the object to get a property value, because you started with its type, you may or may not have it. The first parameter can be null, quite naturally, that means this is a static property. You can sort out static properties from non-static using appropriate BindingFlags.

—SA


这篇关于propertyinfo.getvalue" Object与目标类型不匹配“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆