如何获得泛型类中的属性和值?

时间:2018-08-13 16:24:29

标签: c# generics

在这里调用函数并创建对象 也许通过这种方式,您可以了解我正在尝试做什么

     class Program 
        {

            static void Main(string[] args)
            {

                 //here I create an object and pass the type Person
                var Crud = new Crud<Person>();
                //here I invoke the method Create and pass the object "Person"
                Crud.Create(new Person(Crud.Id) { Name = "Sameone", Age = 24 });
//here I invoke the method again to Create and pass an other object "Person"
                    Crud.Create(new Person(Crud.Id) { Name = "Sameone2", Age = 24 });

               With this method Read() I get the List created back
                var Lista = Crud.Read();
                Lista.ForEach((x) => Console.WriteLine("{0}  {1}",x.Id.ToString(), x.Name));

              /* Here is where my problem starts I'm sending an new object that will replace the object with Id =1 passed by constructor   */
                Crud.Update(new Person(1) { Name = "someone3", Age = 20 });




                Console.ReadKey();
            }
}

这是我尝试使用此类的地方

//此类是通用的,其中T是传递的“ Person”类型

   public class Crud <T>: ICrud <T> {

      private List <T> List = new List <T> ();

      public MessageError Update(T object) {
    //Here it doesn't work its impossible to get the Id property sense its an generic object at this point
       List.RemoveAll(x => x.Id == t.Id);
       List.Add(t);
       return new MessageError {
        Status = Status.Sucessful
       };
      }
     }

我的代码中使用的具体类型

 public class Person
    {
        public int Id { get; private set; }
        public string Name { get; set; }
        public int Age { get; set; }
        //Here I set the id for each object
        public Person(int _id)
        {
            Id = _id;
        }
    }

在这里您可以看到界面

 interface ICrud<T>
    {
        MessageError Create(T t);
        List<T> Read();
        MessageError Update(T t);
        MessageError Delete(int Id);
    }

我现在尝试使用like,但仍然无法正常工作

 List.RemoveAll(x => x.GetProperty("Id") == t.GetProperty("Id"));

我想这是解决方案

public MessageError Update(T t)
    {
        var type = t.GetType();

        PropertyInfo prop = type.GetProperty("Id");

        var value = prop.GetValue(t);    
        List.RemoveAll(x => x.GetType().GetProperty("Id").GetValue(x) == value);
        List.Add(t);
        return new MessageError
        {
            Status = Status.Sucessful
        };
    }

2 个答案:

答案 0 :(得分:2)

我认为也许您只是想走一步。 CRUD代表创建,读取,更新,删除。在这里,您正在创建一些数据,读取所有内容,然后尝试将更新直接发送到数据存储。您应该做的是,在您的个人中阅读,更改其一个或多个属性,然后更改Crud.Update(person),这反过来会知道该个人需要更新而不是在数据存储区中创建。

实际上,根据实体是否具有其ID值来区分“创建”和“更新”是相当正常的做法。在这种情况下,您的代码可能如下所示:

class Program 
{
    static void Main(string[] args)
    {
        TestClass testClass = new TestClass();
        testClass.Create();

        var crud = new Crud<Person>();

        // Note we're not specifying an ID for *new* entities, the Crud class needs to assign it upon saving
        crud.Create(new Person() { Name = "Someone", Age = 24 });
        crud.Create(new Person() { Name = "Someone2", Age = 24 });

        var list = crud.ReadAll();
        list.ForEach(x => Console.WriteLine("{0} {1}", x.Id, x.Name));

        var person1 = list.Single(x => x.Id == 1);
        person1.Name = "Someone3";
        person1.Age = 20;

        Crud.Update(person1);

        Console.ReadKey();
    }
}

尽管这里需要进行进一步的优化。这会将所有对象从数据存储区加载到应用程序的内存中,然后将遍历每个对象,直到找到所需对象为止。如果正在使用的数据存储可以为您做到这一点会更好。在这种情况下,您将可以做更多类似var person1 = crud.FindById(1)的事情。有很多其他技术可以解决这个问题,但是我不想让这个答案进一步复杂化。

答案 1 :(得分:1)

public MessageError Update(T t)
    {
/* I used this why to the the property in my last version I get the name of property by Parameter */
        var type = t.GetType();
        PropertyInfo prop = type.GetProperty("Id");

        var value = prop.GetValue(t);    
        List.RemoveAll(x => x.GetType().GetProperty("Id").GetValue(x) == value);
        List.Add(t);
        return new MessageError
        {
            Status = Status.Sucessful
        };
    }