Why methods need to be declared virtual

67 views
Skip to first unread message

vishy

unread,
Jan 16, 2008, 11:51:35 AM1/16/08
to Rhino.Mocks
Hi,
I was mocking a concrete class and its methods.I found tha I have to
declare those methods as virtual.Why so? Is it because rhinomocks
creates a class at runtime which it inherits from the concrete class?
regards
vishy

isaiah perumalla

unread,
Jan 16, 2008, 11:57:11 AM1/16/08
to Rhino...@googlegroups.com
yep that right, the mocking framework creates a mock implementation for you conrecte class by subclassing it(your concrete class you want to mock must not be sealed) and overrides the method you want to call. this cant be done if you methods are not virtual, since all methods in C# are not virtual by default.
 
isaiah

--
Isaiah Perumalla

isaiah perumalla

unread,
Jan 16, 2008, 12:29:01 PM1/16/08
to Rhino...@googlegroups.com
sorry the framework doesnt actually subclass the concrete class, what it does is, it uses reflection to create a DynamicProxy at runtime which can only intercept virtual method calls.
--
Isaiah Perumalla

Ayende Rahien

unread,
Jan 16, 2008, 12:39:25 PM1/16/08
to Rhino...@googlegroups.com
Dynamic Proxy subclass the class at runtime, you were right the first time.

isaiah perumalla

unread,
Jan 16, 2008, 12:42:10 PM1/16/08
to Rhino...@googlegroups.com
thanks for clarifying that
--
Isaiah Perumalla
Reply all
Reply to author
Forward
0 new messages