c# - 如何在泛型类中获取属性和值?
问题描述
这是调用函数并创建对象的地方也许这样你就可以看到我正在尝试做的事情
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
};
}
解决方案
我想也许你只是错过了一步。CRUD 代表创建、读取、更新、删除。在这里,您正在创建一些数据,读取所有内容,然后尝试将更新直接发送到数据存储。您应该做的是,在您的 Persons 中读取,更改他们的一个或多个属性,然后Crud.Update(person)
,这反过来将知道该人需要更新而不是在数据存储中创建。
事实上,根据实体的 ID 是否具有值来区分 Create 和 Update 是相当正常的做法。在这种情况下,您的代码可能如下所示:
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)
. 有很多其他技术可以解决这个问题,但我不想让这个答案进一步复杂化。
推荐阅读
- android - Firebase 通知在我的 android 应用程序中不起作用
- python - 在 Spyder 中访问模式库
- javascript - Try / Catch 不会捕获已声明的常量错误
- laravel - 测试 Laravel Nova
- php - 使用 php 将 doc 文件转换为 PDF
- javascript - 多个元素上的相同 javascript 函数,只有最后一个触发 | 特别说明
- feathersjs - RS256 jwt 证书不适用于 FeathersJS
- javascript - 在 Selenium 中测试一个自删除/动态 Webelement
- java - 拆分 Json 输出并将其分配给两个不同的对象
- jquery - 使用 ajax 将数据发布到控制器