C# - change behavior of property setter at runtime -


i've run contrived coding problem, , hope can tell me if want possible in standard c#.

i have class stored list of foo objects , made updates them so:

public class program {     private list<foo> listoffoo;      ...      public void somemethod()     {         var fooobject = (foo)(listoffoo[0]);         fooobject.name = "name";         fooobject.address = "address;"     } } 

i wanted replace list<foo> custom class foorepository uses backing list of objects of type foodto generated foo, without making changes how listoffoo used in calling code:

public class foorepository : ienumerator<foo>, ienumerable<foo> {     private list<foodto> backinglist;      ...      public void add(foo foo)     {         backinglist.add(maptodto(foo));     }      public foo this[int i]     {                 {             return mapfromdto(backinglist[i]);         }     }     ... } 

but causes problem how updates objects performed in calling code, since accessing repository doesn't return reference actual object in backing list, newly-generated object instead. question is: keeping basic design, without modifying in calling code or in foo itself, can somehow modify foorepository update in somemethod still permanently committed repository?

i store 2 lists in foorepository, 1 foodtos , 1 foos, , commit changes in foo list foodtos each time called method on repository, let's assume don't want that. seems basic strategy accesses repository should return object can treated foo, has additional behavior of property assignments being committed backing foodto. if c# supported arbitrary runtime monkey patching, like:

public foo this[int i] {         {         return mapfromdto(backinglist[i]).forallproperties(property =>         {             property.newsetter(value => backinglist[i].property = value; ...);         }     } } 

but doesn't. thought using system.reflection.emit generate new child class of foo overrides property setters, none of properties in foo marked virtual, believe can't done. construct dynamic object implements in foo plus new setter behavior , use library impromptuinterface treat dynamic object implementing interface, foo doesn't implement interfaces; properties coded in class itself.

it seems way accomplish want without either modifying calling code or foo this modify il code of foo's methods @ runtime. have right? have overlooked options?

thanks.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -