public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { var result = getNext()(input, getNext); if (!bindStrategy.IsIniPropertyCall(input) || !shouldProcess) { return(result); } //Console.WriteLine($"{nameof(BindIniBehavior)} called..."); var property = input.Target.GetType().GetProperty(input.MethodBase.Name.Substring(4)); //input.MethodBase.Name.Substring(4)可以得到属性的名字 var fullName = bindStrategy.GetFilePath(input, property); if (!File.Exists(fullName)) { CreateIniFile(input); firstTimeCalled = false; } else { if (config == null) { config = new IniFileOperator(fullName); } } if (firstTimeCalled) //第一次调用任何绑定Ini的字段前,先读取ini文件数据 { firstTimeCalled = false; SynchronizeIniData(input); //同步数据 } if (IsSetterCalled(input)) //属性的setter被调用 { UpdateIniKeyValue(input); //更新对应的字段 } return(result); }
private void CreateIniFile(IMethodInvocation input) { var property = BindStrategy.GetCalledProperty(input); var fullName = bindStrategy.GetFilePath(input, property); var stream = File.CreateText(fullName); stream.Close(); config = new IniFileOperator(fullName); shouldProcess = false; foreach (var p in bindStrategy.GetIniProperties(input)) { var value = p.GetValue(input.Target); var valueStr = value != null?value.ToString() : ""; string section = bindStrategy.GetSection(input, p); string key = bindStrategy.GetKey(input, p); config.WriteIniData(section, key, valueStr); } shouldProcess = true; }