//template名, 字段名 public static SortedDictionary <string, CStringFieldInfo> GetOutputFields() { SortedDictionary <string, CStringFieldInfo> templateFieldMap = new SortedDictionary <string, CStringFieldInfo>(); SortedDictionary <string, Type> templateNameSet = GetOutputTypes(); foreach (var kv in templateNameSet) { string templateName = kv.Key; Type t = kv.Value; if (t != null && t.GetCustomAttributes(typeof(global::ProtoBuf.ProtoContractAttribute), false) != null) { PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo prop in properties) { var propMember = prop.GetCustomAttributes(typeof(global::ProtoBuf.ProtoContractAttribute), false); if (propMember == null) { continue; } CStringFieldInfo fieldInfo = GenerateFieldInfo(t); templateFieldMap[templateName] = fieldInfo; } } } return(templateFieldMap); }
public static CStringFieldInfo GenerateFieldInfo(Type t) { CStringFieldInfo fieldInfo = new CStringFieldInfo(); if (t != null && t.GetCustomAttributes(typeof(global::ProtoBuf.ProtoContractAttribute), false) != null) { PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo prop in properties) { var propMember = prop.GetCustomAttributes(typeof(global::ProtoBuf.ProtoContractAttribute), false); if (propMember == null) { continue; } if (prop.PropertyType == typeof(string)) //字符串类型 { fieldInfo.Fields.Add(prop); } //判断是否list类型 bool bIsGenericList = prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(List <>); //内置类型 if (!bIsGenericList && !prop.PropertyType.IsPrimitive && prop.PropertyType != typeof(string) && !prop.PropertyType.IsEnum) { CStringFieldInfo memberFieldInfo = GenerateFieldInfo(prop.PropertyType); fieldInfo.MemberFields[prop.Name] = memberFieldInfo; } //List类型 if (bIsGenericList) { Type itemType = prop.PropertyType.GetGenericArguments()[0]; if (!fieldInfo.ListFields.ContainsKey(prop.Name)) { CStringFieldInfo itemFieldInfo = GenerateFieldInfo(itemType); fieldInfo.ListFields[prop.Name] = itemFieldInfo; } } } } return(fieldInfo); }