diff --git a/Rhino.Mocks/MockRepository.cs b/Rhino.Mocks/MockRepository.cs
index 53a16e40..53553c0d 100644
--- a/Rhino.Mocks/MockRepository.cs
+++ b/Rhino.Mocks/MockRepository.cs
@@ -855,11 +855,7 @@ private void IsMockObjectFromThisRepository(object obj)
///
protected internal static IMockedObject GetMockedObject(object mockedInstance)
{
- IMockedObject mockedObj = GetMockedObjectOrNull(mockedInstance);
- if (mockedObj == null)
- throw new InvalidOperationException("The object '" + mockedInstance +
- "' is not a mocked object.");
- return mockedObj;
+ return mockedInstance.AsMockObject();
}
///
@@ -869,26 +865,7 @@ protected internal static IMockedObject GetMockedObject(object mockedInstance)
///
protected internal static IMockedObject GetMockedObjectOrNull(object mockedInstance)
{
- Delegate mockedDelegate = mockedInstance as Delegate;
-
- if (mockedDelegate != null)
- {
- mockedInstance = mockedDelegate.Target;
- }
-
- // must be careful not to call any methods on mocked objects,
- // or it may cause infinite recursion
- if (mockedInstance is IMockedObject)
- {
- return (IMockedObject)mockedInstance;
- }
-
- if (RemotingMockGenerator.IsRemotingProxy(mockedInstance))
- {
- return RemotingMockGenerator.GetMockedObjectFromProxy(mockedInstance);
- }
-
- return null;
+ return mockedInstance.AsMockObjectOrNull();
}
/// Pops the recorder.
diff --git a/Rhino.Mocks/RhinoMocksExtensions.cs b/Rhino.Mocks/RhinoMocksExtensions.cs
index 5e796b6c..7f546959 100644
--- a/Rhino.Mocks/RhinoMocksExtensions.cs
+++ b/Rhino.Mocks/RhinoMocksExtensions.cs
@@ -30,7 +30,8 @@
using System;
using System.Collections.Generic;
using Rhino.Mocks.Exceptions;
-using Rhino.Mocks.Generated;
+using Rhino.Mocks.Generated;
+using Rhino.Mocks.Impl.RemotingMock;
using Rhino.Mocks.Interfaces;
@@ -482,7 +483,47 @@ private VoidType()
}
}
- #endregion
+ #endregion
+
+ internal static IMockedObject AsMockObject(this object mockedInstance)
+ {
+ IMockedObject mockedObj = mockedInstance.AsMockObjectOrNull();
+ if (mockedObj == null)
+ throw new InvalidOperationException("The object '" + mockedInstance +
+ "' is not a mocked object.");
+ return mockedObj;
+ }
+
+ ///
+ /// Method: GetMockedObjectOrNull
+ /// Get an IProxy from a mocked object instance, or null if the
+ /// object is not a mock object.
+ ///
+ internal static IMockedObject AsMockObjectOrNull(this object mockedInstance)
+ {
+ Delegate mockedDelegate = mockedInstance as Delegate;
+
+ if (mockedDelegate != null)
+ {
+ mockedInstance = mockedDelegate.Target;
+ }
+
+ // must be careful not to call any methods on mocked objects,
+ // or it may cause infinite recursion
+ if (mockedInstance is IMockedObject)
+ {
+ return (IMockedObject)mockedInstance;
+ }
+
+ if (RemotingMockGenerator.IsRemotingProxy(mockedInstance))
+ {
+ return RemotingMockGenerator.GetMockedObjectFromProxy(mockedInstance);
+ }
+
+ return null;
+ }
+
+
}
}
#endif