encompass-cs/encompass-cs/Attributes/Writes.cs

41 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class Writes : Attribute
{
public readonly HashSet<Type> writeTypes = new HashSet<Type>();
public Dictionary<Type, int> priorities = new Dictionary<Type, int>();
public Writes(params Type[] writeTypes)
{
foreach (var writeType in writeTypes)
{
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
}
this.writeTypes.Add(typeof(ComponentWriteMessage<>).MakeGenericType(writeType));
}
}
public Writes(Type writeType, int priority)
{
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent));
if (!isComponent)
{
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
}
this.writeTypes.Add(typeof(ComponentWriteMessage<>).MakeGenericType(writeType));
this.priorities.Add(writeType, priority);
}
}
}