/// <summary> /// Allows the types and attributes of plugin export parameters /// to be enumerated and simultaneously validated. /// </summary> /// <param name="typeVisitors"> /// A dictionary of handlers for the parameter types. /// </param> /// <param name="attributeVisitors"> /// A dictionary of handlers for the parameter attributes. /// </param> protected void VisitParameters( IReadOnlyDictionary <Type, Action <int> > typeVisitors, IReadOnlyDictionary <Type, Action <int, object> > attributeVisitors) { foreach (var param in Method.GetParameters() .Select((param, index) => new { Index = index, Type = param.ParameterType, Attributes = param.GetCustomAttributes() })) { var isValidAPIType = NvimTypesMap.IsValidType(param.Type); var isValidPluginType = typeVisitors.TryGetValue(param.Type, out var visitor); if (!isValidAPIType && !isValidPluginType) { throw new Exception( $"Plugin export has invalid parameter type \"{param.Type.Name}\""); } if (visitor != null // If there is not a visitor for the specific type, // try to use the "object" visitor as a default || typeVisitors.TryGetValue(typeof(object), out visitor)) { visitor(param.Index); } foreach (var attribute in param.Attributes) { if (attributeVisitors.TryGetValue(attribute.GetType(), out var attributeVisitor)) { attributeVisitor(param.Index, attribute); } } } }
public void TestNvimTypeValidation(Type type, bool shouldBeValid) { Assert.AreEqual(shouldBeValid, NvimTypesMap.IsValidType(type)); }