2014-07-12 3 views
0

저는 Vala를 처음 사용하고 조금 놀았습니다. 현재 런타임에 일반 목록의 형식 매개 변수를 결정하는 방법을 찾고 있습니다.Vala : 런타임에 List 내에서 제네릭 형식을 결정하십시오.

아래 코드는 'reflection'을 사용하여 Locations 클래스의 속성을 인쇄합니다. 그러나 런타임에이 목록에 문자열 인스턴스가 포함되어 있는지 확인할 수 없습니다.

이 방법이 있습니까? 또는 Vala에서 지원되지 않습니까?

using Gee; 
class Locations : Object { 
    public string numFound { get; set; } 
    public ArrayList<string> docs { get; set; } 
} 

void main() { 
    ObjectClass ocl = (ObjectClass) typeof (Locations).class_ref(); 
    ParamSpec[] properties = ocl.list_properties(); 
    foreach (ParamSpec spec in properties) { 
     string fieldName = spec.get_nick(); 
     stdout.printf (" fieldName: %s\n", fieldName); 
     Type fieldType = spec.value_type; 
     stdout.printf (" Type : %s\n", fieldType.name()); 
    } 
} 

출력 : G 객체/GType 때문에이 작업을 수행하는 일반적인 방법이 없습니다

fieldName: numFound 
Type : gchararray 
fieldName: docs 
Type : GeeArrayList 

답변

1

단순히 표현이 아니다. 예를 들어 Gee.ArrayList 대신 GLib.GenericArray (또는 GLib.List)을 사용하는 경우 운이 좋지 않을 것입니다.

즉, libgee는 방법을 제공합니다. libgee의 대부분의 컨테이너와 마찬가지로 Gee.ArrayListelement_type 속성을 포함하는 Gee.Traversable을 구현합니다. 그러나 GLib.ObjectClass뿐만 아니라 인스턴스가 필요합니다.

+0

좋아! 당신의 대답은 제게 많은 도움이되었습니다. 오늘 밤 나는 기능적인 예를 올릴 것이다. – scuro

0

첫 번째 답변의 제안으로이 솔루션을 제안했습니다. 이 내가 찾던 정확히 :

using Gee; 
class Locations : Object { 
    public int numFound { get; set; } 
    public Gee.List<string> docs { get; set; } 
    public Locations() { 
     docs = new ArrayList<string>(); 
    } 
} 

void main() { 
    ObjectClass ocl = (ObjectClass) typeof (Locations).class_ref(); 
    ParamSpec[] properties = ocl.list_properties(); 
    Locations locs = new Locations(); 
    foreach (ParamSpec spec in properties) { 
     string fieldName = spec.get_nick(); 
     Type fieldType = spec.value_type; 

     // get the docs instance from the locations instance 
     GLib.Value props = Value(fieldType); 
     locs.get_property(fieldName, ref props); 
     stdout.printf ("Field type %s : %s\n", fieldName, props.type_name()); 
     if(props.holds(typeof (Gee.Iterable))) { 
      Gee.Iterable docs = (Gee.Iterable)props.get_object(); 
      stdout.printf ("\tList parameter type : %s\n", docs.element_type.name()); 
     } 
    } 
} 

출력 :

Field type numFound : gint 
Field type docs : GeeList 
    List parameter type : gchararray 
관련 문제