Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: improved exception message when there are multiple resource IDs with the same name #1302

Merged
merged 2 commits into from
Mar 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/ReactiveUI/Android/ControlFetcherMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,22 @@ static ControlFetcherMixin()
var assm = AppDomain.CurrentDomain.GetAssemblies()[1];
var resources = assm.GetModules().SelectMany(x => x.GetTypes()).First(x => x.Name == "Resource");

controlIds = resources.GetNestedType("Id").GetFields()
.Where(x => x.FieldType == typeof(int))
.ToDictionary(k => k.Name.ToLowerInvariant(), v => (int)v.GetRawConstantValue());
try {
controlIds = resources.GetNestedType("Id").GetFields()
.Where(x => x.FieldType == typeof(int))
.ToDictionary(k => k.Name.ToLowerInvariant(), v => (int)v.GetRawConstantValue());
} catch (ArgumentException argumentException) {
var duplicates = resources.GetNestedType("Id").GetFields()
.Where(x => x.FieldType == typeof(int))
.GroupBy(k => k.Name.ToLowerInvariant())
.Where(g => g.Count() > 1)
.Select(g => "{ " + string.Join(" = ", g.Select(v => v.Name)) + " }");

if (duplicates.Any())
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);

throw argumentException;
}

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