make method reflection for search in model class C#

0 thích 0 không thích
1 lượt xem
đã hỏi 22 Tháng 2, 2023 trong Lập trình C# bởi nguyenthao (9,040 điểm)

Sure, I'd be happy to help you write a method in C# that uses reflection to search for a given term in all properties of a class.

Here's an example of how you can use reflection to search for a given term in all properties of a class in C#:
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    public string Occupation { get; set; }

    public Person(string name, int age, string occupation) {
        Name = name;
        Age = age;
        Occupation = occupation;
    }
}

class Program {
    static List<Person> SearchForTermInProperties<T>(List<T> items, string searchTerm) {
        Type type = typeof(T);
        PropertyInfo[] properties = type.GetProperties();

        return items.Where(item => properties.Any(property => {
            var value = property.GetValue(item);
            if (value is string stringValue) {
                return stringValue.Contains(searchTerm, StringComparison.OrdinalIgnoreCase);
            } else {
                return value != null && value.ToString().Contains(searchTerm, StringComparison.OrdinalIgnoreCase);
            }
        })).Cast<Person>().ToList();
    }

    static void Main(string[] args) {
        List<Person> people = new List<Person> {
            new Person("John", 30, "Engineer"),
            new Person("Jane", 25, "Doctor"),
            new Person("Bob", 40, "Lawyer")
        };

        List<Person> searchResults = SearchForTermInProperties(people, "eng");

        Console.WriteLine(string.Join(", ", searchResults.Select(person => person.Name))); // prints "John"
    }
}
 


In this example, we define a new method called SearchForTermInProperties that takes a list of generic type T (which can be any class with properties) and a search term as input. The method uses reflection to get all the properties of the type T, and then uses the Where method of the items list to filter the list based on whether any of the properties contain the search term.

To check whether a given property contains the search term, we use the GetValue method of the PropertyInfo object to get the value of the current property. We then check whether the property is a string or not, and use the Contains method of the string class to check whether it contains the search term.

Note that using reflection for search operations can be expensive, especially for large lists or complex objects, so you should use this technique with caution and only when necessary. Also, keep in mind that the code above assumes that all the properties of the given class are public, so you may need to modify it to handle private or protected properties if necessary.

    

1 câu trả lời

0 thích 0 không thích
đã trả lời 22 Tháng 2, 2023 bởi nguyenthao (9,040 điểm)
it answer by Chat GPT
...