Refresh a specific column of the <T> list

At first they forgave me for my English, as that is not my native language. I have a method that gets a general list List<T>. I want to view the entire list and be able to update the Excluded column from each class Tand which is of type boolean, can this be done? can anyone help me.

This is what I have so far:

// Delete (Change status delete = true)
public void Delete<T>(List<T> data)
{      
    if (data != null)
    {
        data.ForEach(x =>
        {
           ...
        });
    }           
}

Thanks in advance!

+6
source share
3 answers

Instead, TI would use an interface, because otherwise foreachyou cannot access the property Eliminated.

Here's the interface:

interface IExample {

    bool IsEliminated { get; set; }    

}

foreach.

public void Delete<T>(List<T> data) where T : IExample
{      
    if (data != null)
    {
        data.ForEach(x =>
        {
           x.Eliminated = true;
        });
    }           
}
+11

, - :

public void Update<T>(List<T> data, Action<T> func)
{
    if (data == null)
        throw new ArgumentNullException(nameof(data));

    data.ForEach(func);
}

. , . , , .

, . :

var data = new List<YourClass> = GetData();

Update(data, item => item.Eliminated = true);
+4

T , Eliminated. , T, - , , .

T, , :

public interface Eliminatable
{
    bool Eliminated { get; set; }
}

public void Delete<T>(List<T> data) where T : Eliminatable
{      
    if (data != null)
    {
        data.ForEach(x => { x.Eliminated = true; });
    }           
}

( , ), , - T, :

public void Delete<T>(List<T> data)
{      
    if (data != null)
    {
        data.ForEach(x => { dynamic d = x; d.Eliminated = true; });
    }           
}

Now this will fail if the property is not there. At runtime. Not good. But it "works."

+3
source

All Articles