, . moq , - castle , .Object. :
public object Object
{
get { return this.GetObject(); }
}
private object GetObject()
{
var value = this.OnGetObject();
this.isInitialized = true;
return value;
}
protected override object OnGetObject()
{
if (this.instance == null)
{
this.InitializeInstance();
}
return this.instance;
}
:
private void InitializeInstance()
{
PexProtector.Invoke(() =>
{
this.instance = proxyFactory.CreateProxy<T>(
this.Interceptor,
this.ImplementedInterfaces.ToArray(),
this.constructorArguments);
});
}
ProxyFactory
public T CreateProxy<T>(ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
{
var mockType = typeof(T);
if (mockType.IsInterface)
{
return (T)generator.CreateInterfaceProxyWithoutTarget(mockType, interfaces, new Interceptor(interceptor));
}
try
{
return (T)generator.CreateClassProxy(mockType, interfaces, new ProxyGenerationOptions(), arguments, new Interceptor(interceptor));
}
catch (TypeLoadException e)
{
throw new ArgumentException(Resources.InvalidMockClass, e);
}
catch (MissingMethodException e)
{
throw new ArgumentException(Resources.ConstructorNotFound, e);
}
}