C#中的痛饮-在可移植类库中找不到HandleRef [英] swig in c# - HandleRef could not be found in portable class library

查看:101
本文介绍了C#中的痛饮-在可移植类库中找不到HandleRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swig将我的c ++代码包装到c#中.如果C#文件的输出目标是class library,则没有错误并且可以成功构建.

I'm tring to wrap my c++ code to c# with using Swig. If the output destination of C# files is class library , there is no error and succesfully build.

但是,由于这个原因,我想同时支持Windows Phone 8.1和Windows 8.1,所以我使用的是便携式类库,而不是普通的类库.在这种情况下,我会看到错误消息;

However I want to support both windows phone 8.1 and windows 8.1 because of this reason I'm using portable class library rather than normal class library. In this situation I'm getting error that says;

名称空间"System.Runtime.InteropServices"中不存在类型或名称空间名称"HandleRef"(您是否缺少程序集引用?)

The type or namespace name 'HandleRef' does not exist in the namespace 'System.Runtime.InteropServices' (are you missing an assembly reference?)

我真的不知道我在想什么.这个问题的解决办法是什么? 我猜Windows Phone 8.1和Windows 8.1不支持HandleRef,但我不确定.如果是这样,我该怎么办?

I really don't know what I am missing. What is the solution of this problem? My guess windows phone 8.1 and windows 8.1 does not support HandleRef but I'm not sure. If it so, What should I do?

推荐答案

这个问题有点老了,但是在使用.NET Core时遇到了同样的问题,我想我应该分享我的解决方案.

This question is a bit older, but having had the same problem when using .NET Core I thought I'd share my Solution.

要告诉SWIG停止使用HandleRef,您必须更改所有默认(或特定)类型的%typemap(imtype)%typemap(csbody).

To tell SWIG to stop using HandleRef you have to change the %typemap(imtype) and %typemap(csbody) of all default (or of specific) types.

imtype指定在modulenamePINVOKE方法参数中显示的类型.将其更改为可以从/转换为指针类型的东西.

imtype specifies the type that appears in your modulenamePINVOKE method parameters. Change it to something that can be marshaled from/to a pointer type.

csbody替换了SWIGTYPE_类的整个主体,这意味着您必须实现自己的类(必须更改存储为HandleRef的变量).如果您的新实现没有getCPtr方法,则还必须更改%typemap(csin)

csbody replaces the entire body of your SWIGTYPE_ classes, meaning you'll have to implement you own (You have to, to change the variable that is stored as HandleRef). If your new implementation doesn't have a getCPtr method you have to change %typemap(csin) as well

下面是一个使用System.IntPtr而不是HandleRef的示例,将其放在界面文件的顶部:

Here is an example that uses System.IntPtr instead of HandleRef, place it at the top in your interface file:

%typemap(imtype) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) "System.IntPtr"
%typemap(csin) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) "$csinput.Pointer"

%typemap(csbody) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) %{

  private volatile System.IntPtr swigCPtr;

  protected $csclassname() 
  {
    swigCPtr = System.IntPtr.Zero;
  }

  internal System.IntPtr Pointer
  {
    get
    {
      return swigCPtr;
    }
  }
%}

注意:SWIGTYPE是任何类型的占位符.

Note: SWIGTYPE is a placeholder for any type.

参考: SWIG 3.0文档-第20章"SWIG和C#"

这篇关于C#中的痛饮-在可移植类库中找不到HandleRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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