如何将代码中的int32数据类型转换为其全名称integer?
Absolutely, you can make this happen—here's how to get the "integer" name instead of "Int32" in your DataType field.
The issue right now is that item.FieldType.Name returns the official CLR type name (like "Int32" for the int type) instead of the human-readable term you need. To fix this, you’ll create a mapping between these CLR names and your preferred full names, then use that mapping when setting the DataType property.
Step 1: Create a Type Mapping Dictionary
First, define a dictionary that maps common CLR type names to your desired terms. You can expand this as needed for other types:
private static readonly Dictionary<string, string> TypeNameMapping = new Dictionary<string, string> { {"Int32", "integer"}, {"String", "string"}, {"Boolean", "boolean"}, {"Double", "double"}, {"DateTime", "date-time"}, // Add more mappings for other types you need };
Step 2: Update Your Code to Use the Mapping
Modify the section where you set the DataType value to check the dictionary first. If the CLR name exists in the mapping, use the friendly name; otherwise, fall back to the original type name:
public static List<Parameter> GetParameterMembers(Type parameterType) { var parameterList = new List<Parameter>(); var returnTypeFields = parameterType.IsArray ? ((TypeInfo)parameterType.GetElementType()).DeclaredFields : ((TypeInfo)parameterType).DeclaredFields; returnTypeFields.ToList().ForEach(item => { if (item.MemberType != MemberTypes.Field) return; // Resolve the friendly data type name string dataType; if (!TypeNameMapping.TryGetValue(item.FieldType.Name, out dataType)) { dataType = item.FieldType.Name; // Fallback to original if no mapping exists } parameterList.Add(new Parameter { Name = item.Name, Parameters = item.FieldType.IsClass && !IsNativeType(((FieldInfo)item).FieldType) ? GetParameterMembers(((FieldInfo)item).FieldType) : new List<Parameter>(), DataType = dataType }); }); return parameterList; }
Extra Notes:
- For nullable types (like
int?, which shows up as "Nullable`1"), you can add logic to extract the underlying type first before checking the mapping. - Custom types will use their original name by default, which is likely what you want for non-native types.
- Feel free to expand the
TypeNameMappingdictionary to cover any other CLR types you need to rename.
That’s it—now any int fields will have "integer" as their DataType instead of "Int32".
内容的提问来源于stack exchange,提问作者John

