本文整理汇总了C#中Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithTarget方法的典型用法代码示例。如果您正苦于以下问题:C# ProxyGenerator.CreateInterfaceProxyWithTarget方法的具体用法?C# ProxyGenerator.CreateInterfaceProxyWithTarget怎么用?C# ProxyGenerator.CreateInterfaceProxyWithTarget使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Castle.DynamicProxy.ProxyGenerator
的用法示例。
在下文中一共展示了ProxyGenerator.CreateInterfaceProxyWithTarget方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildProxy
public override object BuildProxy(ProxyGenerator generator, Type objType, CommonData data, object baseObj)
{
if (!objType.IsInterface)
{
throw new Exception("Set can only be created from ISet Interface");
}
object proxy;
if (baseObj == null)
{
proxy = generator.CreateInterfaceProxyWithoutTarget(objType,
new ProxyGenerationOptions(new GeneralProxyGenerationHook())
{
Selector = _setSelector
},
new GeneralGetInterceptor(data), new SetAddInterceptor(data), new SetRemoveInterceptor(data));
}
else
{
proxy = generator.CreateInterfaceProxyWithTarget(objType, baseObj,
new ProxyGenerationOptions(new GeneralProxyGenerationHook())
{
Selector = _setSelector
},
new GeneralGetInterceptor(data), new SetAddInterceptor(data), new SetRemoveInterceptor(data));
}
return proxy;
}
示例2: InitializeProxy
public void InitializeProxy()
{
IBar bar = new Bar();
var pg = new ProxyGenerator();
this.BarInterfaceProxy = pg.CreateInterfaceProxyWithTarget(bar, new CacheInterceptor());
this.FooBarProxy = pg.CreateClassProxy<FooBar>(new CacheInterceptor());
}
示例3: Booting
private static void Booting(Container container)
{
container.Configure(config =>
{
var proxyGenerator = new ProxyGenerator();
config.For<IRepository>().EnrichAllWith(
repository => proxyGenerator.CreateInterfaceProxyWithTarget(repository, new RepositoryInterceptor())).
Use<Repository>();
});
}
示例4: CanCreateInterfaceProxyWithTarget
public void CanCreateInterfaceProxyWithTarget() {
var generator = new ProxyGenerator();
var proxy = (IMyInterface)generator.CreateInterfaceProxyWithTarget(typeof(IMyInterface),
new MyInterfaceImpl(),
new MyInterceptor());
Assert.IsNotNull(proxy);
Assert.IsInstanceOf<IMyInterface>(proxy);
Assert.AreEqual(10, proxy.Calc(5, 5));
}
示例5: When_calling_the_method_it_gets_intercepted
public void When_calling_the_method_it_gets_intercepted()
{
var dp = new ProxyGenerator();
var outputResult = new OutputToList();
var target = new ElementToBeIntercepted(outputResult);
var interceptor = new LogInterceptorWithoutProceed(outputResult);
var proxy = dp.CreateInterfaceProxyWithTarget<IElementToBeIntercepted>(target, interceptor);
proxy.MethodOne();
Assert.Equal(outputResult.OutputText.Count, 1);
Assert.Contains("The method MethodOne has been intercepted", outputResult.OutputText.First());
}
示例6: defaultContainer
private static Container defaultContainer()
{
return new Container(ioc =>
{
var dynamicProxy = new ProxyGenerator();
ioc.For<IMyService>()
.DecorateAllWith(myService =>
dynamicProxy.CreateInterfaceProxyWithTarget(myService, new CacheInterceptor()))
.Use<MyService>();
});
}
示例7: CastleTestInterfaces
public void CastleTestInterfaces()
{
var proxyGenerator = new ProxyGenerator();
var originalObj = new SimpleSpecialDisposable();
var newObj = proxyGenerator.CreateInterfaceProxyWithTarget(typeof(IDisposable), originalObj, Enumerable.Empty<IInterceptor>().ToArray());
Assert.IsAssignableFrom<IDisposable>(newObj);
Assert.Throws<InvalidCastException>(() => (ISpecialDisposable)newObj);
Assert.Throws<InvalidCastException>(() => (SimpleSpecialDisposable)newObj);
}
示例8: Main
static void Main(string[] args)
{
ProxyGenerator generator = new ProxyGenerator();
IPerson person = new Person { Age = 21 };
person = generator.CreateInterfaceProxyWithTarget(person, new PersonProxy());
var phoneNumbers = person.PhoneNumbers;
bool notNull = phoneNumbers != null;
}
示例9: Many_interceptors_can_be_applied
public void Many_interceptors_can_be_applied()
{
var dp = new ProxyGenerator();
var outputResult = new OutputToList();
var target = new ElementToBeIntercepted(outputResult);
var interceptor = new LogInterceptorWithProceed(outputResult);
var secondInterceptor = new OtherLogInterceptor(outputResult);
var proxy = dp.CreateInterfaceProxyWithTarget<IElementToBeIntercepted>(target, interceptor, secondInterceptor);
proxy.MethodOne();
Assert.Equal(outputResult.OutputText.Count, 3);
Assert.Contains("The method MethodOne has been intercepted", outputResult.OutputText.First());
Assert.Contains("with OtherLogInterceptor", outputResult.OutputText.Skip(1).First());
Assert.Contains("Called MethodOne", outputResult.OutputText.Last());
}
示例10: defaultContainer
private static Container defaultContainer()
{
return new Container(ioc =>
{
var dynamicProxy = new ProxyGenerator();
ioc.Scan(scanner =>
{
scanner.AssemblyContainingType<IMyType>(); // نحوه يافتن اسمبلي لايه سرويس
// Connect `IName` interface to 'Name' class automatically
scanner.WithDefaultConventions();
});
ioc.For<IMyType>().DecorateAllWith(
myType => dynamicProxy.CreateInterfaceProxyWithTarget(myType, new LoggingInterceptor()));
});
}
示例11: MethodInvocationWithGenericParameterTest
public void MethodInvocationWithGenericParameterTest(int iterations)
{
var proxyGenerator = new ProxyGenerator();
var interceptors = new IInterceptor[] {new CastleInterceptor()};
var target = new Generic();
var proxy = proxyGenerator.CreateInterfaceProxyWithTarget<IGeneric>(target, interceptors);
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < iterations; i++)
{
proxy.Invoke(i);
}
stopwatch.Stop();
Report.Instance.Write(AssemblyName, Scenario.MethodInvocationWithGenericParameter, iterations, stopwatch.Elapsed);
}
示例12: Main
static void Main()
{
// ObjectFactory is obsolete
//ObjectFactory.Initialize(x =>
//{
// x.For<ITestClass>().Use<TestClass>();
//});
Container container = new Container(x =>
{
x.For<ITestClass>().Use<TestClass>().Ctor<string>("name").Is("123").Ctor<string>("lastName").Is("456");
ProxyGenerator proxyGenerator = new ProxyGenerator();
x.For<ITestClass>()
.DecorateAllWith(
myInterface => proxyGenerator.CreateInterfaceProxyWithTarget(myInterface, new Loging()));
});
container.GetInstance<ITestClass>().Login("test", 10);
}
示例13: Main
public static void Main(string[] args)
{
// Add Console.Out as a listener for Debug.WriteLine to show in Mono
Debug.Listeners.Add( new ConsoleTraceListener(useErrorStream: false) );
// Using dependency injection to specify output stream
// i.e. Foo proxies ILogger
ILogger logger = new Logger (Console.Out);
Foo foo = new Foo (logger);
foo.Write ("Testing Foo\n");
Console.WriteLine("Hard-coded proxy object demonstrating trace logging:");
// You could create a second foo type which appears to be IFoo
// but actually traces method enter/exit by proxy
IFoo foo2 = new FooInterfaceProxy (logger);
foo2.Write ("Testing FooInterfaceProxy #1");
foo2.Write ("Testing FooInterfaceProxy #2");
Console.WriteLine("Hard-coded proxy object demonstrating added functionality:");
IFoo foo3 = new FooClassProxy(logger);
foo3.Write("Testing FooClassProxy");
Console.WriteLine("Dynamically proxy an existing object's virtual members (any object works):");
var proxify = new ProxyGenerator();
var interceptors = new IInterceptor[] { new DebugLogger() };
Foo foo4 = proxify.CreateClassProxyWithTarget(foo, interceptors);
foo4.Write("Class Foo proxied dynamically using CreateClassProxyWithTarget");
Console.WriteLine("Calling Class Foo's ToString(): {0}", foo4.ToString());
Console.WriteLine("Calling IFoo.ShowBar(): {0}", foo4.ShowBar());
Console.WriteLine("Calling Foo.ShowBaz(): {0}\n", foo4.ShowBaz());
Console.WriteLine ("Dynamic Proxy a specific target:");
IFoo foo5 = (IFoo)proxify.CreateInterfaceProxyWithTarget(typeof(IFoo), foo, interceptors);
foo5.Write("Class Foo proxied dynamically using CreateInterfaceProxyWithTarget");
Console.WriteLine("Calling Class Foo's ToString(): {0}\n", foo5.ToString());
Console.WriteLine("Calling IFoo.ShowBar(): {0}", foo5.ShowBar());
Console.WriteLine("Notice the order of interception above.");
}
示例14: Initialize
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
x.For<IPrincipalService>().Use<IrisSupportPrincipalService>();
x.For<IFormsAuthenticationService>().Use<FormsAuthenticationService>();
var dynamicProxy = new ProxyGenerator();
x.For<IUnitOfWork>().HttpContextScoped().Use<IrisDbContext>();
x.For<IUserService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<UserService>();
x.For<IRoleService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<RoleService>();
x.For<IPostService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<PostService>();
x.For<IBookService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<BookService>();
x.For<ILabelService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<LabelService>();
x.For<IDownloadLinkService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<DownloadLinkService>();
x.For<ICommentService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<CommentService>();
x.For<IAnonymousUser>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<AnounymousUserService>();
x.For<IPageService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<PageService>();
x.For<IOptionService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<OptionService>();
x.For<IPageService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<PageService>();
x.For<ICategoryService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<CategoryService>();
x.For<IArticleService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<ArticleService>();
x.For<IForgottenPasswordService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<ForgottenPasswordService>();
x.For<IMessageService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<MessageService>();
x.For<ICacheService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<CacheService>();
x.For<IEmailService>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<EmailService>();
x.For<IViewConvertor>().EnrichAllWith(myTypeInterface =>
dynamicProxy.CreateInterfaceProxyWithTarget(myTypeInterface, new CacheInterceptor())).Use<ViewConvertor>();
});
return ObjectFactory.Container;
}
示例15: Main
static void Main(string[] args)
{
var instance = Activator.CreateInstance<Model>();
instance.String1 = "Test";
Console.WriteLine(instance.String1);
var proxyGenerator = new ProxyGenerator();
var options = new ProxyGenerationOptions(new ProxyGenerationHook());
var instanceMissing = proxyGenerator.CreateInterfaceProxyWithTarget<IModel>(new Model(), options, new MethodMissingInterceptor());
instanceMissing.String1 = "Test2";
instanceMissing.Get();
Console.WriteLine(instanceMissing.String1);
TestabilityTest.CreateUntestableTestableObject();
TestabilityTest.CreateUntestableTestableFileReader();
Console.ReadLine();
}