编组自定义属性类型 [英] marshal custom attribute type

查看:120
本文介绍了编组自定义属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++函数:

i have a C++ function:

void Reshape(const BlobShape& shape)

其中  BlobShape是一个C ++类

where  BlobShape is a C++ class

我 将BlobShape类转换为c#;所以c#BlobShape是:

i  convert the BlobShape class into c#; so the c# BlobShape is :

public partial class BlobShape
	{
		public List<long> Dim { get; set; }

	}

我已经导出了Reshape方法并从c#中调用它,如下所示:

I have exported the Reshape method and call it from c# like this:

BlobShape shape = new BlobShape();

void Reshape(ref shape);

我收到此错误:

MarshalDirectiveException: Type BlobShape which is passed to unmanaged code must have a StructLayout attribute.

推荐答案

注意在C#中使用C ++的一种方便的,特别设计的方法:创建一个CLR类库(在New Project向导中可用)。如果你已经用C ++制作了
a DLL,那么尝试添加"公共语言运行时支持(/ clr)"选项。

这些程序集可以通过C#使用"添加引用"对话框引用。 / span>

在这样的程序集中,您可以声明可以从C#访问的类和函数。例如,在C ++中:

  
名称空间
ClassLibrary1

&NBSP;&NBSP;
{

   {

     
public

ref
class
BlobShape

      public ref class BlobShape

     
{

      {

        
public

     
      
property System :: Collections :: Generic :: List < Int64 >
^ Dim;

     
};

      };

  
}

   }

您不需要在C#中重复定义,因为它是从C ++导出的。在C#中你可以写:

  
var
b =
new ClassLibrary1.BlobShape();

  
b.Dim =
new List< long >();

   b.Dim = new List<long>();

  ;

您还可以添加成员函数。

如果需要(重用一些现有代码),在C ++中可以将数据复制到其他类型。例如,
List 可以使用循环传输到 std :: vector

If required (to reuse some existing code), in C++ you can copy the data to other kind. For example, List can be transferred to std::vector using a loop.





这篇关于编组自定义属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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