gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:47 26 4
gpt4 key购买 nike

我在 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 返回对象的 const 引用时会出现问题。它失去了继承性,变得无法向下转型。

在这种情况下我很迷茫。我还开了一个issue在 SWIG repo 协议(protocol)上。任何帮助都会很棒;

编辑:正如 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# - vector 返回 const 引用,不可能向下转型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44696081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com