Skip to content

Commit 9e4c9fe

Browse files
Qonstruktghuntley
authored andcommitted
feature: improved exception message when there are multiple resource IDs with the same name (#1302)
1 parent 2379efc commit 9e4c9fe

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/ReactiveUI/Android/ControlFetcherMixin.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,22 @@ static ControlFetcherMixin()
3030
var assm = AppDomain.CurrentDomain.GetAssemblies()[1];
3131
var resources = assm.GetModules().SelectMany(x => x.GetTypes()).First(x => x.Name == "Resource");
3232

33-
controlIds = resources.GetNestedType("Id").GetFields()
34-
.Where(x => x.FieldType == typeof(int))
35-
.ToDictionary(k => k.Name.ToLowerInvariant(), v => (int)v.GetRawConstantValue());
33+
try {
34+
controlIds = resources.GetNestedType("Id").GetFields()
35+
.Where(x => x.FieldType == typeof(int))
36+
.ToDictionary(k => k.Name.ToLowerInvariant(), v => (int)v.GetRawConstantValue());
37+
} catch (ArgumentException argumentException) {
38+
var duplicates = resources.GetNestedType("Id").GetFields()
39+
.Where(x => x.FieldType == typeof(int))
40+
.GroupBy(k => k.Name.ToLowerInvariant())
41+
.Where(g => g.Count() > 1)
42+
.Select(g => "{ " + string.Join(" = ", g.Select(v => v.Name)) + " }");
43+
44+
if (duplicates.Any())
45+
throw new InvalidOperationException("You're using multiple resource ID's with the same name but with different casings which isn't allowed for WireUpControls: " + string.Join(", ", duplicates), argumentException);
46+
47+
throw argumentException;
48+
}
3649

3750
var type = typeof(ControlFetcherMixin);
3851
getControlActivity = type.GetMethod("GetControl", new[] { typeof(Activity), typeof(string) });

0 commit comments

Comments
 (0)