动态初始化集合<t>
dynamically intitialize iset<t>
关于构建 class,我想用 HashSet 动态初始化所有 ISet,
下面是我用 List
为 IList 实现它的方法
var properties = GetType()
.GetProperties()
.Where(x => x.PropertyType.IsGenericType &&
x.PropertyType.GetGenericTypeDefinition() == typeof(IList<>))
.ToList();
foreach (var property in properties)
{
// get T type of ISet
if (property.PropertyType.GetGenericArguments().Length > 1) continue;
var listElemType = property.PropertyType.GetGenericArguments()[0];
if (listElemType == null) continue;
// create hashedset
var constructorInfo = typeof(List<>)
.MakeGenericType(listElemType)
.GetConstructor(Type.EmptyTypes);
//construct object
if (constructorInfo == null) continue;
var listInstance = (IList)constructorInfo.Invoke(null);
property.SetValue(this, listInstance);
}
但是如果我对 ISet 尝试同样的事情,它就不起作用:(
var properties = GetType()
.GetProperties()
.Where(x => x.PropertyType.IsGenericType &&
x.PropertyType.GetGenericTypeDefinition() == typeof(ISet<>))
.ToList();
foreach (var property in properties)
{
// get T type of ISet
if (property.PropertyType.GetGenericArguments().Length > 1) continue;
var listElemType = property.PropertyType.GetGenericArguments()[0];
if (listElemType == null) continue;
// create hashedset
var constructorInfo = typeof(HashSet<>)
.MakeGenericType(listElemType)
.GetConstructor(Type.EmptyTypes);
//construct object
if (constructorInfo == null) continue;
//============== HERE IS THE PROBLEM ============
// var listInstance = (ISet)constructorInfo.Invoke(null);
// property.SetValue(this, listInstance);
}
没有像 IList 这样的 ISet.. 在这种情况下如何实现?
PropertyInfo.SetValue
需要 object
,因此您不必转换 constructorInfo.Invoke(null)
:
的结果
var listInstance = constructorInfo.Invoke(null);
property.SetValue(this, listInstance);
关于构建 class,我想用 HashSet 动态初始化所有 ISet,
下面是我用 List
为 IList 实现它的方法var properties = GetType()
.GetProperties()
.Where(x => x.PropertyType.IsGenericType &&
x.PropertyType.GetGenericTypeDefinition() == typeof(IList<>))
.ToList();
foreach (var property in properties)
{
// get T type of ISet
if (property.PropertyType.GetGenericArguments().Length > 1) continue;
var listElemType = property.PropertyType.GetGenericArguments()[0];
if (listElemType == null) continue;
// create hashedset
var constructorInfo = typeof(List<>)
.MakeGenericType(listElemType)
.GetConstructor(Type.EmptyTypes);
//construct object
if (constructorInfo == null) continue;
var listInstance = (IList)constructorInfo.Invoke(null);
property.SetValue(this, listInstance);
}
但是如果我对 ISet 尝试同样的事情,它就不起作用:(
var properties = GetType()
.GetProperties()
.Where(x => x.PropertyType.IsGenericType &&
x.PropertyType.GetGenericTypeDefinition() == typeof(ISet<>))
.ToList();
foreach (var property in properties)
{
// get T type of ISet
if (property.PropertyType.GetGenericArguments().Length > 1) continue;
var listElemType = property.PropertyType.GetGenericArguments()[0];
if (listElemType == null) continue;
// create hashedset
var constructorInfo = typeof(HashSet<>)
.MakeGenericType(listElemType)
.GetConstructor(Type.EmptyTypes);
//construct object
if (constructorInfo == null) continue;
//============== HERE IS THE PROBLEM ============
// var listInstance = (ISet)constructorInfo.Invoke(null);
// property.SetValue(this, listInstance);
}
没有像 IList 这样的 ISet.. 在这种情况下如何实现?
PropertyInfo.SetValue
需要 object
,因此您不必转换 constructorInfo.Invoke(null)
:
var listInstance = constructorInfo.Invoke(null);
property.SetValue(this, listInstance);