且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

直播? Ctype?少了什么东西 ?

更新时间:2023-11-28 23:08:28

你有在两个不同的地方定义KDMUser_Settings,即使它们具有相同的名称和结构


这是.NET中常见的误解,即使这两种类型看起来完全相同,他们不能在彼此之间施放。有很多方法,但它主要使用非托管代码和指针。


Why can I get below [test] statement not to work ?

I' trying to convert my class [KDM_USERSETTINGS] single instance into an object and then back into another instance of the same class

The code breaks when trying to cast back the object into the new instance
What Am I Missing ?

(In the actual app I'm trying to copy the data between two dll's)



Dim OBJ As Object = {CType(Original, KDM_UserSettings)}

              Dim I As New KDM_UserSettings

              I = DirectCast(OBJ, KDM_UserSettings)




Thanks

Georg

Do you have KDMUser_Settings defined in two different places, even though they have the same name and structure?

This is a common misconception in .NET, even though the two types look exactly the same, they cannot be cast between each other. There are ways around this but its mostly using unmanaged code and pointers.