Example #1
0
        public bool InstantiateBoundType(Type t, out object result, IWarningLogger log)
        {
            object init_obj;

            result = null;

            object[] attrs = t.GetCustomAttributes(typeof(StructureBindingAttribute), false);

            if (attrs == null || attrs.Length == 0)
            {
                init_obj = null;
            }
            else
            {
                StructureBindingAttribute sba = attrs[0] as StructureBindingAttribute;
                if (!sba.UsesStructure)
                {
                    init_obj = null;
                }
                else
                {
                    Type stype = sba.StructureType;

                    if (!known_structs.ContainsKey(stype))
                    {
                        string s = String.Format("Type {0} must be created in the context of a {1} structure, " +
                                                 "but none is referenced in this scope.", t, stype);
                        log.Error(9999, s, null);
                        return(true);
                    }

                    init_obj = known_structs[stype];
                }
            }

            result = Activator.CreateInstance(t, init_obj);

            return(false);
        }
Example #2
0
        public UserType ResolveUsedStructureType(TypeResolveContext trc, bool errors)
        {
            if (!resolved)
            {
                if (errors)
                {
                    throw new InvalidOperationException();
                }
                return(null);
            }

            UserType ret;

            if (IsUser)
            {
                // User type. Use our lookup tables

                StructureBoundItem sbi = trc.Driver.GetUserTypeItem((string)t) as StructureBoundItem;

                if (sbi == null)
                {
                    if (errors)
                    {
                        throw ExHelp.App("Expected structure bound item but got {0} for {1}",
                                         trc.Driver.GetUserTypeItem((string)t), this);
                    }
                    return(null);
                }

                if (!sbi.UsesStructure)
                {
                    return(null);
                }

                ret = sbi.NS.ParamsType;
            }
            else
            {
                // System type. Use reflection.

                Type type = (Type)t;

                object[] attrs = type.GetCustomAttributes(typeof(StructureBindingAttribute), false);

                if (attrs.Length == 0)
                {
                    throw ExHelp.App("Expected type {0} to have a StructureBindingAttribute " +
                                     "but it didn't", type);
                }

                StructureBindingAttribute attr = (StructureBindingAttribute)attrs[0];

                if (!attr.UsesStructure)
                {
                    return(null);
                }

                ret = new UserType(attr.StructureType);
            }

            if (ret.Resolve(trc, errors))
            {
                if (errors)
                {
                    throw ExHelp.App("Failed to resolve bound structure type {0}", ret);
                }
                return(null);
            }

            return(ret);
        }