|
|
|
|
|
|
using System.Collections; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using UnityEngine; |
|
|
|
public class TypeMapper<TFrom, TTo> : IEnumerable<TypeMapping> |
|
|
|
public class TypeMapper : IEnumerable<TypeMapping> |
|
|
|
readonly Type m_FromBaseType; |
|
|
|
readonly Type m_ToBaseType; |
|
|
|
public TypeMapper(Type fallbackType = null) |
|
|
|
public TypeMapper(Type fromBaseType = null, Type toBaseType = null, Type fallbackType = null) |
|
|
|
if (fallbackType != null && !(fallbackType.IsSubclassOf(typeof(TFrom)) || fallbackType.GetInterfaces().Contains(typeof(TFrom)))) |
|
|
|
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fallbackType.Name, typeof(TFrom).Name), "fallbackType"); |
|
|
|
if (fallbackType != null && toBaseType != null && !fallbackType.IsSubclassOf(toBaseType)) |
|
|
|
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fallbackType.Name, toBaseType.Name), "fallbackType"); |
|
|
|
m_FromBaseType = fromBaseType ?? typeof(object); |
|
|
|
m_ToBaseType = toBaseType; |
|
|
|
m_FallbackType = fallbackType; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Add(Type fromType, Type toType) |
|
|
|
{ |
|
|
|
if (!fromType.IsSubclassOf(typeof(TFrom)) && !fromType.GetInterfaces().Contains(typeof(TFrom))) |
|
|
|
if (m_FromBaseType != typeof(object) && !fromType.IsSubclassOf(m_FromBaseType) && !fromType.GetInterfaces().Contains(m_FromBaseType)) |
|
|
|
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fromType.Name, typeof(TFrom).Name), "fromType"); |
|
|
|
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fromType.Name, m_FromBaseType.Name), "fromType"); |
|
|
|
if (!toType.IsSubclassOf(typeof(TTo))) |
|
|
|
if (m_ToBaseType != null && !toType.IsSubclassOf(m_ToBaseType)) |
|
|
|
throw new ArgumentException(string.Format("{0} does not derive from {1}.", toType.Name, typeof(TTo).Name), "toType"); |
|
|
|
throw new ArgumentException(string.Format("{0} does not derive from {1}.", toType.Name, m_ToBaseType.Name), "toType"); |
|
|
|
} |
|
|
|
|
|
|
|
m_Mappings[fromType] = toType; |
|
|
|
|
|
|
{ |
|
|
|
Type toType = null; |
|
|
|
|
|
|
|
while (toType == null && fromType != null && fromType != typeof(TFrom)) |
|
|
|
while (toType == null && fromType != null && fromType != m_FromBaseType) |
|
|
|
{ |
|
|
|
if (!m_Mappings.TryGetValue(fromType, out toType)) |
|
|
|
fromType = fromType.BaseType; |
|
|
|