@@ -85,27 +85,52 @@ public void SetAutoRecycle(bool autoRecycle)
8585 /// 根据组件类型获取对应的IComponentAgent
8686 /// </summary>
8787 /// <typeparam name="T">组件类型</typeparam>
88+ /// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
8889 /// <returns></returns>
89- public async Task < T > GetComponentAgent < T > ( ) where T : IComponentAgent
90+ public async Task < T > GetComponentAgent < T > ( bool isNew = true ) where T : IComponentAgent
9091 {
91- return ( T ) await GetComponentAgent ( typeof ( T ) ) ;
92+ return ( T ) await GetComponentAgent ( typeof ( T ) , isNew ) ;
9293 }
9394
9495 /// <summary>
9596 /// 根据组件类型获取对应的IComponentAgent
9697 /// </summary>
9798 /// <param name="agentType">代理类型</param>
99+ /// <param name="isNew">是否当获取为空的时候默认创建,默认值为true</param>
98100 /// <returns></returns>
99- public async Task < IComponentAgent > GetComponentAgent ( Type agentType )
101+ public async Task < IComponentAgent > GetComponentAgent ( Type agentType , bool isNew = true )
100102 {
101103 var compType = agentType . BaseType . GetGenericArguments ( ) [ 0 ] ;
102- var comp = _componentsMap . GetOrAdd ( compType , GetOrAddFactory ) ;
103- var agent = comp . GetAgent ( agentType ) ;
104- if ( ! comp . IsActive )
104+ IComponentAgent agent ;
105+ if ( isNew )
106+ {
107+ var comp = _componentsMap . GetOrAdd ( compType , GetOrAddFactory ) ;
108+ agent = comp . GetAgent ( agentType ) ;
109+ if ( ! comp . IsActive )
110+ {
111+ async Task Worker ( )
112+ {
113+ await comp . Active ( ) ;
114+ agent . Active ( ) ;
115+ }
116+
117+ await SendAsyncWithoutCheck ( Worker ) ;
118+ }
119+
120+ return agent ;
121+ }
122+
123+ if ( ! _componentsMap . TryGetValue ( compType , out var component ) )
124+ {
125+ return default ;
126+ }
127+
128+ agent = component . GetAgent ( agentType ) ;
129+ if ( ! component . IsActive )
105130 {
106131 async Task Worker ( )
107132 {
108- await comp . Active ( ) ;
133+ await component . Active ( ) ;
109134 agent . Active ( ) ;
110135 }
111136
0 commit comments