protected void AutoSetParam<TI>(IRfcDataContainer container, TI model) { Type t = model.GetType(); PropertyInfo[] PropertyList = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo item in PropertyList) { string name = item.Name; if (name.Substring(0, 1) == "_") continue; object value = item.GetValue(model, null); container.SetValue(name.ToUpper(), value); } }
带自定义属性
Type t = HeaderData.GetType(); PropertyInfo[] PropertyList = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo item in PropertyList) { string name = item.Name; var attr = item.GetCustomAttribute<DisplayNameAttribute>(); if (attr != null) { name = attr.DisplayName; } object value = item.GetValue(HeaderData, null); request.Headers.Add(name, value.ToString()); }