public VaultViewFactory(Type t_view) { t_view.HasAttr<VaultViewAttribute>().AssertTrue(); t_view.HasAttr<VaultViewLocAttribute>().AssertTrue(); _factory = () => (IVaultView)Activator.CreateInstance(t_view); _sample = _factory(); }
public ContentType(Type type) { Type = type.AssertNotNull(); Type.HasAttr<ContentTypeAttribute>().AssertTrue(); (Type.Attr<ContentTypeAttribute>().Token != "binary").AssertTrue(); Type.HasAttr<ContentTypeLocAttribute>().AssertTrue(); Type.GetConstructor(typeof(IBranch).MkArray()); Type.GetConstructor(typeof(IValue).MkArray()); }
/// <summary> /// Creates a command line arguments definition and infers things like Arguments, Actions, etc. from the type's metadata. /// </summary> /// <param name="t">The argument scaffold type used to infer the definition</param> public CommandLineArgumentsDefinition(Type t) : this() { ArgumentScaffoldType = t; ExceptionBehavior = t.HasAttr<ArgExceptionBehavior>() ? t.Attr<ArgExceptionBehavior>() : new ArgExceptionBehavior(); Arguments.AddRange(FindCommandLineArguments(t)); Actions.AddRange(FindCommandLineActions(t)); Metadata.AddRange(t.Attrs<IArgMetadata>().AssertAreAllInstanceOf<ICommandLineArgumentsDefinitionMetadata>()); }
internal static IDictionary<string, RouteMember> GetQueryProperties(Type requestType) { var result = new Dictionary<string, RouteMember>(StringExtensions.InvariantComparerIgnoreCase()); var hasDataContract = requestType.HasAttr<DataContractAttribute>(); foreach (var propertyInfo in requestType.GetPublicProperties()) { var propertyName = propertyInfo.Name; if (!propertyInfo.CanRead) continue; if (hasDataContract) { if (!propertyInfo.IsDefined(typeof(DataMemberAttribute), true)) continue; var dataMember = propertyInfo.FirstAttribute<DataMemberAttribute>(); if (!string.IsNullOrEmpty(dataMember.Name)) { propertyName = dataMember.Name; } } else { if (propertyInfo.IsDefined(typeof(IgnoreDataMemberAttribute), true)) continue; } result[propertyName.ToCamelCase()] = new PropertyRouteMember(propertyInfo); } if (JsConfig.IncludePublicFields) { foreach (var fieldInfo in requestType.GetPublicFields()) { var fieldName = fieldInfo.Name; if (fieldInfo.IsDefined(typeof(IgnoreDataMemberAttribute), true)) continue; result[fieldName.ToCamelCase()] = new FieldRouteMember(fieldInfo); } } return result; }
private List<CommandLineAction> FindCommandLineActions(Type t) { var knownAliases = new List<string>(); foreach (var argument in Arguments) knownAliases.AddRange(argument.Aliases); BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public; var actions = (from p in t.GetProperties(flags) where CommandLineAction.IsActionImplementation(p) select CommandLineAction.Create(p, knownAliases)).ToList(); List<Type> typesToSearchForActions = new List<Type>() { t }; if(t.HasAttr<ArgActionResolver>()) { typesToSearchForActions.AddRange(t.Attr<ArgActionResolver>().ResolveActionTypes()); } typesToSearchForActions.AddRange(t.Attrs<ArgActionType>().Select(aat => aat.ActionType)); foreach (var typeToSearch in typesToSearchForActions) { var requireStatic = typeToSearch != t; foreach (var method in typeToSearch.GetMethods(flags).Where(m => CommandLineAction.IsActionImplementation(m))) { if(requireStatic && method.IsStatic == false) { throw new InvalidArgDefinitionException("The method "+method.DeclaringType.FullName+"."+method.Name+" must be static because it has been imported using [ArgActionType] or [ArgActions]"); } var action = CommandLineAction.Create(method, knownAliases.ToList()); var matchingPropertyBasedAction = actions.Where(a => a.Aliases.First() == action.Aliases.First()).SingleOrDefault(); if (matchingPropertyBasedAction != null) continue; actions.Add(action); } } return actions; }
private static void ParseModel(IDictionary<string, SwaggerModel> models, Type modelType) { if (IsSwaggerScalarType(modelType)) return; var modelId = modelType.Name; if (models.ContainsKey(modelId)) return; var model = new SwaggerModel { Id = modelId, Properties = new Dictionary<string, ModelProperty>() }; models[model.Id] = model; var hasDataContract = modelType.HasAttr<DataContractAttribute>(); foreach (var prop in modelType.GetProperties()) { DataMemberAttribute dataMemberAttribute = null; if (hasDataContract) { dataMemberAttribute = prop.GetDataMember(); if (dataMemberAttribute == null) { continue; } } else if (prop.IsDefined(typeof(IgnoreDataMemberAttribute), true)) { continue; } var allApiDocAttributes = prop .GetCustomAttributes(typeof(ApiMemberAttribute), true) .OfType<ApiMemberAttribute>() .Where(attr => prop.Name.Equals(attr.Name, StringComparison.InvariantCultureIgnoreCase)) .ToList(); var apiDoc = allApiDocAttributes.FirstOrDefault(attr => attr.ParameterType == "body"); if (allApiDocAttributes.Any() && apiDoc == null) continue; var propertyType = prop.PropertyType; var isRequired = dataMemberAttribute == null ? !IsNullable(propertyType) : dataMemberAttribute.IsRequired; var modelProp = new ModelProperty { Type = GetSwaggerTypeName(propertyType), Required = isRequired }; if (IsListType(propertyType)) { modelProp.Type = SwaggerType.Array; var listItemType = GetListElementType(propertyType); modelProp.Items = new Dictionary<string, string> { { IsSwaggerScalarType(listItemType) ? "type" : "$ref", GetSwaggerTypeName(listItemType) } }; ParseModel(models, listItemType); } else if ((Nullable.GetUnderlyingType(propertyType) ?? propertyType).IsEnum) { var enumType = Nullable.GetUnderlyingType(propertyType) ?? propertyType; if (enumType.IsNumericType()) { var underlyingType = Enum.GetUnderlyingType(enumType); modelProp.Type = GetSwaggerTypeName(underlyingType); modelProp.AllowableValues = new ParameterAllowableValues { Values = GetNumericValues(enumType, underlyingType).ToArray(), ValueType = "LIST" }; } else { modelProp.Type = SwaggerType.String; modelProp.AllowableValues = new ParameterAllowableValues { Values = Enum.GetNames(enumType), ValueType = "LIST" }; } } else { ParseModel(models, propertyType); } var descriptionAttr = prop.GetCustomAttributes(typeof(DescriptionAttribute), true).OfType<DescriptionAttribute>().FirstOrDefault(); if (descriptionAttr != null) modelProp.Description = descriptionAttr.Description; if (apiDoc != null) modelProp.Description = apiDoc.Description; var allowableValues = prop.GetCustomAttributes(typeof(ApiAllowableValuesAttribute), true).OfType<ApiAllowableValuesAttribute>().FirstOrDefault(); if (allowableValues != null) modelProp.AllowableValues = GetAllowableValue(allowableValues); model.Properties[GetModelPropertyName(prop, dataMemberAttribute)] = modelProp; } }
private List<CommandLineAction> FindCommandLineActions(Type t) { var knownAliases = new List<string>(); foreach (var argument in Arguments) knownAliases.AddRange(argument.Aliases); BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public; var actions = (from p in t.GetProperties(flags) where CommandLineAction.IsActionImplementation(p) select CommandLineAction.Create(p, knownAliases)).ToList(); if (t.HasAttr<ArgActionType>()) { t = t.Attr<ArgActionType>().ActionType; } foreach (var action in t.GetMethods(flags).Where(m => CommandLineAction.IsActionImplementation(m)).Select(m => CommandLineAction.Create(m, knownAliases.ToList()))) { var matchingPropertyBasedAction = actions.Where(a => a.Aliases.First() == action.Aliases.First()).SingleOrDefault(); if (matchingPropertyBasedAction != null) continue; actions.Add(action); } return actions; }
internal static IDictionary<string, PropertyInfo> GetQueryProperties(Type requestType) { var result = new Dictionary<string, PropertyInfo>(StringComparer.InvariantCultureIgnoreCase); var hasDataContract = requestType.HasAttr<DataContractAttribute>(); foreach (var propertyInfo in requestType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { var propertyName = propertyInfo.Name; if (!propertyInfo.CanRead) continue; if (hasDataContract) { if (!propertyInfo.IsDefined(typeof(DataMemberAttribute), true)) continue; var dataMember = (DataMemberAttribute)propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true)[0]; if (!string.IsNullOrEmpty(dataMember.Name)) { propertyName = dataMember.Name; } } else { if (propertyInfo.IsDefined(typeof(IgnoreDataMemberAttribute), true)) continue; } result[propertyName.ToCamelCase()] = propertyInfo; } return result; }
private static IEnumerable<PropertyInfo> GetRequestProperties(Type requestType) { var hasDataContract = requestType.HasAttr<DataContractAttribute>(); foreach (var propertyInfo in requestType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (!propertyInfo.CanRead) continue; if (hasDataContract) { if (!propertyInfo.IsDefined(typeof(DataMemberAttribute), true)) continue; } else { if (propertyInfo.IsDefined(typeof(IgnoreDataMemberAttribute), true)) continue; } yield return propertyInfo; } }