/// <summary> /// Maps all Models implementing IEntity, and not implementing IFluentIgnore and IMapByCode /// </summary> /// <param name="type"> /// The type of model currently being checked /// </param> /// <returns> /// Whether the current type should be automapped or not /// </returns> public override bool ShouldMap(Type type) { return type.GetInterface(typeof(IEntity).FullName) != null && !type.HasAttribute(typeof(FluentIgnoreAttribute)) && !type.HasAttribute(typeof(MapByCodeAttribute)); }
private void Guard(Type sourceType) { bool isSnapshotable = typeof(AggregateRoot).IsAssignableFrom(sourceType) && sourceType.HasAttribute<DynamicSnapshotAttribute>(); if (!isSnapshotable) throw new DynamicSnapshotNotSupportedException() { AggregateType = sourceType }; }
public override string ToQuotedString(Type fieldType, object value) { if (fieldType.HasAttribute<EnumAsIntAttribute>()) { return this.ConvertNumber(fieldType.GetEnumUnderlyingType(), value).ToString(); } if (value is int && !fieldType.IsEnumFlags()) { value = fieldType.GetEnumName(value); } if (fieldType.IsEnum) { var enumValue = DialectProvider.StringSerializer.SerializeToString(value); // Oracle stores empty strings in varchar columns as null so match that behavior here if (enumValue == null) return null; enumValue = DialectProvider.GetQuotedValue(enumValue.Trim('"')); return enumValue == "''" ? "null" : enumValue; } return base.ToQuotedString(fieldType, value); }
public override object ToDbValue(Type fieldType, object value) { if (value is int && !fieldType.IsEnumFlags()) { value = fieldType.GetEnumName(value); } if (fieldType.HasAttribute<EnumAsIntAttribute>()) { if (value is string) { value = Enum.Parse(fieldType, value.ToString()); } return (int) value; } var enumValue = DialectProvider.StringSerializer.SerializeToString(value); // Oracle stores empty strings in varchar columns as null so match that behavior here if (enumValue == null) return null; enumValue = enumValue.Trim('"'); return enumValue == "" ? null : enumValue; }
public bool DoesAggregateSupportsSnapshot(Type aggregateType, Type snapshotType) { bool hasAttribute = aggregateType.HasAttribute<DynamicSnapshotAttribute>(); bool doesSupportSnapshot = snapshotType.Name == SnapshotNameGenerator.Generate(aggregateType); return hasAttribute && doesSupportSnapshot; }
string GetCommandName(Type commandType) { string defaultName = commandType.Name.Replace("Command", ""); string commandName = commandType.HasAttribute<CommandNameAttribute>() ? commandType.GetAttribute<CommandNameAttribute>().Name : defaultName; return commandName; }
static void EnsureBehaviorHasBehaviorsAttribute(Type behaviorType) { if (!behaviorType.HasAttribute<BehaviorsAttribute>()) { throw new SpecificationUsageException( "Behaviors require the BehaviorsAttribute on the type containing the Specifications. Attribute is missing from " + behaviorType.FullName); } }
public static IEnumerable<FieldInfo> GetAll(Type type) { while (type != null) { if (type.HasAttribute<DynamicSnapshotAttribute>()) foreach (var field in GetSnapshotableFields(type)) yield return field; type = type.BaseType; } }
public ControllerInfo(Type type) { this.Type = type; var ms = GetMethods(Type); DefaultAction = GetDefaultAction(ms); Actions = GetActions(ms); IsScaffolding = Type.HasAttribute<ScaffoldingAttribute>(true); ListStyle = GetListStyle(); Name = GetControllerName(); LowerName = Name.ToLower(); Constructor = ClassHelper.GetConstructorDelegate(Type); }
public static BehaviorChain ChainForType(Type type) { if (type.HasAttribute<UrlPatternAttribute>()) { var route = type.GetAttribute<UrlPatternAttribute>().BuildRoute(type); return new RoutedChain(route, type, type); } var chain = BehaviorChain.ForResource(type); chain.IsPartialOnly = true; return chain; }
public static bool TypeIsUnique(Type type) { if (type.HasAttribute<CanBeMultiplesAttribute>()) return false; // If it does not have any non-default constructors if (type.GetConstructors().Any(x => x.GetParameters().Any())) { return false; } if (type.GetProperties().Any(x => x.CanWrite)) { return false; } return true; }
/// <summary> /// This is called from the compile/run appdomain to convert objects within an expression block to a string /// </summary> public string ToStringWithCulture(object objectToConvert) { if ((objectToConvert == null)) { return(""); // KAD temp change //throw new global::System.ArgumentNullException("objectToConvert"); } System.Type t = objectToConvert.GetType(); if (t.IsEnum && t.HasAttribute(typeof(LanguageSpecificAttribute))) { return(GetValueForLanguage(t, objectToConvert)); } else { return(ReflectionHelpers.CultureSpecificToString( t, objectToConvert, this.formatProviderField)); } }
public override string ToQuotedString(Type fieldType, object value) { var isEnumAsInt = fieldType.HasAttribute<EnumAsIntAttribute>(); if (isEnumAsInt) return this.ConvertNumber(Enum.GetUnderlyingType(fieldType), value).ToString(); var isEnumFlags = fieldType.IsEnumFlags() || (!fieldType.IsEnum() && fieldType.IsNumericType()); //i.e. is real int && not Enum long enumValue; if (!isEnumFlags && long.TryParse(value.ToString(), out enumValue)) value = Enum.ToObject(fieldType, enumValue); var enumString = DialectProvider.StringSerializer.SerializeToString(value); if (enumString == null || enumString == "null") enumString = value.ToString(); return !isEnumFlags ? DialectProvider.GetQuotedValue(enumString.Trim('"')) : enumString; }
public object ActivateActivatorlessType(IRyuContainer ryu, Type type) { try { var ctor = type.GetRyuConstructorOrThrow(); var parameters = ctor.GetParameters(); var arguments = parameters.Map(p => ryu.GetOrActivate(p.ParameterType)); var instance = ctor.Invoke(arguments); if (type.HasAttribute<InjectRequiredFields>()) { var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; var fieldsToInitialize = type.GetFields(bindingFlags) .Where(f => f.IsInitOnly && !f.FieldType.IsValueType) .Where(f => f.GetValue(instance) == null); foreach (var field in fieldsToInitialize) { field.SetValue(instance, ryu.GetOrActivate(field.FieldType)); } } return instance; } catch (Exception e) { throw new RyuActivateException(type, e); } }
public override object ToDbValue(Type fieldType, object value) { var isIntEnum = fieldType.IsEnumFlags() || fieldType.HasAttribute<EnumAsIntAttribute>() || (!fieldType.IsEnum() && fieldType.IsNumericType()); //i.e. is real int && not Enum if (isIntEnum && value.GetType().IsEnum()) return Convert.ChangeType(value, Enum.GetUnderlyingType(fieldType)); long enumValue; if (long.TryParse(value.ToString(), out enumValue)) { if (isIntEnum) return enumValue; value = Enum.ToObject(fieldType, enumValue); } var enumString = DialectProvider.StringSerializer.SerializeToString(value); return enumString != null && enumString != "null" ? enumString.Trim('"') : value.ToString(); }
internal static IDictionary<string, RouteMember> GetQueryProperties(Type requestType) { var result = new Dictionary<string, RouteMember>(PclExport.Instance.InvariantComparerIgnoreCase); var hasDataContract = requestType.HasAttribute<DataContractAttribute>(); foreach (var propertyInfo in requestType.GetPublicProperties()) { var propertyName = propertyInfo.Name; if (!propertyInfo.CanRead) continue; if (hasDataContract) { if (!propertyInfo.HasAttribute<DataMemberAttribute>()) continue; var dataMember = propertyInfo.FirstAttribute<DataMemberAttribute>(); if (!string.IsNullOrEmpty(dataMember.Name)) { propertyName = dataMember.Name; } } result[propertyName.ToCamelCase()] = new PropertyRouteMember(propertyInfo) { IgnoreInQueryString = propertyInfo.FirstAttribute<IgnoreDataMemberAttribute>() != null, //but allow in PathInfo }; } if (ServiceStack.Text.JsConfig.IncludePublicFields) { foreach (var fieldInfo in requestType.GetPublicFields()) { var fieldName = fieldInfo.Name; result[fieldName.ToCamelCase()] = new FieldRouteMember(fieldInfo) { IgnoreInQueryString = fieldInfo.FirstAttribute<IgnoreDataMemberAttribute>() != null, //but allow in PathInfo }; } } return result; }
private static bool HasFormatter(Type l, string name) { return l.HasAttribute<FormatterAttribute>(a => a.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) || a.AlternateNames.CsvContains(name, StringComparison.InvariantCultureIgnoreCase)); }
/// <summary> /// Gets the CPP typename. /// </summary> /// <param name="type">The type.</param> /// <returns></returns> public static string GetCppTypename(Type type) { if (type.HasAttribute<CppClassAttribute>()) return type.Name.Substring(1).ToPascalCase(); return type.Name; }
internal static IDictionary<string, RouteMember> GetQueryProperties(Type requestType) { var result = new Dictionary<string, RouteMember>(StringExtensions.InvariantComparerIgnoreCase()); var hasDataContract = requestType.HasAttribute<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 static bool HasMatchingWildcard(Type lexer, string file) { return lexer.HasAttribute<FormatterFileExtensionAttribute>(a => file.MatchesFileWildcard(a.Pattern)); }
static bool IsReplicated(Type type) { return !type.IsAbstract && typeof(IMyReplicable).IsAssignableFrom(type) && !type.HasAttribute<NotReplicableAttribute>(); }
public DissolvedType(Type type) { if (type.HasAttribute<ComponentAttribute>()) { foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { Type fieldType = field.FieldType; bool fieldIsList = fieldType.IsList(); Type fieldListItemType = fieldIsList ? fieldType.GetListItemType() : null; if (fieldType.IsSubclassOf(typeof(UnityEngine.Object)) || (fieldIsList && fieldListItemType.IsSubclassOf(typeof(UnityEngine.Object)))) { bool processed = false; foreach (var attribute in field.GetCustomAttributes(true)) { AddComponentAttribute aca = attribute as AddComponentAttribute; if (aca != null) { AddComponentFields.Add(MakeAddComponentDissolveFieldDescription(aca.name, field)); processed = true; continue; } ComponentAttribute ca = attribute as ComponentAttribute; if (ca != null) { ComponentFields.Add(MakeDissolveFieldDescription(ca.name, field)); processed = true; continue; } ResourceAttribute ra = attribute as ResourceAttribute; if (ra != null) { ResourceFields.Add(MakeResourceDissolveFieldDescription(ra.name, field)); processed = true; continue; } } if (!processed) { ComponentFields.Add(MakeDissolveFieldDescription(string.Empty, field)); } } else { ComponentAttribute ca = field.GetAttribute<ComponentAttribute>(); if (ca != null) { SubComponents.Add(Tuple.Create(ca.name, field)); } } } } else { foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { Type fieldType = field.FieldType; bool isUnityObject = fieldType.IsSubclassOf(typeof(UnityEngine.Object)); foreach (var attribute in field.GetCustomAttributes(true)) { AddComponentAttribute aca = attribute as AddComponentAttribute; if (aca != null) { if (isUnityObject) { AddComponentFields.Add(MakeAddComponentDissolveFieldDescription(aca.name, field)); } continue; } ComponentAttribute ca = attribute as ComponentAttribute; if (ca != null) { if (isUnityObject) { ComponentFields.Add(MakeDissolveFieldDescription(ca.name, field)); } else { SubComponents.Add(Tuple.Create(ca.name, field)); } continue; } ResourceAttribute ra = attribute as ResourceAttribute; if (ra != null) { ResourceFields.Add(MakeResourceDissolveFieldDescription(ra.name, field)); continue; } } } } }
public static bool HasExplicitDescription(Type type) { return type.CanBeCastTo<DescribesItself>() || type.HasAttribute<DescriptionAttribute>() || type.HasAttribute<TitleAttribute>(); }
public static bool TypeIsMigration(Type type) { return typeof(IMigration).IsAssignableFrom(type) && type.HasAttribute<MigrationAttribute>(); }
public static bool TypeIsVersionTableMetaData(Type type) { return typeof(IVersionTableMetaData).IsAssignableFrom(type) && type.HasAttribute<VersionTableMetaDataAttribute>(); }
public ApiControllerInfo(object instance, Type type, string routePrefix = null) { Instance = instance; Type = type; if(Type == null && Instance != null) Type = Instance.GetType(); RoutePrefix = routePrefix; //Prefix if(RoutePrefix == null) { var prefAttr = Type.GetAttribute<ApiRoutePrefixAttribute>(); if(prefAttr != null) RoutePrefix = prefAttr.Prefix; } if (type.HasAttribute<LoggedInOnlyAttribute>()) Flags |= ControllerFlags.LoggedInOnly; //Secured if (type.HasAttribute<SecuredAttribute>()) Flags |= ControllerFlags.Secured; ApiGroup = GetApiGroup(Type); }
private List<MethodOperationParameter> ParseParameters(string verb, Type operationType, IDictionary<string, SwaggerModel> models, string route) { var hasDataContract = operationType.HasAttribute<DataContractAttribute>(); var properties = operationType.GetProperties(); var paramAttrs = new Dictionary<string, ApiMemberAttribute[]>(); var allowableParams = new List<ApiAllowableValuesAttribute>(); var defaultOperationParameters = new List<MethodOperationParameter>(); var hasApiMembers = false; foreach (var property in properties) { if (property.HasAttribute<IgnoreDataMemberAttribute>()) continue; var attr = hasDataContract ? property.FirstAttribute<DataMemberAttribute>() : null; var propertyName = attr != null && attr.Name != null ? attr.Name : property.Name; var apiMembers = property.AllAttributes<ApiMemberAttribute>(); if (apiMembers.Length > 0) hasApiMembers = true; paramAttrs[propertyName] = apiMembers; var allowableValuesAttrs = property.AllAttributes<ApiAllowableValuesAttribute>(); allowableParams.AddRange(allowableValuesAttrs); if (hasDataContract && attr == null) continue; var inPath = (route ?? "").ToLower().Contains("{" + propertyName.ToLower() + "}"); var paramType = inPath ? "path" : verb == HttpMethods.Post || verb == HttpMethods.Put ? "form" : "query"; defaultOperationParameters.Add(new MethodOperationParameter { Type = GetSwaggerTypeName(property.PropertyType), AllowMultiple = false, Description = property.PropertyType.GetDescription(), Name = propertyName, ParamType = paramType, Required = paramType == "path", AllowableValues = GetAllowableValue(allowableValuesAttrs.FirstOrDefault()), }); } var methodOperationParameters = defaultOperationParameters; if (hasApiMembers) { methodOperationParameters = new List<MethodOperationParameter>(); foreach (var key in paramAttrs.Keys) { var apiMembers = paramAttrs[key]; foreach (var member in apiMembers) { if ((member.Verb == null || string.Compare(member.Verb, verb, StringComparison.InvariantCultureIgnoreCase) == 0) && (member.Route == null || (route ?? "").StartsWith(member.Route)) && !string.Equals(member.ParameterType, "model")) { methodOperationParameters.Add(new MethodOperationParameter { Type = member.DataType ?? SwaggerType.String, AllowMultiple = member.AllowMultiple, Description = member.Description, Name = member.Name ?? key, ParamType = member.GetParamType(operationType, member.Verb ?? verb), Required = member.IsRequired, AllowableValues = GetAllowableValue(allowableParams.FirstOrDefault(attr => attr.Name == (member.Name ?? key))) }); } } } } if (!DisableAutoDtoInBodyParam) { if (!HttpMethods.Get.EqualsIgnoreCase(verb) && !HttpMethods.Delete.EqualsIgnoreCase(verb) && !methodOperationParameters.Any(p => "body".EqualsIgnoreCase(p.ParamType))) { ParseModel(models, operationType, route, verb); methodOperationParameters.Add(new MethodOperationParameter { ParamType = "body", Name = "body", Type = GetSwaggerTypeName(operationType, route, verb), }); } } return methodOperationParameters; }
public bool Matches(Type type) { return type.CanBeCastTo<DomainEntity>() && !type.HasAttribute<IgnoreEntityInBindingAttribute>(); }
/// <summary> /// Writes the typed function provider. /// </summary> /// <param name="functionProvider">The function provider.</param> private void WriteTypedFunctionProvider(Type functionProvider) { if (!functionProvider.HasAttribute<CppTypeAttribute>()) throw new NotSupportedException(); var attribute = functionProvider.GetAttribute<CppTypeAttribute>(false); string targetType = attribute.Typename; MethodInfo[] methods = functionProvider.GetMethods(MethodFlags); _writer.WriteLine(); _writer.WriteLine("/*"); _writer.WriteLine(" * Function group: {0}", targetType); _writer.WriteLine(" */"); _writer.WriteLine(); foreach (MethodInfo methodInfo in methods) { if (methodInfo.IsPropertyMember()) continue; WriteFunction(targetType, methodInfo); } _writer.WriteLine(); }
private List<MethodOperationParameter> ParseParameters(string verb, Type operationType, IDictionary<string, SwaggerModel> models, string route) { var hasDataContract = operationType.HasAttribute<DataContractAttribute>(); var properties = operationType.GetProperties(); var paramAttrs = new Dictionary<string, ApiMemberAttribute[]>(); var allowableParams = new List<ApiAllowableValuesAttribute>(); foreach (var property in properties) { var propertyName = property.Name; if (hasDataContract) { var dataMemberAttr = property.FirstAttribute<DataMemberAttribute>(); if (dataMemberAttr != null && dataMemberAttr.Name != null) { propertyName = dataMemberAttr.Name; } } paramAttrs[propertyName] = property.AllAttributes<ApiMemberAttribute>(); allowableParams.AddRange(property.AllAttributes<ApiAllowableValuesAttribute>()); } var methodOperationParameters = new List<MethodOperationParameter>(); foreach (var key in paramAttrs.Keys) { var value = paramAttrs[key]; methodOperationParameters.AddRange( from ApiMemberAttribute member in value where member.Verb == null || string.Compare(member.Verb, verb, StringComparison.InvariantCultureIgnoreCase) == 0 where member.Route == null || (route ?? "").StartsWith(member.Route) where !string.Equals(member.ParameterType, "model") select new MethodOperationParameter { DataType = member.DataType ?? SwaggerType.String, AllowMultiple = member.AllowMultiple, Description = member.Description, Name = member.Name ?? key, ParamType = member.GetParamType(operationType, member.Verb ?? verb), Required = member.IsRequired, AllowableValues = GetAllowableValue(allowableParams.FirstOrDefault(attr => attr.Name == member.Name)) }); } if (!DisableAutoDtoInBodyParam) { if (!HttpMethods.Get.Equals(verb, StringComparison.OrdinalIgnoreCase) && !methodOperationParameters.Any(p => "body".EqualsIgnoreCase(p.ParamType))) { ParseModel(models, operationType, route, verb); methodOperationParameters.Add(new MethodOperationParameter { DataType = GetSwaggerTypeName(operationType, route, verb), ParamType = "body", Name = GetSwaggerTypeName(operationType) }); } } return methodOperationParameters; }
/// <summary> /// Writes the wrapper methods. /// </summary> /// <param name="wrapperType">Type of the wrapper.</param> /// <param name="writeContent">if set to <c>true</c> [write content].</param> protected void WriteWrapperMethods(Type wrapperType, bool writeContent = false) { if (!wrapperType.HasAttribute<CppTypeAttribute>()) throw new NotSupportedException(); var attribute = wrapperType.GetAttribute<CppTypeAttribute>(false); IEnumerable<MethodInfo> methods = Options.GetAllMethods(wrapperType); _writer.WriteLine("/*"); _writer.WriteLine(" * Function group: {0}", wrapperType); _writer.WriteLine(" */"); _writer.WriteLine(); foreach (MethodInfo methodInfo in methods) { if (methodInfo.IsPropertyMember()) continue; WriteMethod(wrapperType, methodInfo, writeContent); } _writer.WriteLine(); }