Example #1
0
        public static object InstantiateCollection(PfCollection pfcoll, Type context_type = null)
        {
            var coll_type   = pfcoll.CollectionType.Resolve(context_type);
            var coll        = Activator.CreateInstance(coll_type);
            var el_type_ary = new Type[pfcoll.CollectionType.ElementTypes.Length];

            for (int i = 0; i < pfcoll.CollectionType.ElementTypes.Length; i++)
            {
                el_type_ary[i] = pfcoll.CollectionType.ElementTypes[i].Resolve(context_type);
            }

            var add_method = coll_type.GetMethod("Add",
                                                 System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
                                                 null,
                                                 el_type_ary,
                                                 null
                                                 );

            for (int i = 0; i < pfcoll.Elements.Length; i++)
            {
                var elem_list      = pfcoll.Elements[i];
                var elem_inst_list = new object[elem_list.Length];
                for (int j = 0; j < elem_list.Length; j++)
                {
                    var elem      = elem_list[j];
                    var elem_inst = ConvertType(elem, null, context_type);
                    Console.WriteLine($"elem_inst: VALUE ({elem_inst}) TYPE ({elem_inst?.GetType()})");
                    elem_inst_list[j] = elem_inst;
                }
                add_method.Invoke(coll, elem_inst_list);
            }

            return(coll);
        }
Example #2
0
        public object TryMakeObject()
        {
            var a = ArrayType != null;
            var b = CollectionType != null;
            var c = ObjectType != null;
            var d = InsertID != null;

            if (a && b || a && c || a && d || b && c || b && d || c && d)
            {
                throw new InvalidOperationException("Inconsistent object type");
            }

            object obj = null;

            if (ArrayType != null)
            {
                obj = new PfArray(ArrayType, ArrayEntries);
            }
            else if (CollectionType != null)
            {
                obj = new PfCollection(CollectionType, CollectionElements);
            }
            else if (ObjectType != null)
            {
                obj = new PfObject(ObjectType, Data);
            }
            else if (InsertID != null)
            {
                obj = new PfInsert(InsertID, Data);
            }

            if (obj == null)
            {
                throw new InvalidOperationException("Unknown object type");
            }

            return(obj);
        }