Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Accessors from an Object

Tags:

c#

Give the following class:

public Person
{
 public string Name {get; set;}
 public string Age {get; set; }
}

If I wanted to then Return a IList is there anyway to return the List will all the Functionality of an IList but with the setters removed? Possibly with an extension method to remove the setters. I would Imagine you would have to use reflection but is this the only way that this could be done? Would it be feasible from a speed/implementation point of view.

I was just wondering how this could be done, it is not an actual design. And I am aware of just returning an IEnumerable, or IEnumerable with a skip or yield that would solve for this problem.

Thanks

like image 550
JJK Avatar asked Nov 22 '25 03:11

JJK


1 Answers

with the setters removed from the Person class? create

IReadonlyPerson
{
  string Name { get; }
  string Age { get; }
}

have Person implement it (it already does), then return a IList. if you are talking about the mutability of the IList look at @RPM1984's answer.

like image 57
Alex Lo Avatar answered Nov 23 '25 17:11

Alex Lo