Skip to content

Commit 5461a68

Browse files
committed
Simplify resource key retrieval for experimental types
1 parent 2bf697c commit 5461a68

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Rubberduck.Core/UI/Settings/GeneralSettingsViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Rubberduck.VBEditor.VbeRuntime.Settings;
1212
using Rubberduck.Resources;
1313
using Rubberduck.Resources.Settings;
14+
using Rubberduck.Parsing.Common;
1415

1516
namespace Rubberduck.UI.Settings
1617
{
@@ -307,10 +308,15 @@ private void TransferSettingsToView(IGeneralSettings general, IHotkeySettings ho
307308
_selectedLogLevel = LogLevels.First(l => l.Ordinal == general.MinimumLogLevel);
308309

309310
ExperimentalFeatures = _experimentalFeatureTypes
310-
// extract the resource key to use
311-
.SelectMany(s => s.CustomAttributes.Where(a => a.ConstructorArguments.Any()).Select(a => (string)a.ConstructorArguments.First().Value))
311+
.Select(type => {
312+
var attribute = (ExperimentalAttribute) type.GetCustomAttributes(typeof(ExperimentalAttribute), false).First();
313+
return attribute.Resource;
314+
})
312315
.Distinct()
313-
.Select(s => new ExperimentalFeature { IsEnabled = general.EnableExperimentalFeatures.SingleOrDefault(d => d.Key == s)?.IsEnabled ?? false, Key = s })
316+
.Select(resourceKey => new ExperimentalFeature {
317+
IsEnabled = general.EnableExperimentalFeatures.SingleOrDefault(d => d.Key == resourceKey)?.IsEnabled ?? false,
318+
Key = resourceKey
319+
})
314320
.ToList();
315321
}
316322

Rubberduck.Main/Root/TypeExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ internal static bool NotDisabledOrExperimental(this Type type, GeneralSettings i
1414

1515
internal static bool NotExperimental(this Type type, GeneralSettings initialSettings)
1616
{
17-
var attribute = type.CustomAttributes.FirstOrDefault(f => f.AttributeType == typeof(ExperimentalAttribute));
18-
var ctorArg = attribute?.ConstructorArguments.Any() == true ? (string)attribute.ConstructorArguments.First().Value : string.Empty;
17+
var attribute = type.GetCustomAttributes(typeof(ExperimentalAttribute), false).FirstOrDefault();
18+
var resourceKey = (attribute as ExperimentalAttribute)?.Resource ?? string.Empty;
1919

20-
return attribute == null || initialSettings.EnableExperimentalFeatures.Any(a => a.Key == ctorArg && a.IsEnabled);
20+
return attribute == null || initialSettings.EnableExperimentalFeatures.Any(a => a.Key == resourceKey && a.IsEnabled);
2121
}
2222

2323
internal static bool NotDisabled(this Type type)

Rubberduck.Parsing/Common/ExperimentalAttribute.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
namespace Rubberduck.Parsing.Common
44
{
5+
/// <summary>
6+
/// Marks a class as belonging to an experimental feature.
7+
/// The feature is identified by the resource key in <see cref="Resources.Experimentals.ExperimentalNames"/> that describes it to the user.
8+
/// Features marked as experimental are excluded from IoC configuration unless the user has explicitly enabled them.
9+
/// <para>
10+
/// See also: <seealso cref="DisabledAttribute"/>
11+
/// </para>
12+
/// </summary>
513
[AttributeUsage(AttributeTargets.Class)]
614
public class ExperimentalAttribute : Attribute
715
{
@@ -11,7 +19,7 @@ public ExperimentalAttribute(string resource)
1119
}
1220

1321
/// <summary>
14-
/// Resource key to look up in <see cref="Rubberduck.Resources.Experimental.ExperimentalNames"/>.
22+
/// Resource key to look up in <see cref="Resources.Experimentals.ExperimentalNames"/>.
1523
/// Also serves as a unique identifier to distinguish experimental features from one another.
1624
/// </summary>
1725
public string Resource { get; }

0 commit comments

Comments
 (0)