【发布时间】:2015-08-14 04:16:07
【问题描述】:
现在我有一个数据库,它初始化了一大堆卡片类型,如下所示:
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);
}
【问题讨论】:
标签: c# templates generics runtime