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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -