在 Vala 中访问指针对象属性

Accessing pointer object properties in Vala

我重构了一些我的代码,所以我需要一个可以包含多个列表类型的指针:

owl_list = new Gee.LinkedList<OpenWithAction> ();
a_list = new Gee.LinkedList<OpenAppAction> ();

Gee.List* any_list = null;

所以我有一个指针 any_list,我可以使用它来访问 owl_list 或 a_list(取决于此处不存在的开关,但假设存在)

if (!any_list.size)
    return null;

但这会失败,因为 valac 向我抛出 error: The name `size' does not exist in the context of `Gee.List*' if (!any_list.size)

很长一段时间以来我都没有使用过任何 C、C++,而且我也不是 vala 专家,因为我使用了更多的无类型语言,但是有什么方法可以工作吗?

编辑:

我刚试过

fieldType = OpenWithAction.get_type();
if (!(any_list as Gee.List<fieldType>).size)

error: The type name `fieldType' could not be found if (!(any_list as Gee.List<fieldType>).size) 显然我做错了什么,我想做的是:Vala: determine generic type inside List at runtime,我只是无法实现它。 return空;

编辑 2:

我只是部分解决了我的问题:

正如@jens-mühlenhoff 所说,是的 OpenWithActionOpenAppAction 有一个共同的祖先,它是 GLib.Action

所以我所要做的就是声明 :

Gee.List<Action> any_list = null;

而不是

Gee.List* any_list = null;

现在 foreach (var action in any_list) 正在工作,但我仍然遇到

的错误
if (any_list->size == null)
    return null;

error: The name `size' does not exist in the context of `Gee.List<Synapse.Action>?' if (any_list->size == null)

另一个尝试是:

if (!any_list.size)
    return null;

Operator not supported for `int' if (!any_list.size)

any_list 是一个指针,所以不能使用 . 运算符。试试 any_list->size.

我做错了两件事:

1- 就像在 C# 中一样 如果不需要,我不应该使用指针 。所以使用: Gee.List<Action> any_list = null; 工作正常。解释是 OpenWithActionOpenAppAction 有一个共同的祖先 class,所以声明一个具有该类型的列表工作正常(另一方面,我不知道他们是否没有共同的祖先)。

2- int类型不能用作bool类型所以if (any_list.size == 0) 会工作,而 if (!any_list.size) 会抛出错误

感谢大家的帮助:)