Get item from list <T> based on id

This question is related to this question: Given System.Type T, Deserialize List <T>

Given this function to retrieve a list of all elements ...

 public static List<T> GetAllItems<T>()
 {
    XmlSerializer deSerializer = new XmlSerializer(typeof(List<T>));
    TextReader tr = new StreamReader(GetPathBasedOnType(typeof(T)));
    List<T> items = (List<T>)deSerializer.Deserialize(tr);
    tr.Close();
 }

... I want to create a function to extract only one element from them with the necessary UID (unique identifier):

public static System.Object GetItemByID(System.Type T, int UID)
{

    IList mainList = GetAllItems<typeof(T)>();
    System.Object item = null;

    if (T == typeof(Article))
        item = ((List<Article>)mainList).Find(
            delegate(Article vr) { return vr.UID == UID; });
    else if (T == typeof(User))
        item = ((List<User>)mainList).Find(
            delegate(User ur) { return ur.UID == UID; });

    return item;
}

However, this does not work, since the call is GetAllItems<typeof(T)>();not correctly formed.

Question 1a . How can I fix the second function to correctly return a unique element, given that all classes that will call GetItemByID () have a UID as an element in them? I would like to, if possible public static <T> GetItemByID<T>(int UID).

Question 1b : the same question, but suppose I cannot change the prototype of the GetItemByID function?

+3
7

1. , T- IUniqueIdentity, UID, IUniqueIdentity. :

public static T GetItemById<T>(int UID) where T:IUniqueIdentity
{
    IList<T> mainList = GetAllItems<T>();

    //assuming there is 1 or 0 occurrences, otherwise FirstOrDefault
    return mainList.SingleOrDefault(item=>item.UID==UID); 

}

public interface IUniqueIdentity
{
    int UID{get;}
}
+3

, , , GetItemByID(), UID ?

GetItemByID : public static T GetItemByID<T>(int UID)

, , GetItemByID

GetAllItems<typeof(T)>() , , T . MethodInfo.MakeGenericMethod(), Type T.

public static System.Object GetItemByID(System.Type type, int UID) 
{
    Type ex = typeof(YourClass);
    MethodInfo mi = ex.GetMethod("GetItemByID");

    MethodInfo miConstructed = mi.MakeGenericMethod(type);

    // Invoke the method.
    object[] args = new[] {UID};
    return miConstructed.Invoke(null, args);
}
+1

1A , UID

interface IHasUID { public int UID { get; } }

Article, User , ;

public static T GetItemByID<T>(int UID) where T : IHasUID

Article, User .., , **, -.

0

- Reflection, :

public static System.Object GetItemByID(System.Type T, int UID)
{

    IList mainList = GetAllItems().OfType<typeof(T)>();

    return mainList.Find(i => (int)(i.GetType().GetFields().Where(f => f.Name == "UID").FirstOrDefault().GetValue(i)) == UID);

}

Reflection UID.

, Article User, , UID, .

0

, , - . , , , MVC5:

...
List<TodoModel> todos = new List<TodoModel>() {
    new TodoModel() {
        id = 0,
        name = "Laundry!",
        desc = "Whites and darks."
    },
    new TodoModel() {
        id = 1,
        name = "Dishes",
        desc = "Oh, first buy dishes."
    },
    new TodoModel() {
        id = 2,
        name = "Twitter Bootstrap",
        desc = "Check out Grids."
    }
};

ViewBag.Item = todos.Find(o => ((TodoModel)o).id == id);

return View();
...

A simplified example, of course, but thought it might come in handy for the Questionnaire or others.

0
source

System.Type is already a type, no need to do typeof.

then I would do: if (T == typeof (Article)) item = ((List) mainList) .Find (vr => vr.UID == UID);

-1
source

Source: https://habr.com/ru/post/1773709/


All Articles