public static string GetPropertyDisplayName(MemberInfo property, bool splitCamelCase = true) { var displayNameAttribute = MemberInfoExtensions.GetCustomAttribute <DisplayNameAttribute>(property, true); if (displayNameAttribute != null) { return(displayNameAttribute.DisplayName); } #if NET45 || NET46 || NETSTANDARD2_1 var displayAttribute = MemberInfoExtensions.GetCustomAttribute <System.ComponentModel.DataAnnotations.DisplayAttribute>(property, true); if (displayAttribute != null) { return(displayAttribute.Name); } #endif //well, no attribute found, let's just take that property's name then... return(splitCamelCase ? SplitCamelCase(property.Name) : property.Name); }
public static float GetMemberDisplayOrder(MemberInfo member) { var attribute = MemberInfoExtensions.GetCustomAttribute <DisplayAttribute>(member); if (attribute != null && attribute.DisplayOrder.HasValue) { return(attribute.Order); } switch (member.MemberType) { case MemberTypes.Field: return(100f); case MemberTypes.Property: return(200f); case MemberTypes.Method: return(300f); default: throw new NotSupportedException(); } }
public static bool IsVisible(MemberInfo member, object target) { MethodCaller <object, bool> isVisible; if (!_isVisibleCache.TryGetValue(member, out isVisible)) { //Debug.Log("Building delegate for conditionally visible member: " + member.Name); var attr = MemberInfoExtensions.GetCustomAttribute <VisibleWhenAttribute>(member); if (attr == null) { _isVisibleCache[member] = AlwaysVisible; return(true); } var targetType = target.GetType(); var conditionMemberNames = attr.ConditionMembers; var conditions = new List <MethodCaller <object, bool> >(conditionMemberNames.Length); for (int i = 0; i < conditionMemberNames.Length; i++) { var conditionMemberName = conditionMemberNames[i]; if (string.IsNullOrEmpty(conditionMemberName)) { Debug.Log("Empty condition is used in VisibleWhen annotated on member: " + member.Name); continue; } bool negate = conditionMemberName[0] == '!'; if (negate) { conditionMemberName = conditionMemberName.Remove(0, 1); } var conditionMember = targetType.GetMemberFromAll(conditionMemberName, Flags.StaticInstanceAnyVisibility); if (conditionMember == null) { Debug.Log("Member not found: " + conditionMemberName); _isVisibleCache[conditionMember] = AlwaysVisible; return(true); } Assert.IsTrue(attr.Operator == '|' || attr.Operator == '&', "Only AND ('&') and OR ('|') operators are supported"); MethodCaller <object, bool> condition = null; switch (conditionMember.MemberType) { case MemberTypes.Field: // I feel like there should be a shorter way of doing this... if (negate) { condition = (x, y) => !(bool)(conditionMember as FieldInfo).GetValue(x); } else { condition = (x, y) => (bool)(conditionMember as FieldInfo).GetValue(x); } break; case MemberTypes.Property: if (negate) { condition = (x, y) => !(bool)(conditionMember as PropertyInfo).GetValue(x, null); } else { condition = (x, y) => (bool)(conditionMember as PropertyInfo).GetValue(x, null); } break; case MemberTypes.Method: if (negate) { condition = (x, y) => !(bool)(conditionMember as MethodInfo).Invoke(x, y); } else { condition = (x, y) => (bool)(conditionMember as MethodInfo).Invoke(x, y); } break; } Assert.IsNotNull(condition, "Should have assigned a condition by now for member type: " + conditionMember.MemberType); conditions.Add(condition); } isVisible = (tgt, args) => { bool ret = attr.Operator == '&'; for (int i = 0; i < conditions.Count; i++) { var condition = conditions[i]; if (attr.Operator == '&') { ret &= condition(tgt, args); } else { ret |= condition(tgt, args); } } return(ret); }; //TODO: Fix FastReflection bug generating methods when target is 'object' //isVisible = method.DelegateForCall<object, bool>(); //FastReflection.GenDebugAssembly("IsVisible_" + method.Name + ".dll", null, null, method, null, null); _isVisibleCache[member] = isVisible; } var result = isVisible(target, null); return(result); }
public void Initialize(MethodInfo method, object rawTarget, UnityObject unityTarget, int id, BaseGUI gui, EditorRecord prefs) { this.prefs = prefs; this.gui = gui; this.rawTarget = rawTarget; this.unityTarget = unityTarget; this.id = id; if (initialized) { return; } initialized = true; isCoroutine = method.ReturnType == typeof(IEnumerator); var commentAttr = MemberInfoExtensions.GetCustomAttribute <CommentAttribute>(method); if (commentAttr != null) { comment = commentAttr.comment; } niceName = method.GetNiceName(); if (niceName.IsPrefix("dbg") || niceName.IsPrefix("Dbg")) { niceName = niceName.Remove(0, 3); } invoke = method.DelegateForCall(); var argInfos = method.GetParameters(); int len = argInfos.Length; argValues = new object[len]; argKeys = new int[len]; argMembers = new EditorMember[len]; for (int iLoop = 0; iLoop < len; iLoop++) { int i = iLoop; var argInfo = argInfos[i]; argKeys[i] = RuntimeHelper.CombineHashCodes(id, argInfo.ParameterType.Name + argInfo.Name); argValues[i] = TryLoad(argInfos[i].ParameterType, argKeys[i]); argMembers[i] = EditorMember.WrapGetSet( @get: () => argValues[i], @set: x => argValues[i] = x, @rawTarget: rawTarget, @unityTarget: unityTarget, @attributes: argInfo.GetCustomAttributes(true) as Attribute[], @name: argInfo.Name, @id: argKeys[i], @dataType: argInfo.ParameterType ); } #if DBG Log("Method drawer init"); #endif }
public CategoryDefinitionResolver() { _excluded = new MembersList(); // member resolver (when including members to a certain category via attributes) _memres = (input, def) => { var output = new MembersList(); output.AddRange(input.Where(m => { var caetgory = MemberInfoExtensions.GetCustomAttribute <CategoryAttribute>(m); if (caetgory != null && caetgory.name == def.FullPath) { return(true); } var show = MemberInfoExtensions.GetCustomAttribute <ShowAttribute>(m); return(show != null && show.Category == def.FullPath); })); return(output); }; _defres = new Func <MembersList, DefineCategoryAttribute, MembersList>[] { // regex pattern resolver (input, def) => { var output = new MembersList(); var pattern = def.Pattern; if (!pattern.IsNullOrEmpty()) { output.AddRange(input.Where(member => Regex.IsMatch(member.Name, pattern))); } return(output); }, // return type resolver (input, def) => { var output = new MembersList(); var returnType = def.DataType; if (returnType != null) { output.AddRange(input.Where(m => m.GetDataType().IsA(returnType))); } return(output); }, // member type resolver (input, def) => { var output = new MembersList(); Predicate <CategoryMemberType> isMemberTypeDefined = mType => (def.MemberType & mType) > 0; output.AddRange(input.Where(m => isMemberTypeDefined((CategoryMemberType)m.MemberType))); return(output); }, // explicit members resolver (input, def) => { var output = new MembersList(); var explicitMembers = def.ExplicitMembers; output.AddRange(input.Where(m => explicitMembers.Contains(m.Name))); return(output); }, }; }