本文整理汇总了C#中Castle.DynamicProxy.ProxyGenerator.CreateClassProxyWithTarget方法的典型用法代码示例。如果您正苦于以下问题:C# ProxyGenerator.CreateClassProxyWithTarget方法的具体用法?C# ProxyGenerator.CreateClassProxyWithTarget怎么用?C# ProxyGenerator.CreateClassProxyWithTarget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Castle.DynamicProxy.ProxyGenerator
的用法示例。
在下文中一共展示了ProxyGenerator.CreateClassProxyWithTarget方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildProxy
public override object BuildProxy(ProxyGenerator generator, Type objType, CommonData data, object baseObj)
{
object proxy;
if (baseObj == null)
{
proxy = generator.CreateClassProxyWithTarget(objType,
new ProxyGenerationOptions(new GeneralProxyGenerationHook())
{
Selector = _redisObjectSelector
},
new GeneralGetInterceptor(data), new RedisObjectSetInterceptor(data));
}
else
{
proxy = generator.CreateClassProxyWithTarget(objType, baseObj,
new ProxyGenerationOptions(new GeneralProxyGenerationHook())
{
Selector = _redisObjectSelector
},
new GeneralGetInterceptor(data), new RedisObjectSetInterceptor(data));
}
return proxy;
}
示例2: 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.");
}
示例3: CastleTestSealedClass
public void CastleTestSealedClass()
{
var proxyGenerator = new ProxyGenerator();
var originalObj = new SealedSpecialDisposable();
Assert.Throws<TypeLoadException>(() => proxyGenerator.CreateClassProxyWithTarget(originalObj, Enumerable.Empty<IInterceptor>().ToArray()));
}
示例4: CastleTestClass1
public void CastleTestClass1()
{
var proxyGenerator = new ProxyGenerator();
var originalObj = new SimpleSpecialDisposable();
var newObj = proxyGenerator.CreateClassProxyWithTarget(originalObj, Enumerable.Empty<IInterceptor>().ToArray());
Assert.IsAssignableFrom<IDisposable>(newObj);
Assert.IsAssignableFrom<ISpecialDisposable>(newObj);
Assert.IsAssignableFrom<SimpleSpecialDisposable>(newObj);
}
示例5: CastleTestReallyCrazyConstructorClass
public void CastleTestReallyCrazyConstructorClass()
{
var proxyGenerator = new ProxyGenerator();
var originalObj = new ReallyCrazyConstructorSpecialDisposable("3");
Assert.Throws<InvalidProxyConstructorArgumentsException>(
() => proxyGenerator.CreateClassProxyWithTarget(originalObj.GetType(), originalObj, new object[] { "a", "b" }, Enumerable.Empty<IInterceptor>().ToArray()));
}
示例6: CastleTestConstructorClass
public void CastleTestConstructorClass()
{
var proxyGenerator = new ProxyGenerator();
var originalObj = new CrazyConstructorSpecialDisposable("test", "test2");
var newObj = (CrazyConstructorSpecialDisposable)proxyGenerator.CreateClassProxyWithTarget(originalObj.GetType(), originalObj, new object[] { "a", "b" }, Enumerable.Empty<IInterceptor>().ToArray());
Assert.IsAssignableFrom<IDisposable>(newObj);
Assert.IsAssignableFrom<ISpecialDisposable>(newObj);
Assert.IsAssignableFrom<CrazyConstructorSpecialDisposable>(newObj);
Assert.Equal("a", newObj.A);
Assert.Equal("b", newObj.B);
}
示例7: Decorate
/// <summary>
/// Decoration request
/// </summary>
/// <param name="context">Context info</param>
/// <remarks>do not have to decorate, but may if it wants to. sorta..</remarks>
public void Decorate(DecoratorContext context)
{
if (!CanDecorate(context))
return;
var options = new ProxyGenerationOptions();
var services = context.Services;
if (IgnoreClassAsService && services.Length > 1)
services = services.Where(x => !x.IsClass).ToArray();
var generator = new ProxyGenerator();
if (services.Any(x => x.IsClass))
{
if (services.Length > 1)
throw new InvalidOperationException(
"A class that register itself as a service may not also be registered with interfaces. See the remarks in the IgnoreClassAsService property.");
var clazz = context.Services.Single(x => x.IsClass);
context.Instance = generator.CreateClassProxyWithTarget(clazz, context.Instance,
CreateInterceptor(context));
}
else
{
var others = services.Where(x => x.IsInterface).Skip(1);
var first = services.First();
context.Instance = generator.CreateInterfaceProxyWithTarget
(first, others.ToArray(), context.Instance,
CreateInterceptor(context));
}
}
示例8: CreateProxyFor
public static object CreateProxyFor(Type type, IDictionary<string, object> data)
{
var generator = new ProxyGenerator();
return generator.CreateClassProxyWithTarget(type, Activator.CreateInstance(type), new DataInterceptor(data));
}