C# 泛型约束 xxx<T> Where T:约束(一)
发布时间:2021-05-21 06:59:07 所属栏目:大数据 来源: https://www.jb51.cc
导读:泛型约束 代码举例 发现我们游戏的代码中,主程写了很多类似这样的代码: public static T CreateObjectT( out int objectId) where T : new () // 方法名 单例类 class CSingletonT new () 根据 url和Type 查找UI控件 public T GetControlT( string uri,Tra
泛型约束代码举例发现我们游戏的代码中,主程写了很多类似这样的代码: public static T CreateObject<T>(out int objectId) where T : new() //方法名 单例类 class CSingleton<T> new() 根据 url和Type 查找UI控件 public T GetControl<T>(string uri,Transform findTrans = null,bool isLog = true) where T : UnityEngine.Object public T FindControl<T>(string name) T : Component void OpenWindow<T>(params object[] args) T : CUIBase void CallUI<T>(Action<T,1)">object[]> callback,1)">加载Tab表 void LoadTab<T>(string tabPath) where T : CBaseInfo,1)">new() 具体使用定义: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 插件也使用泛型写法当然项目中使用一些插件也是有类似的写法 FingerGesturesabstract class ContinuousGestureRecognizer<T> : GestureRecognizerTS<T> where T : ContinuousGesture,1)">() static T CreateAsset<T>() T : ScriptableObject class FingerEventDetector<T> : FingerEventDetector where T : FingerEvent,1)">new()NGUIstatic public T[] FindActive<T> () public T AddChild<T> (GameObject parent,1)">bool undo) public T Begin<T> (GameObject go,1)">float duration) where T : UITweenerTK2Dstatic T LoadResourceByName<T>(string guid) static T AddComponent<T>(GameObject go,tk2dSpriteCollectionData spriteCollection,1)">string spriteName) where T : tk2dBaseSprite(编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |