C# 泛型约束 xxx<T> Where T:约束(一)
2014-08-06 11:12
453 查看
发现我们游戏的代码中,主程写了很多类似这样的代码:
public static T CreateObject<T>(out int objectId) where T : new() //方法名
public class CSingleton<T> where T : new() //单例类
public T GetControl<T>(string uri, Transform findTrans = null, bool isLog = true) where T : UnityEngine.Object //根据 url和Type 查找UI控件
public T FindControl<T>(string name) where T : Component
public void OpenWindow<T>(params object[] args) where T : CUIBase
public void CallUI<T>(Action<T, object[]> callback, params object[] args) where T : CUIBase
public void LoadTab<T>(string tabPath) where T : CBaseInfo, new() //加载Tab表
使用:ActorInfo actorInfo = CGameSettings.Instance.GetInfo<ActorInfo>(actorId); 解释:[T GetInfo(String id)]传入一个 ID 返回一个Class ,[where T:CBaseInfo]约束这个Class的Type 必须 继承之 CBaseInfo
当然项目中一些插件也是有类似的写法:
public static T CreateAsset<T>() where T : ScriptableObject
public abstract class FingerEventDetector<T> : FingerEventDetector where T : FingerEvent, new()
static public T AddChild<T> (GameObject parent, bool undo) where T : Component
static public T Begin<T> (GameObject go, float duration) where T : UITweener
public static T AddComponent<T>(GameObject go, tk2dSpriteCollectionData spriteCollection, string spriteName) where T : tk2dBaseSprite
public static T CreateObject<T>(out int objectId) where T : new() //方法名
public class CSingleton<T> where T : new() //单例类
public T GetControl<T>(string uri, Transform findTrans = null, bool isLog = true) where T : UnityEngine.Object //根据 url和Type 查找UI控件
public T FindControl<T>(string name) where T : Component
public void OpenWindow<T>(params object[] args) where T : CUIBase
public void CallUI<T>(Action<T, object[]> callback, params object[] args) where T : CUIBase
public void LoadTab<T>(string tabPath) where T : CBaseInfo, new() //加载Tab表
----- 具体使用 -----
定义:public T GetInfo<T>(string id) where T : CBaseInfo使用:ActorInfo actorInfo = CGameSettings.Instance.GetInfo<ActorInfo>(actorId); 解释:[T GetInfo(String id)]传入一个 ID 返回一个Class ,[where T:CBaseInfo]约束这个Class的Type 必须 继承之 CBaseInfo
当然项目中一些插件也是有类似的写法:
FingerGestures
public abstract class ContinuousGestureRecognizer<T> : GestureRecognizerTS<T> where T : ContinuousGesture, new()public static T CreateAsset<T>() where T : ScriptableObject
public abstract class FingerEventDetector<T> : FingerEventDetector where T : FingerEvent, new()
NGUI
static public T[] FindActive<T> () where T : Componentstatic public T AddChild<T> (GameObject parent, bool undo) where T : Component
static public T Begin<T> (GameObject go, float duration) where T : UITweener
TK2D
public static T LoadResourceByName<T>(string guid) where T : UnityEngine.Objectpublic static T AddComponent<T>(GameObject go, tk2dSpriteCollectionData spriteCollection, string spriteName) where T : tk2dBaseSprite
相关文章推荐
- 泛型中字符串转集合,(T)object,typeOf(T),LIst<T> xxx<T>();c# stringtolist
- 泛型类型参数约束 <T> where T : class,new(){}
- c# 泛型<T>类型参数T的约束where
- C# 泛型约束List<T> (转)
- C# 泛型约束 xxx Where T:约束(二)
- C#中string[]数组和list<string>泛型的相互转换
- C#中的where泛型约束中的new()构造函数约束
- C#中的where泛型约束中的new()构造函数约束
- c# 泛型<T>类型参数T的约束where
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- c#泛型类型的约束条件where
- c# 泛型集合Dictionary<TKey,TValue>
- c#中的WHERE语句 ——where(泛型类型约束)
- 读<<CLR via C#>>总结(13) 详谈泛型
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,Queue<T>,Stack<T>等
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,
- C# 泛型类型约束 where
- C#中string[]数组和list<string>泛型的相互转换
- c# 泛型集合Dictionary&lt;TKey,TValue&gt;