EventManager : Generic Action Contraintes
Bonjour messieurs,
Je rencontre une problématique sur un manageur d'évênement sur lequel je travaille actuellement, actuellement il n'y a pas de contraintes pour définir qu'un type générique est une delegate, deplus il n'y a pas d'interface permettant d'avoir les opérateurs +=, -=.
Par conséquent je cherche à faire compiler cette ligne : this.events[EnumHelper.GetIndex<EnumType>(enumeration)] += action;
Auriez-vous des idées de comment la faire compiler ?
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| using System.Collections.Generic;
using System;
public static class EnumHelper
{
public static int GetIndex<EnumType>(EnumType enumeration) where EnumType : struct, IConvertible
{
return Convert.ToInt32(enumeration);
}
public static int Count<EnumType>() where EnumType : struct, IConvertible
{
return System.Enum.GetValues(typeof(EnumType)).Length;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| using System.Collections.Generic;
using System;
public abstract class AEventManager<EventType, EnumType>
where EnumType : struct, IConvertible
where EventType : class, Action<int>
{
protected EventType[] events;
public AEventManager()
{
if (!typeof(EnumType).IsEnum)
throw new ArgumentException("EnumType must be an enumerated type");
this.events = new EventType[EnumHelper.Count<EnumType>()];
}
protected EventType GetEvent(EnumType enumeration)
{
return this.events[EnumHelper.GetIndex<EnumType>(enumeration)];
}
public void SubcribeToEvent(EnumType enumeration, EventType action)
{
this.events[EnumHelper.GetIndex<EnumType>(enumeration)] += action;
}
//Unsubscribe, abstract callDelegate(EnumType enumeration, ... templateVarags)..
} |
Code:
1 2 3
| public class EventManager : AEventManager<Action<int>, EEvent>
{
} |