【发布时间】:2017-06-22 09:56:32
【问题描述】:
我在 SWIG 中遇到问题。
如文档 here 中所述,SWIG 不支持 Java 和 C# 中的向下转换。我遵循了文档的建议,并为对象工厂创建了正确的类型映射。我可以知道创建一个对象 A 并将其向下转换为 B。
但是,我还有一个方法返回指向对象 A 的指针向量。我编写了一些类型映射以使用我创建的工厂。但是,我不能向下转换向量的任何元素。
这是一个例子:
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 复制向量并将其放入一个数组中,例如,它就可以工作。
A_vector tmp = someRandomMethod();
A[] test = tmp.ToArray(); // imagine the ToArray method was implemented
if (test[0].name == "B")
(B)tmp.specialBMethod(); // works fine
我认为当向量返回对象的 const 引用时会出现问题。它失去了继承,变得无法向下转型。
在这种情况下,我很迷茫。我还在 SWIG 存储库上打开了issue。 任何帮助都会很棒;
编辑:正如 Flexo 所要求的,这里是一个最小的完整示例。
example.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();
example.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"
test.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
【问题讨论】:
-
查看我对旧问题的回答:stackoverflow.com/a/27209463/168175
-
是的,你在这篇文章中解释的内容我已经做到了,我使用我在 C# 中制作的工厂制作了相同的类型图。现在,当我的方法返回 shared_ptr 的向量时,问题仍然存在。也许我需要在“std::vector<:shared_ptr>>”上制作一个 csout 类型映射,但我还不知道该放什么。
标签: c# c++ vector swig downcast