For fast creation of types, useful for creating POCOs
Ejemplo n.º 1
0
        static void TypeLoadedHandler(TypeFactory sender, TypeFactoryLoadedEventArgs e)
        {
            var atbType = typeof(ActionDisplayScriptAttribute);
            var mpdType = typeof(ActionDisplayScriptHandler);

            const BindingFlags bindFlags =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Static;

            // Search through every method in the class
            foreach (var method in e.LoadedType.GetMethods(bindFlags))
            {
                // Get all of the ActionDisplayScriptAttributes for the method (should only be one)
                var atbs = (ActionDisplayScriptAttribute[])method.GetCustomAttributes(atbType, true);
                if (atbs.Length > 1)
                {
                    const string errmsg = "Multiple ActionDisplayScriptAttributes found for method `{0}`.";
                    Debug.Fail(string.Format(errmsg, method.Name));
                    throw new TypeException(string.Format(errmsg, method.Name));
                }

                // Create the delegates for the methods
                foreach (var atb in atbs)
                {
                    ActionDisplayScriptHandler del;
                    try
                    {
                        del = (ActionDisplayScriptHandler)Delegate.CreateDelegate(mpdType, null, method, true);
                        if (del == null)
                        {
                            const string errmsg = "Failed to create ActionDisplayScriptHandler delegate for `{0}`.";
                            throw new TypeException(string.Format(errmsg, atb));
                        }
                    }
                    catch (Exception ex)
                    {
                        const string errmsg =
                            "Failed to create ActionDisplayScriptHandler delegate for method `{0}`. Make sure it is a static method and contains the correct parameters.";
                        Debug.Fail(string.Format(errmsg, method));
                        throw new InstantiateTypeException(string.Format(errmsg, method), ex);
                    }

                    // Ensure the name is unique
                    if (_scriptHandlers.ContainsKey(atb.Name))
                    {
                        const string errmsg =
                            "Found multiple ActionDisplayScriptHandlers with the name `{0}`. Names must be unique.";
                        Debug.Fail(string.Format(errmsg, atb.Name));
                        throw new InstantiateTypeException(string.Format(errmsg, atb.Name), mpdType);
                    }

                    _scriptHandlers.Add(atb.Name, del);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles when a new type has been loaded into the <see cref="TypeFactory"/>.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param>
        /// <exception cref="ArgumentException">The loaded type in <paramref name="e"/> was invalid or already loaded.</exception>
        static void OnLoadTypeHandler(TypeFactory sender, TypeFactoryLoadedEventArgs e)
        {
            var instance = (NPCChatConditionalBase)sender.GetTypeInstance(e.Name);

            // Make sure the name is not already in use
            if (ContainsConditional(instance.Name))
            {
                const string errmsg =
                    "Could not add Type `{0}` - a NPC chat conditional named `{1}` already exists as Type `{2}`.";
                var err = string.Format(errmsg, e.LoadedType, instance.Name, _instances[instance.Name].GetType());
                if (log.IsFatalEnabled)
                    log.Fatal(err);
                Debug.Fail(err);
                throw new ArgumentException(err);
            }

            // Add the value to the Dictionary
            _instances.Add(instance.Name, instance);

            if (log.IsDebugEnabled)
                log.DebugFormat("Loaded NPC chat conditional `{0}` from Type `{1}`.", instance.Name, e.LoadedType);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicEntityFactoryBase"/> class.
        /// </summary>
        protected DynamicEntityFactoryBase()
        {
            var filter = GetTypeFilterCreator();

            _typeCollection = new TypeFactory(filter.GetFilter(), OnLoadTypeHandler);
        }
 /// <summary>
 /// Handles when a new type has been loaded into the <see cref="DynamicEntityFactoryBase"/>.
 /// </summary>
 /// <param name="typeFactory">The type factory.</param>
 /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param>
 protected virtual void OnLoadTypeHandler(TypeFactory typeFactory, TypeFactoryLoadedEventArgs e)
 {
     if (log.IsDebugEnabled)
         log.DebugFormat("Loaded DynamicEntity `{0}` from Type `{1}`.", e.Name, e.LoadedType);
 }
Ejemplo n.º 5
0
		private static void CreateEntriesFromFactory (MenuItem menu, TreeStore store, EventHandler handler, TypeFactory factory, IList menuEntries)
		{
			GLib.SList group = new GLib.SList (IntPtr.Zero);
			Menu submenu = new Menu ();

			foreach (DictionaryEntry de in factory) {
				TypeFactoryEntry entry = (TypeFactoryEntry) de.Value;
				if (store != null)
					store.AppendValues (false, entry.Description, entry.Key);
				RadioMenuItem item = new RadioMenuItem (group, entry.Description);
				item.Activated += handler;
				group = item.Group;
				submenu.Append (item);
				menuEntries.Add (new RadioMenuItemInfo (item, entry.Key));
			}

			menu.Submenu = submenu;
			menu.ShowAll ();
		}