反射Ctype对象的结构 [英] Reflection Ctype object to structure

查看:53
本文介绍了反射Ctype对象的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我试试这个有点不同也许我可以让别人咬...... :-)

如果直接调用代码将会起作用 - 但如果用反射调用会失败吗?

任何想法为什么会这样 - 再次查看数据视图中的调试器,无论你如何调用它,数据看起来都是相同的..但如果使用反射则无法执行

TEST_COPY是我的结构。

我想念的是什么?它必须是简单的....



Let me try this a bit different maybe I can get somebody to bite ....:-)
Below Code will work if called direct - but will fail if called with reflection ??
Any Ideas why that would be - again looking at the debugger in the data view, the data looks the "same" regardless on how you call it ..but fails to execute if you use reflection
TEST_COPY is my structure.
What am I Missing ???? It has to be something simple ....

<br />
 Public Sub DLL_SUB(ByVal Par As Object)<br />
        Dim x As Integer<br />
        For x = 0 To NEW_STRUCTURE.Length - 1<br />
            ' The following code will fail<br />
            NEW_STRUCTURE(x) = CType(Par(x), TEST_COPY)<br />
<br />
        Next<br />
    End Sub<br />





我正在努力将DLL添加到我的主程序中并偶然发现以下问题:

- 当试图将结构从主程序复制到DLL时,我得到了一个异常

- 我没有问题将数据传输到DLL - 当我尝试复制整个结构时是我收到错误

- 在代码中断的地方你可以清楚地看到正确的数据存在 - 只是从对象到结构的转换失败

结构减速e $>



I'm working on Adding DLL's to my main Program and stumbled over the following Issue:
- When trying to copy a structure from the main program to the DLL I'm getting an exception
- I have no problem transferring data to the DLL - it's when I try to copy over the complete structure is when I get the error
- At the point, where the code breaks you can "clearly" see that the correct data is there -it's just the conversion from the object to the structure is failing
Deceleration of Structure

Public Structure TEST_COPY
        Public V_STRING As String ' same as KdmCognexSetup(Index)
        Public v_Integer As Integer
        Public v_Bool As Boolean
    End Structure
    Dim NEW_STRUCTURE(2) As TEST_COPY



'DLL中的最终代码失败(DLL称为槽反射)


' final code inside DLL that fails (DLL called trough reflection )

Public Sub DLL_SUB(ByVal MethodeName As String, ByVal Par As Object)
        Select Case MethodeName
            Case "TEST"
                Dim x As Integer
                'Try
                For x = 0 To NEW_STRUCTURE.Length - 1
                    '**********
                    ' This line fails whil trying to convert the object to the structure [TEST_COPY]
                    ' You can clearly see the data is there - it's just the conversion fails
                    ' What's missing ?
                    NEW_STRUCTURE(x) = CType(Par(x), TEST_COPY)
                    '**********
                Next
                'Catch
                'MsgBox("Failed")
                'Exit Sub
                'End Try
                'MsgBox("Worked")
        End Select
    End Sub

< br $>




将结构转换为主程序内部和后面的对象完全没问题=因此我认为我的问题必须要做反思



我写了一个小测试程序来解决这个问题 - 但是我不知道如何将包含代码的Zip文件添加到这个问题



如何在问题中添加文件?





谢谢

Georg




Converting the Structure to an object for and back inside the Main Program is no issue at all = therefore I assume my problem has to do with reflection

I wrote a small test program to trouble shoot the issue - but I'm not sure how I can add the Zip file containing the code to this question

How can I add a file to the question ?


Thanks
Georg

推荐答案

:-(nobody有一个建议..

此时此刻我只是将结构的值放在一个字符串中 - 移动t下沉到DLL中,然后将值复制回另一个结构(这是废话,但它允许我继续前进,直到我有更多的时间来研究这个)



幸运的是,在不久的将来,结构不会有太大的变化 - 只需要小心保持正确的订单......但它的工作原理:-)



喜欢它或者不是 - 现在这是我糟糕的解决方案



Georg
:-( "nobody" had a suggestion ..
At this point I just put the values of the structure in a string - move them over into the DLL and then copy the values back into another structure (it's crap but it allows me to move on until I have more time to look into this)

Luckily the structure won't change much in the near future - just need to be careful to keep the correct orders ...but It works :-)

Like it or not - that's my crappy "solution" for now

Georg


我想我搞砸了应该点击解决它我自己..
I guess I messed up should Have clicked on solved it myself ..


我想我知道这里发生了什么...



你有结构中定义的结构DLL,你在另一个程序中定义另一个具有相同成员的结构,并尝试将一个转换为另一个。



你不能这样做,它会抛出异常。尽管数据类型是相同的结构和相同的成员(甚至是相同的名称),但由于它们是不同的类型,因此您无法将其转换为另一个。我不知道VB的解决方案是什么,但在C#中我会使用反射从DLL创建类型并使用dynamic关键字与它进行交互。



您可能可以潜入非托管世界并尝试编组和解组以使它们匹配,但它会导致不安全的代码。



如果不是这样,那么你的问题就很混乱了,即使你的例子在上下文中也没有多少意义,我想你必须展示完整的例子来有意义。
I "think" I know what's going on here...

You have the structure defined in the DLL, you define another structure with the same members in another program and try to convert one to the other.

You can't do that and it will throw an exception. Even though the data types are the same structure and same members (and even the same name), you can't cast one to the other since they are different types. I don't know what the solution to this is in VB, but in C# I would use reflection to create the type from the DLL and use the dynamic keyword to interact with it.

You might be able to dive into the unmanaged world and try marshaling and unmarshaling to get them to match, but it makes for unsafe code.

If this isn't it then your question is very confusing, and even your example doesn't make a lot of sense out of context, I think you would have to show the full example to make sense.


这篇关于反射Ctype对象的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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