c# - 返回 const 引用的 vector ,不可能向下转型

标签 c# c++ vector swig downcast

我在 SWIG 中遇到问题。

如文档 here 中所述,SWIG 不支持 Java 和 C# 中的向下转型。我遵循了文档的建议,并为对象工厂创建了正确的类型映射。我可以知道创建一个对象 A 并将其向下转换为 B。

但是,我还有一个方法返回指向对象 A 的指针 vector 。我编写了一些类型映射以便使用我创建的工厂。但是,我无法向下转换 vector 的任何元素。

这是一个例子:

class A {  public:
    string name;
    void someMethod(); 
... }

class B : public A { 
       specialBMethod();
... }

class C : public A { 
       specialCMethod();
... }

std::vector<std::shared_ptr<A>> someRandomMethod();

现在,我想用 C# 来实现:

A tmp = someFactoryMethod("B") // will return a B class;
(B)tmp.specialBMethod(); // works fine;

A_vector test = someRandomMethod();
if (test[0].name == "B")
  (B)tmp.specialBMethod(); // InvalidCastException

最有趣的部分是,如果我使用 CopyTo 复制 vector ,并将其放入数组中,例如,它就可以工作。

A_vector tmp = someRandomMethod();
A[] test = tmp.ToArray(); // imagine the ToArray method was implemented
if (test[0].name == "B")
  (B)tmp.specialBMethod(); // works fine

我认为当 vector 返回对象的常量引用时存在问题。它失去了继承权,变得不可能堕落。

在这种情况下我很迷失。我还开了一个issue在 SWIG 存储库上。 任何帮助都会很棒;

编辑:按照 Flexo 的要求,这里是一个最小的完整示例。

示例.hpp

class A {  
    string name;
public:
    void someMethod(); 
    string getName() { return name; }
... }

class B : public A { 
       specialBMethod();
... }


class C : public A { 
       specialCMethod();
... }

std::vector<std::shared_ptr<A>> someRandomMethod();

示例.i

%{
#include "example.hpp"
%}

%pragma(csharp) imclasscode=%{
public static System.Collections.Generic.Dictionary<string, System.Type> aDictionary;

public static System.Collections.Generic.Dictionary<string, System.Type> createDictionary<T>() where T : class
{
    System.Collections.Generic.Dictionary<string, System.Type> dictionary = new System.Collections.Generic.Dictionary<string, System.Type>();
    foreach (System.Type type in
        System.Reflection.Assembly.GetAssembly(typeof(T)).GetTypes())
    {
        if (type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(T)))
        {
            string tmp = type.ToString().Split('.')[type.ToString().Split('.').Length - 1].Substring(0, type.ToString().Split('.')[type.ToString().Split('.').Length - 1].IndexOf(typeof(T).Name));
            dictionary.Add(tmp, type);
        }
    }
    return dictionary;
}

public static A createA(System.IntPtr cPtr, bool owner)
{
    A ret = null;
    if (cPtr == System.IntPtr.Zero) {
      return ret;
    }
    string ct = ($imclassname.A_getName(new System.Runtime.InteropServices.HandleRef(null, cPtr)));
    if (aDictionary == null)
        aDictionary = createDictionary<A>();
    if (aDictionary.ContainsKey(ct))
    {
        System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
        ret = (A)System.Activator.CreateInstance(aDictionary[ct], flags, null, new object[] { cPtr, owner }, null);
    }
    else
    {
        ret = new A(cPtr, owner);
    }
    return ret;
}
%}

%typemap(csout, excode=SWIGEXCODE)
  A*, std::shared_ptr<A> {
    System.IntPtr cPtr = $imcall;
    Chip ret = liblogicalaccess_examplePINVOKE.createA(cPtr, $owner);$excode
    return ret;
}

%include "example.hpp"

测试.cs

A tmp = someFactoryMethod("B") // will return a B class;
(B)tmp.specialBMethod(); // works fine;

A_vector test = someRandomMethod();
if (test[0].name == "B")
  (B)tmp.specialBMethod(); // InvalidCastException

A_vector tmp = someRandomMethod();
A[] test = new A[tmp.Count];
tmp.CopyTo(test);
if (test[0].name == "B")
  (B)tmp.specialBMethod(); // works fine

最佳答案

编辑:由于问题发生了变化,我的答案不再有任何贡献。

尝试使用 std::unique_ptr vector ,因为对象不是多态的。

关于c# - 返回 const 引用的 vector ,不可能向下转型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44696081/

相关文章:

c# - 将 int[] 连接到相同大小的 string[]

c++ - UDP打洞

c++ - 如何更改 QTimeEdit 的步长?

c++ - STL - 字符串是 vector 吗?

c++ - 如何删除 'all duplicated ' 值?

matlab - 如何将向量转换为矩阵? (Matlab)

c# - 是否可以在服务器 (IIS) 上设置内容过期以强制下载新的脚本文件版本?

c# - WinSCP .NET 程序集 : How to download directories

c++ - 为同一类提供不同比较运算符的最佳策略是什么?

c# - 按时间取消任务