/// <summary> /// Initializes a new instance of the <see cref="DbControllerQueryAttributeChecker"/> class. /// </summary> /// <param name="missingAttributeHandler">The event handler for types with missing attributes. /// Cannot be null.</param> /// <exception cref="ArgumentNullException"><paramref name="missingAttributeHandler"/> is null.</exception> /// <param name="typesToIgnore">Optional array of types to ignore. If a type is in this collection, it will /// never be invoked by the <paramref name="missingAttributeHandler"/>.</param> public DbControllerQueryAttributeChecker( TypedEventHandler<DbControllerQueryAttributeChecker, EventArgs<Type>> missingAttributeHandler, params Type[] typesToIgnore) { if (missingAttributeHandler == null) throw new ArgumentNullException("missingAttributeHandler"); _missingAttributeHandler = missingAttributeHandler; _typesToIgnore = typesToIgnore; var filter = new TypeFilterCreator { IsClass = true, IsAbstract = false, IsInterface = false, RequireAttributes = false, Interfaces = new Type[] { typeof(IDbQuery) }, MatchAllInterfaces = true, RequireInterfaces = false, RequireConstructor = false, IsEnum = false }; new TypeFactory(filter.GetFilter(), LoadTypeHandler); }
/// <summary> /// Initializes a new instance of the <see cref="ToolManager"/> class. /// </summary> ToolManager() { // Create the type filter var tfc = new TypeFilterCreator { IsClass = true, IsAbstract = false, RequireConstructor = true, ConstructorParameters = new Type[] { typeof(ToolManager) }, Subclass = typeof(Tool), }; var filter = tfc.GetFilter(); // Create the type factory to load the tools new TypeFactory(filter, typeFactory_TypeLoaded); // Listen for containers being added and removed ToolTargetContainers.Added += ToolTargetContainers_Added; ToolTargetContainers.Removed += ToolTargetContainers_Removed; // Load the default state settings _toolState.CurrentSettingsFile = ToolStateManager.GetFilePath(ContentPaths.Build, null); // Change to the generic "user" profile ToolSettingsProfileName = "User"; // Create the auto-saver _autoSaveSettingsTimer = new Timer(AutoSaveSettingsTimerCallback, _toolState, _autoSaveSettingsRate, _autoSaveSettingsRate); }
/// <summary> /// Initializes a new instance of the <see cref="DbControllerQueryAttributeChecker"/> class. /// </summary> /// <param name="missingAttributeHandler">The event handler for types with missing attributes. /// Cannot be null.</param> /// <exception cref="ArgumentNullException"><paramref name="missingAttributeHandler"/> is null.</exception> /// <param name="typesToIgnore">Optional array of types to ignore. If a type is in this collection, it will /// never be invoked by the <paramref name="missingAttributeHandler"/>.</param> public DbControllerQueryAttributeChecker( TypedEventHandler <DbControllerQueryAttributeChecker, EventArgs <Type> > missingAttributeHandler, params Type[] typesToIgnore) { if (missingAttributeHandler == null) { throw new ArgumentNullException("missingAttributeHandler"); } _missingAttributeHandler = missingAttributeHandler; _typesToIgnore = typesToIgnore; var filter = new TypeFilterCreator { IsClass = true, IsAbstract = false, IsInterface = false, RequireAttributes = false, Interfaces = new Type[] { typeof(IDbQuery) }, MatchAllInterfaces = true, RequireInterfaces = false, RequireConstructor = false, IsEnum = false }; new TypeFactory(filter.GetFilter(), LoadTypeHandler); }
public void InterfaceFailTest() { var f1 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ia) } }; var f2 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ib) } }; var f3 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ia), typeof(Ib) } }; var f4 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ia), typeof(Ib) } }; var f5 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = false, Interfaces = new Type[] { typeof(IEnumerable) } }; Assert.Throws <TypeFilterException>(() => f1.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f2.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f3.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f4.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f5.GetFilter()(typeof(A))); }
public void IsInterfacePassTest() { var f1 = new TypeFilterCreator { IsInterface = true }; Assert.IsTrue(f1.GetFilter()(typeof(Ia))); }
public void IsInterfaceNullTest() { var f1 = new TypeFilterCreator { IsInterface = null }; Assert.IsTrue(f1.GetFilter()(typeof(int))); }
public void IsInterfaceFailTest() { var f1 = new TypeFilterCreator { IsInterface = false }; Assert.IsFalse(f1.GetFilter()(typeof(Ia))); }
public void IsClassPassTest() { var f1 = new TypeFilterCreator { IsClass = true }; Assert.IsTrue(f1.GetFilter()(typeof(A))); }
public void IsClassFailTest() { var f1 = new TypeFilterCreator { IsClass = false }; Assert.IsFalse(f1.GetFilter()(typeof(A))); }
public void IsAbstractPassTest() { var f1 = new TypeFilterCreator { IsAbstract = true }; Assert.IsTrue(f1.GetFilter()(typeof(baseClass))); }
public void IsAbstractNullTest() { var f1 = new TypeFilterCreator { IsAbstract = null }; Assert.IsTrue(f1.GetFilter()(typeof(int))); }
public void IsAbstractFailTest() { var f1 = new TypeFilterCreator { IsAbstract = false }; Assert.IsFalse(f1.GetFilter()(typeof(baseClass))); }
public void ConstructorFailTest() { var f1 = new TypeFilterCreator { RequireConstructor = true, ConstructorParameters = new Type[] { typeof(object) } }; var f2 = new TypeFilterCreator { RequireConstructor = true, ConstructorParameters = new Type[] { typeof(object), typeof(int) } }; Assert.Throws<TypeFilterException>(() => f1.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f2.GetFilter()(typeof(A))); }
public void ConstructorPassTest() { var f1 = new TypeFilterCreator { RequireConstructor = true, ConstructorParameters = new Type[] { typeof(object), typeof(string) } }; var f2 = new TypeFilterCreator { RequireConstructor = true, ConstructorParameters = Type.EmptyTypes }; Assert.IsTrue(f1.GetFilter()(typeof(A))); Assert.IsTrue(f2.GetFilter()(typeof(A))); }
public void CustomFilterTest() { var f1 = new TypeFilterCreator { CustomFilter = (x => !x.IsPublic) }; var f2 = new TypeFilterCreator { CustomFilter = (x => x == typeof(object)) }; Assert.IsTrue(f1.GetFilter()(typeof(A))); Assert.IsFalse(f1.GetFilter()(typeof(object))); Assert.IsFalse(f2.GetFilter()(typeof(A))); Assert.IsTrue(f2.GetFilter()(typeof(object))); }
public void SubclassFailTest() { var f1 = new TypeFilterCreator { Subclass = typeof(StringBuilder) }; var f2 = new TypeFilterCreator { Subclass = typeof(Ia) }; Assert.IsFalse(f1.GetFilter()(typeof(A))); Assert.IsFalse(f2.GetFilter()(typeof(A))); }
public void SubclassPassTest() { var f1 = new TypeFilterCreator { Subclass = typeof(baseClass) }; var f2 = new TypeFilterCreator { Subclass = typeof(object) }; Assert.IsTrue(f1.GetFilter()(typeof(A))); Assert.IsTrue(f2.GetFilter()(typeof(A))); }
/// <summary> /// Creates the default type filter. /// </summary> /// <returns>The default type filter.</returns> static Func <Type, bool> GetDefaultFilter() { var typeFilterCreator = new TypeFilterCreator { IsClass = true, IsAbstract = false, ConstructorParameters = Type.EmptyTypes, RequireConstructor = true, Interfaces = new Type[] { typeof(IStatusEffect <TStatType, TStatusEffectType>) } }; return(typeFilterCreator.GetFilter()); }
/// <summary> /// Gets the type filter for the <see cref="EmitterModifierFactory"/>. /// </summary> /// <returns>The type filter for the <see cref="EmitterModifierFactory"/>.</returns> static Func<Type, bool> GetTypeFilter() { var filterCreator = new TypeFilterCreator { IsClass = true, Subclass = typeof(EmitterModifier), IsAbstract = false, RequireConstructor = true, ConstructorParameters = Type.EmptyTypes, }; return filterCreator.GetFilter(); }
/// <summary> /// Gets the type filter for the <see cref="EmitterModifierFactory"/>. /// </summary> /// <returns>The type filter for the <see cref="EmitterModifierFactory"/>.</returns> static Func <Type, bool> GetTypeFilter() { var filterCreator = new TypeFilterCreator { IsClass = true, Subclass = typeof(EmitterModifier), IsAbstract = false, RequireConstructor = true, ConstructorParameters = Type.EmptyTypes, }; return(filterCreator.GetFilter()); }
static Func <Type, bool> GetTypeFilter() { var filterCreator = new TypeFilterCreator { IsClass = true, IsAbstract = false, Interfaces = new Type[] { typeof(ISkill <TSkillType, TStatType, TCharacter>) }, ConstructorParameters = Type.EmptyTypes, RequireConstructor = true }; return(filterCreator.GetFilter()); }
public void ConstructorFailTest() { var f1 = new TypeFilterCreator { RequireConstructor = true, ConstructorParameters = new Type[] { typeof(object) } }; var f2 = new TypeFilterCreator { RequireConstructor = true, ConstructorParameters = new Type[] { typeof(object), typeof(int) } }; Assert.Throws <TypeFilterException>(() => f1.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f2.GetFilter()(typeof(A))); }
/// <summary> /// Initializes the <see cref="NPCChatResponseActionBase"/> class. /// </summary> static NPCChatResponseActionBase() { _emptyActions = new NPCChatResponseActionBase[0]; var filter = new TypeFilterCreator { IsClass = true, IsAbstract = false, Subclass = typeof(NPCChatResponseActionBase), RequireConstructor = true, ConstructorParameters = Type.EmptyTypes }; new TypeFactory(filter.GetFilter(), OnLoadTypeHandler); }
/// <summary> /// Initializes the <see cref="Map"/> class. /// </summary> static Map() { // Cache the _checkDynamicEntitiesForIServerSaveableOnly value var filterCreator = new TypeFilterCreator { IsClass = true, Subclass = typeof(Entity), Interfaces = new Type[] { typeof(IServerSaveable) }, CustomFilter = (x => !x.IsSubclassOf(typeof(DynamicEntity))) }; var filter = filterCreator.GetFilter(); _checkDynamicEntitiesForIServerSaveableOnly = TypeHelper.FindTypes(filter, null).IsEmpty(); }
public void AttributePassTest() { var f1 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(AttribA) } }; var f2 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(AttribB) } }; var f3 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(AttribA), typeof(AttribB) } }; var f4 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(AttribA), typeof(AttribB) } }; Assert.IsTrue(f1.GetFilter()(typeof(A))); Assert.IsTrue(f2.GetFilter()(typeof(A))); Assert.IsTrue(f3.GetFilter()(typeof(A))); Assert.IsTrue(f4.GetFilter()(typeof(A))); }
/// <summary> /// Initializes the <see cref="RefractionEffectFactory"/> class. /// </summary> static RefractionEffectFactory() { // Create the filter var fc = new TypeFilterCreator { IsAbstract = false, IsClass = true, Interfaces = new Type[] { typeof(IPersistable), typeof(IRefractionEffect) }, ConstructorParameters = Type.EmptyTypes }; _filter = fc.GetFilter(); // Create the factory instance _instance = new RefractionEffectFactory(); }
/// <summary> /// Initializes the <see cref="ParticleEmitterFactory"/> class. /// </summary> static ParticleEmitterFactory() { // Create the filter var filterCreator = new TypeFilterCreator { IsClass = true, IsAbstract = false, RequireConstructor = true, ConstructorParameters = new Type[] { typeof(IParticleEffect) }, Subclass = typeof(ParticleEmitter) }; _particleEmitterFilter = filterCreator.GetFilter(); // Create the factory instance _instance = new ParticleEmitterFactory(); }
static void AddAdvancedClassTypeConverters() { // Add the types we want to have use the AdvancedClassTypeConverter. This is purely just for // a better PropertyGrid-based editing experience. Since it doesn't really matter if we add too many // types (since it is only really for the PropertyGrid), we just add EVERY table from the DbObjs. var filterCreator = new TypeFilterCreator { IsClass = true, IsAbstract = false, CustomFilter = (x => x.Name.EndsWith("Table") && (x.Namespace ?? string.Empty).Contains("DbObjs")) }; var filter = filterCreator.GetFilter(); var typesToAdd = TypeHelper.FindTypes(filter, null); AdvancedClassTypeConverter.AddTypes(typesToAdd.ToArray()); // Also automatically add all the instantiable types that derive from some of our base classes var baseTypes = new Type[] { typeof(Entity), typeof(MapBase) }; filterCreator = new TypeFilterCreator { IsClass = true, IsAbstract = false, CustomFilter = (x => baseTypes.Any(x.IsSubclassOf)) }; filter = filterCreator.GetFilter(); typesToAdd = TypeHelper.FindTypes(filter, null); AdvancedClassTypeConverter.AddTypes(typesToAdd.ToArray()); // Manually add some other types we want to have use the AdvancedClassTypeConverter. Again, doesn't // really matter what you add here. In general, you should just add to this when you notice that // a PropertyGrid isn't using the AdvancedClassTypeConverter. AdvancedClassTypeConverter.AddTypes(typeof(MutablePair <ItemTemplateID, byte>), typeof(MutablePair <CharacterTemplateID, ushort>), typeof(EditorQuest), typeof(EditorAlliance), typeof(EditorShop), typeof(EditorCharacterTemplate)); // Set the properties we want to force being readonly in the PropertyGrid AdvancedClassTypeConverter.SetForceReadOnlyProperties(typeof(CharacterTemplateTable), "ID"); AdvancedClassTypeConverter.SetForceReadOnlyProperties(typeof(EditorCharacterTemplate), "ID"); AdvancedClassTypeConverter.SetForceReadOnlyProperties(typeof(ItemTemplateTable), "ID"); #if !TOPDOWN // Set the UITypeEditor for specific properties on classes instead of every property with a certain type AdvancedClassTypeConverter.SetForceEditor(typeof(ItemTemplateTable), new KeyValuePair <string, UITypeEditor>("EquippedBody", new BodyPaperDollTypeEditor())); #endif }
public void AttributeFailTest() { var f1 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(DescriptionAttribute), typeof(AttribA) } }; var f2 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(DescriptionAttribute), typeof(AttribB) } }; var f3 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(DescriptionAttribute) } }; Assert.Throws<TypeFilterException>(() => f1.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f2.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f3.GetFilter()(typeof(A))); }
public void AttributeFailTest() { var f1 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(DescriptionAttribute), typeof(AttribA) } }; var f2 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(DescriptionAttribute), typeof(AttribB) } }; var f3 = new TypeFilterCreator { RequireAttributes = true, MatchAllAttributes = true, Attributes = new Type[] { typeof(DescriptionAttribute) } }; Assert.Throws <TypeFilterException>(() => f1.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f2.GetFilter()(typeof(A))); Assert.Throws <TypeFilterException>(() => f3.GetFilter()(typeof(A))); }
/// <summary> /// Initializes a new instance of the <see cref="AIFactoryBase{T}"/> class. /// </summary> /// <param name="subclass">The Type of the subclass that all AI classes must be derived from. If null, the /// subclass will not be enforced.</param> protected AIFactoryBase(Type subclass) { if (log.IsInfoEnabled) { log.Info("Creating AI factory."); } var filter = new TypeFilterCreator { IsClass = true, IsAbstract = false, Subclass = subclass, Interfaces = new Type[] { typeof(IAI) }, Attributes = new Type[] { typeof(AIAttribute) }, ConstructorParameters = new Type[] { typeof(T) }, MatchAllAttributes = true, MatchAllInterfaces = true, RequireAttributes = true, RequireConstructor = true, RequireInterfaces = (subclass != null ? true : false) }; _typeFactory = new TypeFactory(filter.GetFilter(), OnLoadTypeHandler); }
public void InterfacePassTest() { var f1 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(Ia) } }; var f2 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(Ib) } }; var f3 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(Ia), typeof(Ib) } }; var f4 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(Ia), typeof(Ib) } }; Assert.IsTrue(f1.GetFilter()(typeof(A))); Assert.IsTrue(f2.GetFilter()(typeof(A))); Assert.IsTrue(f3.GetFilter()(typeof(A))); Assert.IsTrue(f4.GetFilter()(typeof(A))); }
static PropertySyncHelper() { // Create the dictionary cache that maps a PropertySyncHandler's handled type // to the type of the PropertySyncHandler itself var typeFilterCreator = new TypeFilterCreator { IsClass = true, IsAbstract = false, Attributes = new Type[] { typeof(PropertySyncHandlerAttribute) } }; var typeFilter = typeFilterCreator.GetFilter(); foreach (var type in TypeHelper.AllTypes().Where(typeFilter)) { // Look for classes that inherit the PropertySyncHandlerAttribute var attribs = type.GetCustomAttributes(typeof(PropertySyncHandlerAttribute), true); if (attribs.Length < 1) { continue; } if (attribs.Length > 1) { const string errmsg = "Multiple PropertySyncHandlerAttributes found on type `{0}`"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, type); } Debug.Fail(string.Format(errmsg, type)); throw new TypeLoadException(string.Format(errmsg, type)); } // Grab the attribute so we can find out what type this class handles var attrib = (PropertySyncHandlerAttribute)attribs[0]; // Make sure the handler doesn't already exist if (_propertySyncTypes.ContainsKey(attrib.HandledType)) { const string errmsg = "Duplicate PropertySync implementations for type `{0}`. Implementations: `{1}` and `{2}`."; var existingPST = _propertySyncTypes[attrib.HandledType]; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, attrib.HandledType, existingPST, type); } Debug.Fail(string.Format(errmsg, attrib.HandledType, existingPST, type)); continue; } // Store the handled type _propertySyncTypes.Add(attrib.HandledType, type); // If the type can be made nullable, also store the nullable type if (attrib.HandledType.IsValueType && !attrib.HandledType.IsGenericType) { try { var nullableType = typeof(Nullable <>).MakeGenericType(attrib.HandledType); // Make sure the key doesn't already exist if (!_propertySyncTypes.ContainsKey(nullableType)) { var psType = typeof(PropertySyncNullable <>).MakeGenericType(attrib.HandledType); _propertySyncTypes.Add(nullableType, psType); } } catch (Exception ex) { const string errmsg = "Failed to create nullable type from `{0}`. Reason: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, attrib.HandledType, ex); } Debug.Fail(string.Format(errmsg, attrib.HandledType, ex)); } } } }
public void InterfaceFailTest() { var f1 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ia) } }; var f2 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ib) } }; var f3 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ia), typeof(Ib) } }; var f4 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = true, Interfaces = new Type[] { typeof(IEnumerable), typeof(Ia), typeof(Ib) } }; var f5 = new TypeFilterCreator { RequireInterfaces = true, MatchAllInterfaces = false, Interfaces = new Type[] { typeof(IEnumerable) } }; Assert.Throws<TypeFilterException>(() => f1.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f2.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f3.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f4.GetFilter()(typeof(A))); Assert.Throws<TypeFilterException>(() => f5.GetFilter()(typeof(A))); }