Skip to content

Commit beb4583

Browse files
authored
Merge pull request #15 from itn3000/fix-alias-skip
Fix CommandAttribute's name not working
2 parents 178ede0 + e4f3656 commit beb4583

File tree

2 files changed

+76
-21
lines changed

2 files changed

+76
-21
lines changed

src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,43 +200,51 @@ static List<Type> GetBatchTypes()
200200

201201
static (Type, MethodInfo) GetTypeFromAssemblies(string arg0)
202202
{
203-
var split = arg0.Split('.');
204-
if (split.Length != 2)
205-
{
206-
return (null, null);
207-
}
208-
var typeName = split[0];
209-
var methodName = split[1];
210-
211203
var batchBaseTypes = GetBatchTypes();
212204
if (batchBaseTypes == null)
213205
{
214206
return (null, null);
215207
}
216208

209+
var split = arg0.Split('.');
217210
Type foundType = null;
218-
foreach (var item in batchBaseTypes)
211+
MethodInfo foundMethod = null;
212+
foreach (var baseType in batchBaseTypes)
219213
{
220-
if (item.Name == typeName)
214+
bool isFound = false;
215+
foreach (var (method, cmdattr) in baseType.GetMethods().
216+
Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
221217
{
222-
if (foundType != null)
218+
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
223219
{
224-
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + item.FullName);
220+
if(foundType != null && foundMethod != null)
221+
{
222+
throw new InvalidOperationException($"Duplicate BatchBase Command name is not allowed, {foundType.FullName}.{foundMethod.Name} and {baseType.FullName}.{method.Name}");
223+
}
224+
foundType = baseType;
225+
foundMethod = method;
226+
isFound = true;
225227
}
226-
foundType = item;
227228
}
228-
}
229-
230-
if (foundType != null)
231-
{
232-
var method = foundType.GetMethod(methodName);
233-
if (method != null)
229+
if (!isFound && split.Length == 2)
234230
{
235-
return (foundType, method);
231+
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
232+
{
233+
if (foundType != null)
234+
{
235+
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName);
236+
}
237+
foundType = baseType;
238+
foundMethod = baseType.GetMethod(split[1]);
239+
}
236240
}
237241
}
238-
242+
if(foundType != null && foundMethod != null)
243+
{
244+
return (foundType, foundMethod);
245+
}
239246
return (null, null);
247+
240248
}
241249
}
242250
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace MicroBatchFramework.Tests
11+
{
12+
public class CommandAttributeTest
13+
{
14+
class CommandAttributeTestCommand : BatchBase
15+
{
16+
ResultContainer _Result;
17+
public CommandAttributeTestCommand(ResultContainer r)
18+
{
19+
_Result = r;
20+
}
21+
[Command("test")]
22+
public void TestCommand(int value)
23+
{
24+
_Result.X = value;
25+
}
26+
}
27+
class ResultContainer
28+
{
29+
public int X;
30+
}
31+
[Fact]
32+
public async Task TestCommandName()
33+
{
34+
var host = BatchHost.CreateDefaultBuilder()
35+
.ConfigureServices((c, services) =>
36+
{
37+
services.AddSingleton<ResultContainer>();
38+
})
39+
.UseBatchEngine<CommandAttributeTestCommand>(new string[]{ "test", "-value", "1" })
40+
.Build();
41+
var result = host.Services.GetService<ResultContainer>();
42+
await host.RunAsync();
43+
result.X.Should().Be(1);
44+
}
45+
46+
}
47+
}

0 commit comments

Comments
 (0)