protected void Init <TArg>(ref Action <TArg> action, bool requireSetter = true) { BindSource bindSource = new BindSource { Target = _target, MemberName = _memberName, Params = _params }; Init(ref action, ref bindSource, requireSetter); }
protected void Init <TResult>(ref Func <TResult> func, bool requireGetter = true) { BindSource bindSource = new BindSource { Target = _target, MemberName = _memberName, Params = _params }; Init(ref func, ref bindSource, requireGetter); }
private void BindGrid() { No.FieldName = "Code"; Document.FieldName = "Document"; Type.FieldName = "Type"; Year.FieldName = "Year"; NOG.FieldName = "NGO"; gridControl1.DataSource = BindSource.OrderBy(s => s.OrderType) .ThenBy(s => s.Type) .ThenByDescending(s => s.Year) .ThenBy(s => s.NGO); labelRight.Text = "សរុបឯកសារ: " + BindSource.Count + "/" + LawDictionaryModels.Count; }
protected void Init <TArg>(ref Action <TArg> action, ref BindSource bindSource, bool requireSetter = true) { try { Type type = bindSource.Target.GetType(); do { PropertyInfo prop = type.GetProperty(bindSource.MemberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (prop != null) { MethodInfo propSetter = prop.GetSetMethod(true); if (propSetter != null) { action = (Action <TArg>)Delegate.CreateDelegate(typeof(Action <TArg>), bindSource.Target, propSetter); return; } } type = type.BaseType; } while (type != typeof(Object)); } catch (Exception ex) { Debug.LogException(ex, this); } if (requireSetter) { Debug.LogError( "[ABinder] - Init Fail: " + bindSource.Target.name + "->" + bindSource.Target.GetType().Name + "." + bindSource.MemberName + " has no setter", this); } }
protected void Init <TResult>(ref Func <TResult> action, ref BindSource bindSource, bool requireGetter = true) { try { Type type = bindSource.Target.GetType(); do { PropertyInfo prop = type.GetProperty(bindSource.MemberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (prop != null) { if (prop.GetCustomAttributes(typeof(BindAttribute), true).Length > 0) { MethodInfo propGetter = prop.GetGetMethod(true); if (propGetter.ReturnType == typeof(Enum) && typeof(TResult) != typeof(Enum)) { Func <Enum> delegat = (Func <Enum>)Delegate.CreateDelegate(typeof(Func <Enum>), bindSource.Target, propGetter); action = () => (TResult)(object)delegat(); } else { action = (Func <TResult>)Delegate.CreateDelegate(typeof(Func <TResult>), bindSource.Target, propGetter); } return; } } foreach (MethodInfo method in type.GetMethods( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (method.Name != bindSource.MemberName || !typeof(TResult).IsAssignableFrom(method.ReturnType) || method.GetCustomAttributes(typeof(BindAttribute), true).Length == 0) { continue; } action = BindMethod <TResult>(bindSource.Target, method, bindSource.Params); if (action != null) { return; } } type = type.BaseType; } while (type != typeof(Object)); } catch (Exception ex) { Debug.LogException(ex, this); } if (requireGetter) { Debug.LogError( "[ABinder] - Init Fail: " + bindSource.Target.name + "->" + bindSource.Target.GetType().Name + "." + bindSource.MemberName + " has no getter", this); } else { Debug.Log( "[ABinder] - Property " + bindSource.Target.name + "->" + bindSource.Target.GetType().Name + "." + bindSource.MemberName + " has no getter. Binder get logic will not work.", this); } }