2

我正在尝试查看是否可以使用 Linq 搜索具有任何给定对象的 .ToString() 方法的所有公共变量。

基本上我想提供两个参数,一个List<T>用于搜索和一个string并返回一个List<T>对象,这些对象具有一个或多个包含我的字符串的公共变量。

我可以使用获取对象的所有公共属性

Type objectType = _data[0].GetType();
var properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
var fields = objectType.GetFields(BindingFlags.Public | BindingFlags.Instance)

但是我一直坚持如何使用它来搜索对象列表,而无需遍历所有对象及其所有属性。

4

2 回答 2

5

这是一个完整的例子:

(注意:我将编辑此答案以考虑ToString()覆盖)

编辑:完成

public class Program
{
    public class ToStringNotOverrided0 { }
    public int Int { get; set; }
    public string str;
    public ToStringNotOverrided0 toStringNotOverrided;

    public static IEnumerable<T> getMatches<T>(List<T> list, string search) {
        if (search == null)
            throw new ArgumentNullException("search");
        return list.Select(x => new
        {
            X = x,
            Props = x.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public),
            Fields = x.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public),
        })
        .Where(x => x.Props.Any(p =>
                            {
                                var val = p.GetValue(x.X, null);
                                return val != null
                                    && val.GetType().GetMethod("ToString", Type.EmptyTypes).DeclaringType == val.GetType()
                                    && val.ToString().Contains(search);
                            })
                    || x.Fields.Any(p =>
                            {
                                var val = p.GetValue(x.X);
                                return val != null
                                    && val.GetType().GetMethod("ToString", Type.EmptyTypes).DeclaringType == val.GetType()
                                    && val.ToString().Contains(search);
                            }))
        .Select(x => x.X);
    }

    static void Main(string[] args)
    {
        List<Program> list = new List<Program>{
            new Program { Int = 0, str = "foo bar" , toStringNotOverrided = new ToStringNotOverrided0()},
            new Program { Int = 54, str = "foo 0 bar"  , toStringNotOverrided = new ToStringNotOverrided0()},
            new Program { Int = 12, str = "foo bar"  , toStringNotOverrided = new ToStringNotOverrided0()},
            new Program { Int = 720, str = "foo bar" , toStringNotOverrided = new ToStringNotOverrided0() }
        };
        foreach (var item in getMatches(list, "0"))
        {
            Debug.WriteLine("Int = " + item.Int 
                + ", str = " + item.str
                + ", toStringNotOverrided = " + item.toStringNotOverrided);
        }
    }
}
于 2013-10-18T14:25:07.600 回答
0
var type = objectToCheck.GetType();
bool isMethodExist = type.GetMethod(methodName) != null;

如果你有List<T>

var type = _data.GetType().GetGenericArguments()[0];
bool isMethodExist = type.GetMethod(methodName) != null;
于 2013-10-18T14:17:47.143 回答