我认为你已经掌握了你需要的所有信息,但是你不能把所有的工作委托给编译器,你必须自己做这些工作:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using static System.Console;
public static class MyParser
{
public static void ParseObj<T>(T data)
{
foreach (var prop in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if(prop.PropertyType.IsArray) WriteLine($"{prop.Name}:array");
else if(prop.PropertyType == (typeof(string))) WriteLine($"{prop.Name}:string");
else if(prop.PropertyType.IsValueType) WriteLine($"{prop.Name}:value type");
else if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)) WriteLine($"{prop.Name}:IEnumerable");
else if(prop.PropertyType.IsEnum) WriteLine($"{prop.Name}:enum");
else if(prop.PropertyType.IsClass) WriteLine($"{prop.Name}:class");
else WriteLine($"{prop.Name}:something else");
}
}
}
public class Program
{
public static void Main()
{
MyParser.ParseObj(new
{
Str = "abc"
, Num = 543
, Arr = new[]{1, 2, 3}
, Chars = new char[]{'x', 'y', 'z'}
, SomeList = new List<string>(){"a","b","c"}
});
}
}
输出:
Str:string
Num:value type
Arr:array
Chars:array
SomeList:IEnumerable
*更新*
将代码的演变添加到注释的答案中,以防其他人发现此问题有用:
-
https://dotnetfiddle.net/Zkx7UJ
-
https://dotnetfiddle.net/aeCG3z
-
https://dotnetfiddle.net/6Z49ta
-
https://dotnetfiddle.net/u5HAFP