C# 创建用于将项添加到哈希集的非泛型函数<;T>;

C# 创建用于将项添加到哈希集的非泛型函数<;T>;,c#,generics,reflection,hashset,C#,Generics,Reflection,Hashset,在我的C#程序中,我想通过反射将项添加到HashSet。使用列表这不是问题,因为我可以将列表强制转换到非通用IList界面: foreach (PropertyInfo property in myClass.GetType().GetProperties()) { object value = property.GetValue(myClass); IList valueAsIList = value as IList; if (valueAsIList != null

在我的C#程序中,我想通过反射将项添加到
HashSet
。使用
列表
这不是问题,因为我可以将列表强制转换到非通用
IList
界面:

foreach (PropertyInfo property in myClass.GetType().GetProperties())
{
    object value = property.GetValue(myClass);
    IList valueAsIList = value as IList;
    if (valueAsIList != null)
        valueAsIList.Add(item2Insert);
}

现在我想对
HashSet
做同样的事情,但是没有像
IList
这样的非通用契约,我可以将它转换为
Add
方法。还有其他方法吗?

既然您已经在使用反射,为什么不尝试查找Add方法呢

var addMethod = value.GetType().GetMethods().FirstOrDefault(m => m.Name == "Add");

//validate this method; has it been found? What should we do if it didnt? Maybe it should be SingleOrDefault

addMethod.Invoke(value, valueToAdd)

可能会添加更多的验证,还有什么没有…)

既然您已经在使用反射,为什么不尝试查找Add方法呢

var addMethod = value.GetType().GetMethods().FirstOrDefault(m => m.Name == "Add");

//validate this method; has it been found? What should we do if it didnt? Maybe it should be SingleOrDefault

addMethod.Invoke(value, valueToAdd)

可能会添加更多的验证,还有什么没有…)

您可以使用
动态
解决此问题。它将“照顾”你的反思工作

using System;
using System.Collections.Generic;

namespace Bob
{
    public class Program
    {
        static void Main(string[] args)
        {
            var hash = new HashSet<int>();
            Console.WriteLine(hash.Count);
            Add(hash);
            Console.WriteLine(hash.Count);
            Console.ReadLine();
        }

        private static void Add(dynamic hash)
        {
            hash.Add(1);
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间Bob
{
公共课程
{
静态void Main(字符串[]参数)
{
var hash=new HashSet();
Console.WriteLine(hash.Count);
添加(散列);
Console.WriteLine(hash.Count);
Console.ReadLine();
}
私有静态无效添加(动态哈希)
{
添加(1);
}
}
}

您可以使用
动态
解决此问题。它将“照顾”你的反思工作

using System;
using System.Collections.Generic;

namespace Bob
{
    public class Program
    {
        static void Main(string[] args)
        {
            var hash = new HashSet<int>();
            Console.WriteLine(hash.Count);
            Add(hash);
            Console.WriteLine(hash.Count);
            Console.ReadLine();
        }

        private static void Add(dynamic hash)
        {
            hash.Add(1);
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间Bob
{
公共课程
{
静态void Main(字符串[]参数)
{
var hash=new HashSet();
Console.WriteLine(hash.Count);
添加(散列);
Console.WriteLine(hash.Count);
Console.ReadLine();
}
私有静态无效添加(动态哈希)
{
添加(1);
}
}
}

A
Hashset
也是一个
ICollection
@stuartd和Chris:OP提到了
IList
,因为
List
实现了非泛型
IList
接口,该接口可用于向集合添加非类型化对象。但是HashSet
并没有实现这样一个非通用的契约。啊,我没有完全理解这个问题,感谢您的注意。
Hashset
也是一个
ICollection
@stuartd和Chris:OP提到了
IList
,因为
List
实现了非泛型的
IList
接口,可用于向集合添加非类型化对象。但是
HashSet
没有实现这样一个非泛型契约。啊,我没有完全理解这个问题,谢谢你的提醒。注意
value
是对象,而不是类型,所以你必须执行
value.GetType().GetMethods()…
。此外,您还可以使用(单数)直接获取方法,而无需查询所有方法。已修复,谢谢!!关于特定的GetMethod(“Add”)也可以,但是您还必须捕获含糊不清的MatchException注意,
value
是对象,而不是类型,因此您必须执行
value.GetType().GetMethods()…
。此外,您还可以使用(单数)直接获取方法,而无需查询所有方法。已修复,谢谢!!关于特定的GetMethod(“Add”)也可以,但是您还必须捕获含糊不清的MatchException