首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取byref字符串引用的内存地址

获取byref字符串引用的内存地址
EN

Stack Overflow用户
提问于 2011-03-16 00:00:30
回答 2查看 866关注 0票数 1

我正在尝试更有效的方法来生成表单。我的实验探讨了直接通过类引用链接字符串和其他类型的可能性,并使用它来更新原始值,而不需要任何强类型代码。

我一直使用GCHandle.AddrOfPinnedObject来获取字符串的内存地址,但它给出了字符串数据的内存地址,而不是我需要更改以允许使用此方法的字符串类/引用。

我知道字符串是不可变的,也是不能改变的(实际上,你可以,但不推荐这样做),但我不想改变实际的字符串,而是改变对它的引用( string对象)。

有没有其他方法来获取string对象引用的地址?

一些示例代码:

代码语言:javascript
运行
复制
Private oTestperson As New Person

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' Create the form
    AddText("Name", oTestperson.Name)
    AddText("Phone", oTestperson.Phone)
    AddText("Address", oTestperson.Address)

End Sub

Public Sub AddText(ByVal Title As String, ByRef sValue As String)

    Dim oGCHandle As System.Runtime.InteropServices.GCHandle
    Dim oInputItem As New InputItem

    ' This does not do the job, as it only returns the address of the string data, not the reference to the string class
    oGCHandle = System.Runtime.InteropServices.GCHandle.Alloc(CType(sValue, Object), System.Runtime.InteropServices.GCHandleType.Pinned)
    oInputItem.Pointer = oGCHandle.AddrOfPinnedObject()
    oGCHandle.Free()

    ' Store data
    oInputItem.ID = GetID()
    oInputItem.Type = InputTypes.Text
    oInputItem.BaseValue = sValue
    If Not Request.Form(oInputItem.ID) Then oInputItem.Value = Request.Form(oInputItem.ID)
    If oInputItem.Value Is Nothing Then oInputItem.Value = sValue

    ' Save in collection
    InputItems.Add(oInputItem)

End Sub

Private Sub linkbuttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkbuttonSave.Click
    Save()
End Sub

Public Sub Save()

    Dim oTest As New Person
    Dim oGCHandle As System.Runtime.InteropServices.GCHandle
    Dim NewStrPointer As IntPtr

    ' Check if something has been changed, in that case update the origional value
    For Each oInputItem As InputItem In InputItems
        If oInputItem.Value <> oInputItem.BaseValue Then
            oGCHandle = GCHandle.Alloc(oInputItem.Value, GCHandleType.Pinned)
            NewStrPointer = oGCHandle.AddrOfPinnedObject()
            oGCHandle.Free()

            ' This fails as oInputItem.Pointer is the address of the string data, not the string class
            Marshal.WriteInt32(oInputItem.Pointer.ToInt32, NewStrPointer.ToInt32)

        End If
    Next

End Sub

Private InputItems As New ArrayList

Private Class Person
    Public Property Name As String
    Public Property Phone As String
    Public Property Address As String
End Class

Public Enum InputTypes As Integer
    Text = 0
End Enum

Public Class InputItem

    Public Property ID As String
    Public Property BaseValue As Object
    Public Property Value As Object
    Public Property Type As InputTypes = 0
    Public Property Pointer As IntPtr

End Class
EN

回答 2

Stack Overflow用户

发布于 2011-03-16 01:10:21

被认可的方法是使用反射,这将更加容易,并且CLR将能够确保您不会到处乱扔对象。

票数 0
EN

Stack Overflow用户

发布于 2011-03-16 01:33:59

您的方法有点C++/VB6/90年代后期,但10的努力!:)

阅读用于iterating classesSystem.Reflection,用于将(数据)类绑定到图形用户界面的DataBinding,以及用于读/写类的Serialization。使用WPF将进一步简化任务,使其接近于虚无(注: WPF可以通过ElementHost在WinForms中使用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5314453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档