public MetadataPropertyDescriptorWrapper(PropertyDescriptor descriptor, Attribute[] newAttributes) : base(descriptor, newAttributes) { _descriptor = descriptor; var readOnlyAttribute = newAttributes.OfType<ReadOnlyAttribute>().FirstOrDefault(); _isReadOnly = (readOnlyAttribute != null ? readOnlyAttribute.IsReadOnly : false); }
public LoggingData[] Extract(Attribute[] attributes, string defaultName, object @object) { return attributes .OfType<LogAttribute>() .Select(x => new LoggingData { Name = GetLogName(x, defaultName), Value = @object.ToString() }) .ToArray(); }
public CachedDataAnnotationsMetadataAttributes(Attribute[] attributes) { DataType = attributes.OfType<DataTypeAttribute>().FirstOrDefault(); Display = attributes.OfType<DisplayAttribute>().FirstOrDefault(); DisplayColumn = attributes.OfType<DisplayColumnAttribute>().FirstOrDefault(); DisplayFormat = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault(); DisplayName = attributes.OfType<DisplayNameAttribute>().FirstOrDefault(); Editable = attributes.OfType<EditableAttribute>().FirstOrDefault(); HiddenInput = attributes.OfType<HiddenInputAttribute>().FirstOrDefault(); ReadOnly = attributes.OfType<ReadOnlyAttribute>().FirstOrDefault(); Required = attributes.OfType<RequiredAttribute>().FirstOrDefault(); ScaffoldColumn = attributes.OfType<ScaffoldColumnAttribute>().FirstOrDefault(); var uiHintAttributes = attributes.OfType<UIHintAttribute>(); UIHint = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase)) ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer)); if (DisplayFormat == null && DataType != null) { DisplayFormat = DataType.DisplayFormat; } }
//For generic automatic editors. Passing a MemberInfo will also check for attributes public static object GenericField(string name, object value, Type t, MemberInfo member = null, object instance = null) { if (t == null){ GUILayout.Label("NO TYPE PROVIDED!"); return value; } //Preliminary Hides if (typeof(Delegate).IsAssignableFrom(t)){ return value; } name = name.SplitCamelCase(); // IEnumerable<Attribute> attributes = new Attribute[0]; if (member != null){ //Hide class? if (t.GetCustomAttributes(typeof(HideInInspector), true ).FirstOrDefault() != null){ return value; } attributes = member.GetCustomAttributes(true).Cast<Attribute>(); //Hide field? if (attributes.Any(a => a is HideInInspector) ){ return value; } //Is required? if (attributes.Any(a => a is RequiredFieldAttribute)){ if ( (value == null || value.Equals(null) ) || (t == typeof(string) && string.IsNullOrEmpty((string)value) ) || (typeof(BBParameter).IsAssignableFrom(t) && (value as BBParameter).isNull) ) { GUI.backgroundColor = lightRed; } } } if (member != null){ var nameAtt = attributes.FirstOrDefault(a => a is NameAttribute) as NameAttribute; if (nameAtt != null){ name = nameAtt.name; } if (instance != null){ var showAtt = attributes.FirstOrDefault(a => a is ShowIfAttribute) as ShowIfAttribute; if (showAtt != null){ var targetField = instance.GetType().GetField(showAtt.fieldName); if (targetField == null || targetField.FieldType != typeof(bool)){ GUILayout.Label(string.Format("[ShowIf] Error: bool \"{0}\" does not exist.", showAtt.fieldName)); } else { if ((bool)targetField.GetValue(instance) != showAtt.show){ return value; } } } } } //Before everything check BBParameter if (typeof(BBParameter).IsAssignableFrom(t)){ return BBParameterField(name, (BBParameter)value, false, member); } //Cutstom object drawers var objectDrawer = GetCustomDrawer(t); if (objectDrawer != null && !(objectDrawer is NoDrawer) ){ return objectDrawer.DrawGUI(name, value, member as FieldInfo, null, instance); } //Cutstom attribute drawers foreach(CustomDrawerAttribute att in attributes.OfType<CustomDrawerAttribute>()){ var attributeDrawer = GetCustomDrawer(att.GetType()); if (attributeDrawer != null && !(attributeDrawer is NoDrawer)){ return attributeDrawer.DrawGUI(name, value, member as FieldInfo, att, instance); } } //Then check UnityObjects if ( typeof(UnityObject).IsAssignableFrom(t) ) { if (t == typeof(Component) && (Component)value != null) return ComponentField(name, (Component)value, typeof(Component)); return EditorGUILayout.ObjectField(name, (UnityObject)value, t, true); } //Force UnityObject field? if (member != null && attributes.Any(a => a is ForceObjectFieldAttribute)){ return EditorGUILayout.ObjectField(name, value as UnityObject, t, true ); } //Restricted popup values? if (member != null){ var popAtt = attributes.FirstOrDefault(a => a is PopupFieldAttribute) as PopupFieldAttribute; if (popAtt != null){ if (popAtt.staticPath != null){ try { var typeName = popAtt.staticPath.Substring(0, popAtt.staticPath.LastIndexOf(".")); var type = ReflectionTools.GetType( typeName ); var start = popAtt.staticPath.LastIndexOf(".") + 1; var end = popAtt.staticPath.Length; var propName = popAtt.staticPath.Substring(start, end - start); var prop = type.GetProperty( propName, BindingFlags.Static | BindingFlags.Public ); var propValue = prop.GetValue(null, null); var values = ((IEnumerable)propValue).Cast<object>().ToList(); return Popup<object>(name, value, values); } catch { EditorGUILayout.LabelField(name, "[PopupField] attribute error!"); return value; } } return Popup<object>(name, value, popAtt.values.ToList()); } } //Check Type of Type if (t == typeof(Type)){ return Popup<Type>(name, (Type)value, UserTypePrefs.GetPreferedTypesList(typeof(object), false) ); } //Check abstract if ( (value != null && value.GetType().IsAbstract) || (value == null && t.IsAbstract) ){ EditorGUILayout.LabelField(name, string.Format("Abstract ({0})", t.FriendlyName())); return value; } //Create instance for some types if (value == null && !t.IsAbstract && !t.IsInterface && (t.IsValueType || t.GetConstructor(Type.EmptyTypes) != null || t.IsArray) ){ if (t.IsArray){ value = Array.CreateInstance(t.GetElementType(), 0); } else { value = Activator.CreateInstance(t); } } //Check the rest //.............. if (t == typeof(string)){ if (member != null){ if (attributes.Any(a => a is TagFieldAttribute)) return EditorGUILayout.TagField(name, (string)value); var areaAtt = attributes.FirstOrDefault(a => a is TextAreaFieldAttribute) as TextAreaFieldAttribute; if (areaAtt != null){ GUILayout.Label(name); var areaStyle = new GUIStyle(GUI.skin.GetStyle("TextArea")); areaStyle.wordWrap = true; var s = EditorGUILayout.TextArea((string)value, areaStyle, GUILayout.Height(areaAtt.height)); return s; } } return EditorGUILayout.TextField(name, (string)value); } if (t == typeof(bool)) return EditorGUILayout.Toggle(name, (bool)value); if (t == typeof(int)){ if (member != null){ var sField = attributes.FirstOrDefault(a => a is SliderFieldAttribute) as SliderFieldAttribute; if (sField != null) return (int)EditorGUILayout.Slider(name, (int)value, (int)sField.left, (int)sField.right ); if (attributes.Any(a => a is LayerFieldAttribute)) return EditorGUILayout.LayerField(name, (int)value); } return EditorGUILayout.IntField(name, (int)value); } if (t == typeof(float)){ if (member != null){ var sField = attributes.FirstOrDefault(a => a is SliderFieldAttribute) as SliderFieldAttribute; if (sField != null) return EditorGUILayout.Slider(name, (float)value, sField.left, sField.right); } return EditorGUILayout.FloatField(name, (float)value); } if (t == typeof(byte)){ return Convert.ToByte( Mathf.Clamp(EditorGUILayout.IntField(name, (byte)value), 0, 255) ); } if (t == typeof(Vector2)) return EditorGUILayout.Vector2Field(name, (Vector2)value); if (t == typeof(Vector3)) return EditorGUILayout.Vector3Field(name, (Vector3)value); if (t == typeof(Vector4)) return EditorGUILayout.Vector4Field(name, (Vector4)value); if (t == typeof(Quaternion)){ var quat = (Quaternion)value; var vec4 = new Vector4(quat.x, quat.y, quat.z, quat.w); vec4 = EditorGUILayout.Vector4Field(name, vec4); return new Quaternion(vec4.x, vec4.y, vec4.z, vec4.w); } if (t == typeof(Color)) return EditorGUILayout.ColorField(name, (Color)value); if (t == typeof(Rect)) return EditorGUILayout.RectField(name, (Rect)value); if (t == typeof(AnimationCurve)) return EditorGUILayout.CurveField(name, (AnimationCurve)value); if (t == typeof(Bounds)) return EditorGUILayout.BoundsField(name, (Bounds)value); if (t == typeof(LayerMask)) return LayerMaskField(name, (LayerMask)value); if (t.IsSubclassOf(typeof(System.Enum))){ #if UNITY_5 if (t.GetCustomAttributes(typeof(FlagsAttribute), true).FirstOrDefault() != null ){ return EditorGUILayout.EnumMaskPopup(new GUIContent(name), (System.Enum)value); } #endif return EditorGUILayout.EnumPopup(name, (System.Enum)value); } if (typeof(IList).IsAssignableFrom(t)) return ListEditor(name, (IList)value, t, instance); if (typeof(IDictionary).IsAssignableFrom(t)) return DictionaryEditor(name, (IDictionary)value, t, instance); //show nested class members recursively if (value != null && !t.IsEnum && !t.IsInterface){ GUILayout.BeginVertical(); EditorGUILayout.LabelField(name, t.FriendlyName()); EditorGUI.indentLevel ++; ShowAutoEditorGUI(value); EditorGUI.indentLevel --; GUILayout.EndVertical(); } else { EditorGUILayout.LabelField(name, string.Format("({0})", t.FriendlyName())); } return value; }
static string GetFormat(Type propertyType, Attribute[] attributes) { var format = attributes.OfType<DisplayFormatAttribute>().Select(f => f.DataFormatString).FirstOrDefault(); if (format == null && propertyType == typeof(int)) format = "N0"; return format; }
/// <summary> /// Overridden to allow the addition of buddy-class attributes to the list of attributes associated with the <see cref="ModelType"/> /// </summary> /// <param name="declaringType"></param> /// <param name="property"></param> /// <param name="name"></param> /// <param name="isStatic"></param> /// <param name="isBoundary"></param> /// <param name="propertyType"></param> /// <param name="isList"></param> /// <param name="attributes"></param> /// <returns></returns> protected override ModelValueProperty CreateValueProperty(ModelType declaringType, System.Reflection.PropertyInfo property, string name, string label, string helptext, string format, bool isStatic, Type propertyType, TypeConverter converter, bool isList, bool isReadOnly, bool isPersisted, Attribute[] attributes) { // Do not include entity reference properties in the model if (property.PropertyType.IsSubclassOf(typeof(EntityReference))) return null; // Fetch any attributes associated with a buddy-class attributes = attributes.Union(GetBuddyClassAttributes(declaringType, property)).ToArray(); // Mark properties that are not mapped as not persisted isPersisted = !attributes.OfType<NotMappedAttribute>().Any(); return new EntityValueProperty(declaringType, property, name, label, helptext, format, isStatic, propertyType, converter, isList, isReadOnly, isPersisted, attributes); }
/// <summary> /// Overridden to allow the addition of buddy-class attributes to the list of attributes associated with the <see cref="ModelType"/> /// </summary> /// <param name="declaringType"></param> /// <param name="property"></param> /// <param name="name"></param> /// <param name="isStatic"></param> /// <param name="isBoundary"></param> /// <param name="propertyType"></param> /// <param name="isList"></param> /// <param name="attributes"></param> /// <returns></returns> protected override ModelReferenceProperty CreateReferenceProperty(ModelType declaringType, System.Reflection.PropertyInfo property, string name, string label, string helptext, string format, bool isStatic, ModelType propertyType, bool isList, bool isReadOnly, bool isPersisted, Attribute[] attributes) { // Fetch any attributes associated with a buddy-class attributes = attributes.Union(GetBuddyClassAttributes(declaringType, property)).ToArray(); // Mark properties that are not mapped as not persisted isPersisted = !attributes.OfType<NotMappedAttribute>().Any(); // Determine whether the property represents an actual entity framework navigation property or an custom property var context = GetObjectContext(); var type = context.ObjectContext.MetadataWorkspace.GetItem<EntityType>(((EntityModelType)declaringType).UnderlyingType.FullName, DataSpace.CSpace); NavigationProperty navProp; type.NavigationProperties.TryGetValue(name, false, out navProp); return new EntityReferenceProperty(declaringType, navProp, property, name, label, helptext, format, isStatic, propertyType, isList, isReadOnly, isPersisted, attributes); }
protected override ModelValueProperty CreateValueProperty(ModelType declaringType, PropertyInfo property, string name, string label, string helptext, string format, bool isStatic, Type propertyType, System.ComponentModel.TypeConverter converter, bool isList, bool isReadOnly, bool isPersisted, Attribute[] attributes) { format = format ?? attributes.OfType<DisplayFormatAttribute>().Select(f => f.DataFormatString).FirstOrDefault(); return base.CreateValueProperty(declaringType, property, name, label, helptext, format, isStatic, propertyType, converter, isList, isReadOnly, isPersisted, attributes); }
private static void GetConstraints(Attribute[] attributes, IConstraintsNode constraintsNode) { if (attributes != null) { if (attributes.Any(x => x is RequiredAttribute)) { constraintsNode.IsRequired = true; } var stringLengthAttribute = attributes.OfType<StringLengthAttribute>().FirstOrDefault(); if (stringLengthAttribute != null) { constraintsNode.MinimumLength = stringLengthAttribute.MinimumLength; constraintsNode.MaximumLength = stringLengthAttribute.MaximumLength; } var minLengthAttribute = attributes.OfType<MaxLengthAttribute>().FirstOrDefault(); if (minLengthAttribute != null) { constraintsNode.MinimumLength = minLengthAttribute.Length; } var maxLengthAttribute = attributes.OfType<MaxLengthAttribute>().FirstOrDefault(); if (maxLengthAttribute != null) { constraintsNode.MaximumLength = maxLengthAttribute.Length; } var compareAttribute = attributes.OfType<CompareAttribute>().FirstOrDefault(); if (compareAttribute != null) { constraintsNode.CompareTo = StringHelpers.ToCamelCase(compareAttribute.OtherProperty); } var rangeAttribute = attributes.OfType<RangeAttribute>().FirstOrDefault(); if (rangeAttribute != null) { constraintsNode.Minimum = rangeAttribute.Minimum; constraintsNode.Maximum = rangeAttribute.Maximum; } if (attributes.Any(x => x is EmailAddressAttribute)) { constraintsNode.Format = Format.Email; } } }