public static void RegisterFormScripts(IDynamicScriptManager scriptManager, ITypeSource typeSource, IPropertyItemProvider propertyProvider, IServiceProvider serviceProvider) { if (scriptManager == null) { throw new ArgumentNullException(nameof(scriptManager)); } if (typeSource == null) { throw new ArgumentNullException(nameof(typeSource)); } if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } var scripts = new List <Func <string> >(); foreach (var type in typeSource.GetTypesWithAttribute(typeof(FormScriptAttribute))) { var attr = type.GetCustomAttribute <FormScriptAttribute>(); var key = attr.Key ?? type.FullName; var script = new FormScript(key, type, propertyProvider, serviceProvider); scriptManager.Register(script); scripts.Add(script.GetScript); } scriptManager.Register("FormBundle", new ConcatenatedScript(scripts)); }
public static IDictionary<string, HashSet<string>> GetImplicitPermissions(IMemoryCache memoryCache, ITypeSource typeSource) { if (memoryCache is null) throw new ArgumentNullException(nameof(memoryCache)); if (typeSource is null) throw new ArgumentNullException(nameof(typeSource)); return memoryCache.Get<IDictionary<string, HashSet<string>>>("ImplicitPermissions", TimeSpan.Zero, () => { var result = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase); Action<Type> addFrom = null; addFrom = (type) => { foreach (var member in type.GetFields(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic)) { if (member.FieldType != typeof(String)) continue; var key = member.GetValue(null) as string; if (key == null) continue; foreach (var attr in member.GetCustomAttributes<ImplicitPermissionAttribute>()) { HashSet<string> list; if (!result.TryGetValue(key, out list)) { list = new HashSet<string>(StringComparer.OrdinalIgnoreCase); result[key] = list; } list.Add(attr.Value); } } foreach (var nested in type.GetNestedTypes(BindingFlags.Public | BindingFlags.DeclaredOnly)) addFrom(nested); }; foreach (var type in typeSource.GetTypesWithAttribute( typeof(NestedPermissionKeysAttribute))) { addFrom(type); } return result; }); }
/// <summary> /// Adds translations from static nested local text classes marked with NestedLocalTextAttribute. /// </summary> public static void AddNestedTexts(this ILocalTextRegistry registry, ITypeSource typeSource) { if (typeSource == null) { throw new ArgumentNullException(nameof(typeSource)); } foreach (var type in typeSource.GetTypesWithAttribute(typeof(NestedLocalTextsAttribute))) { var attr = type.GetCustomAttribute <NestedLocalTextsAttribute>(); if (attr != null) { Initialize(registry, type, attr.LanguageID ?? LocalText.InvariantLanguageID, attr.Prefix ?? ""); } } }
/// <summary> /// Gets permission keys and adds texts if any from static nested permission key /// classes marked with NestedPermissionKeys attribute. /// </summary> public static HashSet <string> AddNestedPermissions(this ILocalTextRegistry registry, ITypeSource typeSource) { if (typeSource == null) { throw new ArgumentNullException(nameof(typeSource)); } var permissions = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var type in typeSource.GetTypesWithAttribute(typeof(NestedPermissionKeysAttribute))) { var attr = type.GetCustomAttribute <NestedPermissionKeysAttribute>(); if (attr != null) { AddKeysFrom(permissions, registry, type, attr.LanguageID ?? LocalText.InvariantLanguageID); } } return(permissions); }
public static void RegisterLookupScripts(IDynamicScriptManager scriptManager, ITypeSource typeSource, IServiceProvider serviceProvider) { if (scriptManager == null) { throw new ArgumentNullException(nameof(scriptManager)); } if (typeSource == null) { throw new ArgumentNullException(nameof(typeSource)); } if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } var registeredType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase); foreach (var type in typeSource.GetTypesWithAttribute(typeof(LookupScriptAttribute))) { var attr = type.GetCustomAttribute <LookupScriptAttribute>(); LookupScript script; if (typeof(IRow).IsAssignableFrom(type)) { if (attr.LookupType == null) { script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider, typeof(RowLookupScript <>).MakeGenericType(type)); } else if (attr.LookupType.IsGenericType) { script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider, attr.LookupType.MakeGenericType(type)); } else if (attr.LookupType.GetCustomAttribute <LookupScriptAttribute>() == null) { script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider, attr.LookupType); } else { // lookup script type already has a LookupScript attribute, // so it's dynamic script will be generated on itself continue; } } else if (!typeof(LookupScript).IsAssignableFrom(type) || type.IsAbstract) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Type {0} can't be registered as a lookup script!", type.FullName)); } else { script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider, type); } script.LookupKey = attr.Key ?? LookupScriptAttribute.AutoLookupKeyFor(type); if (registeredType.TryGetValue(script.LookupKey, out Type otherType)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Types {0} and {1} has the same lookup key (\"{2}\"). " + "\r\n\r\nPlease remove LookupScript attribute from one of them or change the lookup key!", type.FullName, otherType.FullName, script.LookupKey)); } registeredType[script.LookupKey] = type; if (attr.Permission != null) { script.Permission = attr.Permission; } if (attr.Expiration != 0) { script.Expiration = TimeSpan.FromSeconds(attr.Expiration); } scriptManager.Register(script.ScriptName, script); } }