如何在泛型类中获取Property和Value?

问题描述 投票:0回答:2

这里是调用函​​数并创建对象的地方也许这样你可以看到我正在尝试做什么

     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);
    }

我现在尝试使用但仍然不工作

 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
        };
    }
c# generics
2个回答
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
投票
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
        };
    }
© www.soinside.com 2019 - 2024. All rights reserved.