现在我有一个数据库,可以按如下方式初始化一大堆卡类型:
private void LoadCardTypes()
{
database.cardTypeList.Clear();
database.cardTypeMap.Clear();
Object[] assets = Resources.LoadAll("ScriptableObjects/CardTypes", typeof(Object)) as Object[];
if (showLog) Debug.Log("Loading CardTypes");
foreach (Object asset in assets)
{
M_CardPlayType type = (M_CardPlayType)asset;
if (type.name.Length == 0)
{
Debug.Log("WARNING: Object Mapped Without Name");
}
CheckIdCollision(type, database.cardMap);
VerifyIdExists(type, CardPlayTypes.SINGLETON);
database.cardTypeList.Add(type);
database.cardTypeMap.Add(type.id, type);
}
}
代码转到的地方M_CardPlayType type = (M_CardPlayType)asset;
我希望将其设为模板类型。
我希望它像
private void LoadCardTypes(Type<T> WhateverType)
{
//other code
WhateverType type = (WhateverType)asset;
//other code
VerifyIdExists(type, WhateverType.SINGLETON);
}
可以这样做吗? (如果有一种技术叫什么叫做奖金问题?)。
更新除1部分以外的工作
新签名
private void LoadNewCardTypes<T,P>(DictionaryOfIntAndSerializableObject map, List<T> list, string path) where T : M_Object where P : ID
我的P给了我麻烦。这是我的ID类
public class ID
{
protected string _className = "ID";
protected static ID _singleton = new ID();
public static ID SINGLETON
{
get { return _singleton; }
}
}
当我尝试从P中获取单身时我得到一个错误(它无法找到它)
P.SINGLETON//doesnt work
你知道为什么我的单身人士不在这种情况下工作吗?
感谢@kailanjian
的最终解决方案private void LoadNewCardTypes<T,P>(DictionaryOfIntAndSerializableObject map, List<T> list, string path) where T : M_Object where P : ID
{
map.Clear();
list.Clear();
Object[] assets = Resources.LoadAll(path, typeof(Object)) as Object[];
if (showLog) Debug.Log("Loading " + path + " types");
foreach (Object asset in assets)
{
T type = (T)asset;
if (type.name.Length == 0)
{
Debug.Log("WARNING: Object Mapped Without Name");
}
CheckIdCollision(type, map);
VerifyIdExists(type, (P)ID.SINGLETON);
}
答案 0 :(得分:1)
尝试这样的事情:
private void LoadCardTypes<T>() where T : ParentClass
{
//other code
T type = (T)asset;
//other code
VerifyIdExists(type, T.SINGLETON);
}
ParentClass
是您保证由您传递的任何类型T
继承/实现的类型。它应该具有SINGLETON
值作为其值之一,以便您能够使用它。