Why methods need to be declared virtual

Visto 63 veces
Saltar al primer mensaje no leído

vishy

no leída,
16 ene 2008, 11:51:3516/1/08
a 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

no leída,
16 ene 2008, 11:57:1116/1/08
a 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

no leída,
16 ene 2008, 12:29:0116/1/08
a 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

no leída,
16 ene 2008, 12:39:2516/1/08
a Rhino...@googlegroups.com
Dynamic Proxy subclass the class at runtime, you were right the first time.

isaiah perumalla

no leída,
16 ene 2008, 12:42:1016/1/08
a Rhino...@googlegroups.com
thanks for clarifying that
--
Isaiah Perumalla
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos