/// <summary> /// Set up any enumerations whose values are stored as strings /// </summary> /// <param name="attribute"></param> /// <param name="enumType"></param> public static void SetupStringBasedEnumeration(AttributeInfo attribute, Type enumType) { var strings = Enum.GetNames(enumType); AttributeRule rule = new StringEnumRule(strings); attribute.AddRule(rule); attribute.DefaultValue = strings[0]; }
public void TestValidate() { StringEnumRule test = new StringEnumRule(new string[] { "a", "b" }); Assert.True(test.Validate("a", null)); Assert.True(test.Validate("b", null)); Assert.False(test.Validate("c", null)); }
private static void GenerateEnums(StringBuilder sb, SchemaLoader typeLoader, XmlQualifiedName[] namespaces, Dictionary <string, string> domNodeTypeToClassName, Dictionary <string, string> classNameToDomNodeType) { // Temp code // Currently the only way I can see to find out which strings are enum type // is to search for the StringEnumRule on the AttributeType // If this exists then use reflection to access the values list // This could and should be done in some nicer way in the future! System.Reflection.FieldInfo fInfo = typeof(StringEnumRule).GetField("m_values", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (fInfo != null) { var enumAttributeTypes = new HashSet <AttributeType>(); foreach (AttributeType attributeType in typeLoader.GetAttributeTypes()) { StringEnumRule rule = attributeType.Rules.FirstOrDefault(x => x is StringEnumRule) as StringEnumRule; if (rule != null) { if (enumAttributeTypes.Add(attributeType)) { string[] values = fInfo.GetValue(rule) as string[]; if (values != null) { string enumTypeName = GetClassName(namespaces, attributeType, domNodeTypeToClassName, classNameToDomNodeType); WriteLine(sb, ""); WriteLine(sb, " public enum {0}", enumTypeName); WriteLine(sb, " {{"); foreach (string value in values) { WriteLine(sb, " " + value + ","); } if (values.Length > 0) { sb.Length = sb.Length - 1; } WriteLine(sb, " }}\r"); } } } } } }
private static void SelectEditorNConvertorForAttribute(AttributeInfo attribute, ref string editor, ref string convertor) { var clrType = attribute.Type.ClrType; string attrTypeName = attribute.Type.Name; int separator = attrTypeName.LastIndexOf(':'); if (separator > 0) { attrTypeName = attrTypeName.Substring(separator + 1); } StringEnumRule rule = attribute.Type.Rules.FirstOrDefault(x => x is StringEnumRule) as StringEnumRule; bool isEnumAttribute = attribute.Type.ClrType == typeof(string) && rule != null; if (!string.IsNullOrEmpty(editor)) { string parameters = ""; // cut off parameter // parse editor type int param = editor.IndexOf(':'); if (param > 0) { parameters = editor.Substring(param + 1); editor = editor.Substring(0, param); } separator = editor.IndexOf(','); if (separator > 0) { editor = editor.Substring(0, separator); } if (isEnumAttribute && string.IsNullOrEmpty(parameters)) { editor = string.Format("new {0}(typeof({1}))", editor, attrTypeName); } else { editor = string.Format("new {0}({1})", editor, parameters); } } else { if (isEnumAttribute) { editor = string.Format("new Sce.Atf.Controls.PropertyEditing.LongEnumEditor(typeof({0}))", attrTypeName); convertor = "Sce.Atf.Controls.PropertyEditing.ExclusiveEnumTypeConverter"; } } if (string.IsNullOrEmpty(convertor)) { if (isEnumAttribute) { convertor = string.Format("new Sce.Atf.Controls.PropertyEditing.ExclusiveEnumTypeConverter(typeof({0})", attrTypeName); } } else { if (isEnumAttribute) { convertor = string.Format("new {0}(typeof({1}))", convertor, attrTypeName); } else { convertor = string.Format("new {0}()", convertor); } } }