public ExperimentalFlag( string name, Guid namespaceId, bool enabled = false, IExperimentalFlag parentFlag = null ) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } mParentFlag = parentFlag; mGuid = GuidUtils.CreateNamed(namespaceId, name); mName = name; mEnabled = enabled; }
private void RegisterPropertiesAndAliases() { PropertyInfo existingFlag; var experimentsType = typeof(TExperiments); var aliasProperties = new List <(PropertyInfo property, ExperimentalFlagAliasAttribute aliasAttribute)>(); var properties = experimentsType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var flagProperties = properties .Where(property => typeof(IExperimentalFlag).IsAssignableFrom(property.PropertyType)) .ToList(); flagProperties.ForEach( property => { var aliasAttribute = property?.GetCustomAttribute <ExperimentalFlagAliasAttribute>(true); if (aliasAttribute != null) { aliasProperties.Add((property, aliasAttribute)); return; } if (property?.DeclaringType == null) { throw new InvalidOperationException(); } var flagName = property.Name.ToLowerInvariant(); var namespaceId = GetNamespaceIdFor(property.DeclaringType); var flagId = GuidUtils.CreateNamed(namespaceId, property.Name); if (TryGetProperty(flagName, out existingFlag)) { throw new InvalidOperationException( $@"Tried to add a flag with name '{flagName}' in '{property.DeclaringType?.FullName}' but there is already a flag with that name defined in '{existingFlag.DeclaringType?.FullName}'." ); } mFlagsById.Add(flagId, property); mFlagsByName.Add(flagName, property); } ); aliasProperties.ForEach( pair => { var(property, aliasAttribute) = pair; var aliasName = property.Name.ToLowerInvariant(); var targetName = aliasAttribute.Of; var alias = new ExperimentalFlagAlias(this, targetName, aliasName); property.SetValue(this, alias); if (TryGetProperty(aliasName, out existingFlag)) { throw new InvalidOperationException( $@"Tried to add an alias with name '{aliasName}' in '{property.DeclaringType?.FullName}' but there is already a flag with that name defined in '{existingFlag.DeclaringType?.FullName}'." ); } mFlagsByName.Add(aliasName, property); } ); }