简体   繁体   English

object 表示和托管/非托管交互的结构/类元数据

[英]Struct/Class metadata for object representation and managed/unmanaged interaction

I know there is plenty of similar questions, but I want to understand very specific aspects that are never mentioned anywhere.我知道有很多类似的问题,但我想了解从未在任何地方提及的非常具体的方面。 In both cases: managed (NET runtime in this case) and native /unmanaged (c, cpp, etc)在这两种情况下:托管(在这种情况下为 NET 运行时)和本机/非托管(c、cpp 等)

Taken from here: https://adamsitnik.com/Value-Types-vs-Reference-Types取自这里: https://adamsitnik.com/Value-Types-vs-Reference-Types https://i.stack.imgur.com/0Qtnu.png

在此处输入图像描述

The first issue that not just wasn't glossed over but not even mentioned is how your code/runtime supposed to know what type of struct you are dealing with?第一个不仅没有被掩盖甚至没有提到的问题是您的代码/运行时应该如何知道您正在处理的结构类型? If struct is just all its data packed together, then where is metadata about what type of struct it is?如果 struct 只是将所有数据打包在一起,那么关于它是什么类型的 struct 的元数据在哪里? Okay, may be in case of managed .NET runtime and binaries it is easier, it is part of the IL, but what about native binary code?好的,在托管 .NET 运行时和二进制文件的情况下,它更容易,它是 IL 的一部分,但是本机二进制代码呢? It gets stripped 100%, if you open it for text reading there is no function or struct names in you binary.它被剥离 100%,如果您打开它进行文本阅读,那么二进制文件中没有 function 或结构名称。 How would you know runtime what the struct you receive and how to treat/ parse it if there is no struct metadata with it?如果没有结构元数据,您将如何知道运行时收到的结构以及如何处理/解析它? The pointer just points at memory, but there is no struct' structure and members there stored.指针只指向 memory,但没有存储结构和成员。 At least class can be identified by extra data it has (object header and method table).至少 class 可以通过它拥有的额外数据(对象 header 和方法表)来识别。

Things get even more confusing when you receive struct data from unmanaged/native space.当您从非托管/本机空间接收结构数据时,事情会变得更加混乱 You NEED to have that data embedded into struct otherwise how would you know what you receive?您需要将该数据嵌入到结构中,否则您怎么知道收到了什么? And I can't even begin to understand * how would this work for something like classes, because they are thousandfold more complex.而且我什至无法理解* 这对于类之类的东西是如何工作的,因为它们要复杂千倍。 How do you even return class from unmanaged space?您甚至如何从非托管空间返回 class ?

If struct is just all its data packed together, then where is metadata about what type of struct it is?如果 struct 只是将所有数据打包在一起,那么关于它是什么类型的 struct 的元数据在哪里?

There isn't any.没有。 With raw value data - whether that means an individual value, or an array/vector/whatever, the consuming code is the thing that knows how to interpret it.对于原始值数据——无论是单个值,还是数组/向量/其他,消费代码都是知道如何解释它的东西。 What it is is just: bytes.只是:字节。

How would you know runtime what the struct you receive and how to treat/ parse it if there is no struct metadata with it?如果没有结构元数据,您将如何知道运行时收到的结构以及如何处理/解析它?

Because it is defined in the API, either in a signature, or with human words such as "the pointer refers to the start of len elements of type Foo "因为它是在 API 中定义的,要么在签名中,要么用人类的话,例如“指针指向Foo类型的len元素的开始”

You NEED to have that data embedded into struct您需要将该数据嵌入到结构中

No you don't;不,你没有; you just need to agree in advance what you are sending/receiving.您只需要事先同意您发送/接收的内容。 This is how the vast majority of interactions between different codebases works, and has always worked.这就是不同代码库之间绝大多数交互的工作方式,并且一直有效。 Having object metadata is the exception , not the norm.拥有object 元数据是例外,而不是常态。

How do you even return class from unmanaged space?您甚至如何从非托管空间返回 class ?

You wouldn't, if by "classes" in this context you mean managed objects.你不会,如果在这种情况下“类”是指托管对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM