无法获取PropertyInfo SetValue以用于结构 [英] Cannot get PropertyInfo SetValue to Work for a Structure

查看:46
本文介绍了无法获取PropertyInfo SetValue以用于结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从PropertyInfo的MSDN帮助中调整了以下代码

SetValue。在原始代码中,结构MyStructure被定义为

类MyProperty,并且它按预期工作。我已经注释了

类Mypropertyinfo的微小变化。


但是,当使用结构时,第二次调用GetValue会返回

"默认标题。谁能告诉我为什么,以及如何使这项工作?


< code>

进口系统

进口系统.Reflection


公共结构MyStructure


私人myCaption字符串


公共财产标题()作为String

获取

返回myCaption

结束获取

设置(ByVal Value As String)

如果myCaption<>价值然后

myCaption =价值

结束如果

结束套件

结束物业

结束结构


类Mypropertyinfo


公共共享功能Main()为整数


控制台.WriteLine(Reflection.PropertyInfo)


''''昏暗的af新的MyProperty''原版

昏暗af作为MyStructure


af.Caption ="默认标题"


''获取类型和PropertyInfo

Dim MyType As Type = af .GetType

Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty(" Caption")


''获取并显示GetValue方法

Console.WriteLine(" GetValue - "& Mypropertyinfo.GetValue(af,

Nothing).ToString())


''使用更改标题的SetValue方法

Mypropertyinfo.SetValue(af,此标题已更改。,

Nothin g)


''再次获取标题并显示它

Console.WriteLine(" GetValue - " &安培; Mypropertyinfo.GetValue(af,

没什么).ToString())

Console.ReadLine()


返回0


结束功能

结束课程

< / code>


TIA


Charles

I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is also a minor change in
class Mypropertyinfo, which I have commented out.

When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?

<code>
Imports System
Imports System.Reflection

Public Structure MyStructure

Private myCaption As String

Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> Value Then
myCaption = Value
End If
End Set
End Property
End Structure

Class Mypropertyinfo

Public Shared Function Main() As Integer

Console.WriteLine("Reflection.PropertyInfo")

''''Dim af As New MyProperty '' original
Dim af As MyStructure

af.Caption = "Default caption"

'' Get the type and PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty("Caption")

'' Get and display the GetValue method
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

'' Use the SetValue method to change the caption
Mypropertyinfo.SetValue(af, "This caption has been changed.",
Nothing)

'' Get the caption again and display it
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

Console.ReadLine()

Return 0

End Function
End Class
</code>

TIA

Charles

推荐答案

这是因为结构是一种值类型,同时包含SetValue和GetValue

expect对象导致参数被装箱。因此,GetValue和

SetValue正在处理盒装对象而不是实际结构。我相信这种技术不会对结构起作用 - 虽然我可能错了。


希望有帮助......

Imran。


" Charles Law" < BL *** @ nowhere.com>在消息中写道

news:et ************* @ TK2MSFTNGP11.phx.gbl ...
This is because a structure is a value type and both SetValue and GetValue
expect Objects causing the arguments to boxed. As a result, the GetValue and
SetValue are working the boxed object rather than on the actual structure. I
believe this technique won''t work on structures - I could be wrong though.

hope that helps..
Imran.

"Charles Law" <bl***@nowhere.com> wrote in message
news:et*************@TK2MSFTNGP11.phx.gbl...
我改编了以下代码来自PropertyInfo的MSDN帮助
SetValue。在原始代码中,结构MyStructure被定义为一个类MyProperty,它按预期工作。在Mypropertyinfo类中还有一个小的变化
,我已经注释掉了。

当使用结构时,第二次调用GetValue会返回
默认标题。 。任何人都可以告诉我为什么,以及我如何使这项工作?

< code>
进口系统
进口System.Reflection

公共结构MyStructure

私人myCaption为字符串

公共属性标题()为字符串
获取
返回myCaption
结束获取
设置(ByVal Value As String)
如果myCaption<>价值然后
myCaption =价值
结束如果
结束设置
结束属性
结束结构

类Mypropertyinfo
公共共享功能Main()作为整数

Console.WriteLine(" Reflection.PropertyInfo")

''''昏暗af作为新MyProperty''原创
af.Caption ="默认标题

''获取类型和PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty(Caption)

''获取并显示GetValue方法
Console.WriteLine(" GetValue - "& Mypropertyinfo.GetValue(af,
Nothing).ToString())
''使用SetValue方法更改标题
Mypropertyinfo.SetValue(af,此标题有陈ged。",
没什么。

''再次获取标题并显示它
Console.WriteLine(" GetValue - " &安培; Mypropertyinfo.GetValue(af,
没什么).ToString())

Console.ReadLine()

返回0

结束功能
结束班
< / code>

TIA

I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is also a minor change in class Mypropertyinfo, which I have commented out.

When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?

<code>
Imports System
Imports System.Reflection

Public Structure MyStructure

Private myCaption As String

Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> Value Then
myCaption = Value
End If
End Set
End Property
End Structure

Class Mypropertyinfo

Public Shared Function Main() As Integer

Console.WriteLine("Reflection.PropertyInfo")

''''Dim af As New MyProperty '' original
Dim af As MyStructure

af.Caption = "Default caption"

'' Get the type and PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo = MyType.GetProperty("Caption")

'' Get and display the GetValue method
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

'' Use the SetValue method to change the caption
Mypropertyinfo.SetValue(af, "This caption has been changed.",
Nothing)

'' Get the caption again and display it
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

Console.ReadLine()

Return 0

End Function
End Class
</code>

TIA

Charles



您好伊姆兰


感谢您的快速回复。它确实有道理,但遗憾的是,如果它是真的,我有点卡住了,因为我需要能够使用这个或类似的技术来设置
物业价值。


您或其他任何人知道某种解决方法或其他方式吗?


Charles

" Imran Koradia" <无**** @ microsoft.com>在消息中写道

新闻:OE ************** @ TK2MSFTNGP10.phx.gbl ...
Hi Imran

Thanks for a quick response. It does make sense, but sadly, if it is true, I
am a bit stuck, as I need to be able to use this or a similar technique to
set property values.

Do you, or anyone else know of a workaround, or another way of doing it?

Charles
"Imran Koradia" <no****@microsoft.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
这是因为一个结构是一个值类型,SetValue和GetValue都希望对象导致参数被装箱。因此,GetValue
和SetValue正在处理盒装对象而不是实际结构。
我相信这种技术不适用于结构 - 我可以虽然错了。

希望有所帮助..
伊姆兰。

查尔斯劳 < BL *** @ nowhere.com>在消息中写道
新闻:et ************* @ TK2MSFTNGP11.phx.gbl ...
This is because a structure is a value type and both SetValue and GetValue
expect Objects causing the arguments to boxed. As a result, the GetValue
and
SetValue are working the boxed object rather than on the actual structure.
I
believe this technique won''t work on structures - I could be wrong though.

hope that helps..
Imran.

"Charles Law" <bl***@nowhere.com> wrote in message
news:et*************@TK2MSFTNGP11.phx.gbl...
我已经从MSDN帮助改编了以下代码对于PropertyInfo
SetValue。在原始代码中,结构MyStructure被定义为一个类MyProperty,它按预期工作。在
I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is also a minor change
类Mypropertyinfo中也有一个小的改动


in


我已经注释掉了。

当使用结构时,第二次调用GetValue返回
默认标题。任何人都可以告诉我为什么,以及我如何使这项工作?

< code>
进口系统
进口System.Reflection

公共结构MyStructure

私人myCaption为字符串

公共属性标题()为字符串
获取
返回myCaption
结束获取
设置(ByVal Value As String)
如果myCaption<>价值然后
myCaption =价值
结束如果
结束设置
结束属性
结束结构

类Mypropertyinfo
公共共享功能Main()作为整数

Console.WriteLine(" Reflection.PropertyInfo")

''''昏暗af作为新MyProperty''原创
af.Caption ="默认标题

''获取类型和PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo =
MyType.GetProperty(Caption)

''获取并显示GetValue方法
Console.WriteLine(" GetValue - && Mypropertyinfo.GetValue(af,
Nothing).ToString())
''使用SetValue方法更改标题
Mypropertyinfo.SetValue(af,& ;这个标题有蜜蜂n改了。,没什么。

''再次获取标题并显示它
Console.WriteLine(" GetValue - " &安培; Mypropertyinfo.GetValue(af,
没什么).ToString())

Console.ReadLine()

返回0

结束功能
结束班
< / code>

TIA

class Mypropertyinfo, which I have commented out.

When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?

<code>
Imports System
Imports System.Reflection

Public Structure MyStructure

Private myCaption As String

Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> Value Then
myCaption = Value
End If
End Set
End Property
End Structure

Class Mypropertyinfo

Public Shared Function Main() As Integer

Console.WriteLine("Reflection.PropertyInfo")

''''Dim af As New MyProperty '' original
Dim af As MyStructure

af.Caption = "Default caption"

'' Get the type and PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo =
MyType.GetProperty("Caption")

'' Get and display the GetValue method
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

'' Use the SetValue method to change the caption
Mypropertyinfo.SetValue(af, "This caption has been changed.",
Nothing)

'' Get the caption again and display it
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

Console.ReadLine()

Return 0

End Function
End Class
</code>

TIA

Charles




Charles,

最简单的方法是不使用结构。


否则我相信你需要使用C#,因为VB.NET添加了对

RuntimeHelpers.GetObjectValue的调用每当你尝试用

对象变量做某事时。


问题使用Structure是一个结构是一个值类型,因此调用GetObjectValue,其中包装了一个值类型。 GetObjectValue

用于确保当您在对象变量中指定值类型

另一个对象变量时,会生成该值的副本,而不是对<的引用br />
相同的实例。这与类型化值类型的工作方式保持一致。


您可以使用ILDASM.EXE查看VB.NET何时添加调用

RuntimeHelpers.GetObjectValue。


我建议您尝试使用CallByName,但是它似乎首先调用

RuntimeHelpers.GetObjectValue ...


希望这有帮助

Jay

" Charles Law" < BL *** @ nowhere.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Charles,
The "easiest" way would be not to use a Structure.

Otherwise I believe you will need to use C#, as VB.NET adds calls to a
RuntimeHelpers.GetObjectValue whenever you try to do something with an
object variable.

The "problem" with using a Structure is that a Structure is a value type,
hence the call to GetObjectValue which "boxes a value type". GetObjectValue
is used to ensure that when you assign a value type in an object variable to
another object variable a copy of the value is made, not a reference to the
same instance. Which is in keeping with how typed value types work.

You can use ILDASM.EXE to see when VB.NET adds calls to
RuntimeHelpers.GetObjectValue.

I would suggest you try CallByName, however it appears to call
RuntimeHelpers.GetObjectValue first also...

Hope this helps
Jay
"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi Imran

感谢您的快速回复。它确实有意义,但遗憾的是,如果它是真的,
我有点卡住了,因为我需要能够使用这个或类似的技术来设置属性值。
<你或其他任何人都知道一种解决方法或其他方式吗?

Charles

Imran Koradia <无**** @ microsoft.com>在消息中写道
新闻:OE ************** @ TK2MSFTNGP10.phx.gbl ...
Hi Imran

Thanks for a quick response. It does make sense, but sadly, if it is true,
I am a bit stuck, as I need to be able to use this or a similar technique
to set property values.

Do you, or anyone else know of a workaround, or another way of doing it?

Charles
"Imran Koradia" <no****@microsoft.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
这是因为结构是值类型并且SetValue和
GetValue
期望对象导致参数被装箱。因此,GetValue
和SetValue正在处理盒装对象而不是实际的结构。我相信这种技术不会对结构起作用 - 我可能错了。

希望有所帮助..
Imran。
<查尔斯·劳斯(Charles Law) < BL *** @ nowhere.com>在消息中写道
新闻:et ************* @ TK2MSFTNGP11.phx.gbl ...
This is because a structure is a value type and both SetValue and
GetValue
expect Objects causing the arguments to boxed. As a result, the GetValue
and
SetValue are working the boxed object rather than on the actual
structure. I
believe this technique won''t work on structures - I could be wrong
though.

hope that helps..
Imran.

"Charles Law" <bl***@nowhere.com> wrote in message
news:et*************@TK2MSFTNGP11.phx.gbl...
我已经从MSDN帮助改编了以下代码对于PropertyInfo
SetValue。在原始代码中,结构MyStructure被定义为类MyProperty,它按预期工作。在
I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as
a
class MyProperty, and it works as expected. There is also a minor change
类Mypropertyinfo中也有一个小的改动


in


我已经注释掉了。

当使用结构时,第二次调用GetValue返回
默认标题。任何人都可以告诉我为什么,以及我如何使这项工作?

< code>
进口系统
进口System.Reflection

公共结构MyStructure

私人myCaption为字符串

公共属性标题()为字符串
获取
返回myCaption
结束获取
设置(ByVal Value As String)
如果myCaption<>价值然后
myCaption =价值
结束如果
结束设置
结束属性
结束结构

类Mypropertyinfo
公共共享功能Main()作为整数

Console.WriteLine(" Reflection.PropertyInfo")

''''昏暗af作为新MyProperty''原创
af.Caption ="默认标题

''获取类型和PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo =
MyType.GetProperty(Caption)

''获取并显示GetValue方法
Console.WriteLine(" GetValue - && Mypropertyinfo.GetValue(af,
Nothing).ToString())
''使用SetValue方法更改标题
Mypropertyinfo.SetValue(af,& ;这个标题有蜜蜂n改了。,没什么。

''再次获取标题并显示它
Console.WriteLine(" GetValue - " &安培; Mypropertyinfo.GetValue(af,
没什么).ToString())

Console.ReadLine()

返回0

结束功能
结束班
< / code>

TIA

class Mypropertyinfo, which I have commented out.

When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?

<code>
Imports System
Imports System.Reflection

Public Structure MyStructure

Private myCaption As String

Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> Value Then
myCaption = Value
End If
End Set
End Property
End Structure

Class Mypropertyinfo

Public Shared Function Main() As Integer

Console.WriteLine("Reflection.PropertyInfo")

''''Dim af As New MyProperty '' original
Dim af As MyStructure

af.Caption = "Default caption"

'' Get the type and PropertyInfo
Dim MyType As Type = af.GetType
Dim Mypropertyinfo As PropertyInfo =
MyType.GetProperty("Caption")

'' Get and display the GetValue method
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

'' Use the SetValue method to change the caption
Mypropertyinfo.SetValue(af, "This caption has been changed.",
Nothing)

'' Get the caption again and display it
Console.WriteLine("GetValue - " & Mypropertyinfo.GetValue(af,
Nothing).ToString())

Console.ReadLine()

Return 0

End Function
End Class
</code>

TIA

Charles





这篇关于无法获取PropertyInfo SetValue以用于结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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