test: 修复无法递归获取所有内嵌类型的问题

This commit is contained in:
Fu Diwei 2021-06-13 18:30:50 +08:00
parent 1a3aba1f5a
commit f2abcf3f71

View File

@ -60,16 +60,22 @@ namespace SKIT.FlurlHttpClient.Wechat
{ {
if (type == null) throw new ArgumentNullException(nameof(type)); if (type == null) throw new ArgumentNullException(nameof(type));
static Type[] GetAllNestedTypes(Type type) static Type[] GetAllNestedTypes(Type externalType)
{ {
return type.GetNestedTypes() List<Type> lstType = new List<Type>();
foreach (Type innerType in externalType.GetNestedTypes())
{
if (innerType.IsClass)
{
lstType.Add(innerType);
lstType.AddRange(GetAllNestedTypes(innerType));
}
}
return lstType
.Where(e => .Where(e =>
e.IsClass &&
!e.IsAbstract && !e.IsAbstract &&
!e.IsInterface e.IsNestedPublic
)
.SelectMany(e =>
GetAllNestedTypes(e)
) )
.ToArray(); .ToArray();
} }
@ -110,15 +116,22 @@ namespace SKIT.FlurlHttpClient.Wechat
} }
else if (propInfo.PropertyType.IsArray) else if (propInfo.PropertyType.IsArray)
{ {
Type elType = propInfo.PropertyType.Assembly.GetType(propInfo.PropertyType.FullName.Replace("[]", string.Empty)); if (propInfo.PropertyType.FullName.Contains("[][]"))
object elObj = (elType == typeof(string)) ? string.Empty : Activator.CreateInstance(elType); {
elObj = Convert.ChangeType(elObj, elType); // noop
func(elObj); }
else
{
Type elType = propInfo.PropertyType.Assembly.GetType(propInfo.PropertyType.FullName.Replace("[]", string.Empty));
object elObj = (elType == typeof(string)) ? string.Empty : Activator.CreateInstance(elType);
elObj = Convert.ChangeType(elObj, elType);
func(elObj);
Array prop = Array.CreateInstance(elType, 1); Array prop = Array.CreateInstance(elType, 1);
prop.SetValue(elObj, 0); prop.SetValue(elObj, 0);
propInfo.SetValue(obj, prop); propInfo.SetValue(obj, prop);
}
} }
else if (propInfo.PropertyType == typeof(string)) else if (propInfo.PropertyType == typeof(string))
{ {