public void FormatObject() { Type t = _source.GetType(); PropertyInfo[] PropertyList = t.GetProperties(); foreach (PropertyInfo item in PropertyList) { if (!item.CanWrite) { continue; //该属性不可写,直接跳出 } string name = item.Name; object value = item.GetValue(_source, null); string code = GetCode(item.Name); RuleKey key = new RuleKey(item.PropertyType, code); try { if (_typeFormatRuleSource.ContainsKey(key)) { Type ruleType = _typeFormatRuleSource[key]; IRule formatRule = new FormatRuleBuilder().GetRule(ruleType, value); object targetValue = formatRule.Format(); item.SetValue(_source, targetValue); } } catch (Exception ex) { throw ex; } } }
public void AddOrUpdateFormatRule(RuleKey key, Type value) { _typeFormatRuleSource[key] = value; }