浏览代码

Add collection initialization support to TypeMapper

/main
Peter Bay Bastian 7 年前
当前提交
5c6dfe96
共有 2 个文件被更改,包括 17 次插入6 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/ScriptableObjectFactory.cs
  2. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/TypeMapper.cs

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/ScriptableObjectFactory.cs


public ScriptableObjectFactory(IEnumerable<TypeMapping> typeMappings)
{
foreach (var typeMapping in typeMappings)
m_TypeMapper.AddMapping(typeMapping);
m_TypeMapper.Add(typeMapping);
}
public TTo Create(TFrom from)

21
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/TypeMapper.cs


using System;
using System.Collections;
public class TypeMapper<TFrom, TTo, TFallback>
public class TypeMapper<TFrom, TTo, TFallback> : IEnumerable<TypeMapping>
private readonly Dictionary<Type, Type> m_Mappings = new Dictionary<Type, Type>();
readonly Dictionary<Type, Type> m_Mappings = new Dictionary<Type, Type>();
public void AddMapping(TypeMapping mapping)
public void Add(TypeMapping mapping)
AddMapping(mapping.fromType, mapping.toType);
Add(mapping.fromType, mapping.toType);
public void AddMapping(Type fromType, Type toType)
public void Add(Type fromType, Type toType)
{
if (!fromType.IsSubclassOf(typeof(TFrom)) && !fromType.GetInterfaces().Contains(typeof(TFrom)))
{

}
return toType ?? typeof(TFallback);
}
public IEnumerator<TypeMapping> GetEnumerator()
{
return m_Mappings.Select(kvp => new TypeMapping(kvp.Key, kvp.Value)).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
正在加载...
取消
保存