本文整理汇总了C#中Castle.DynamicProxy.ProxyGenerationOptions类的典型用法代码示例。如果您正苦于以下问题:C# ProxyGenerationOptions类的具体用法?C# ProxyGenerationOptions怎么用?C# ProxyGenerationOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ProxyGenerationOptions类属于Castle.DynamicProxy命名空间,在下文中一共展示了ProxyGenerationOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Proceed
public override void Proceed()
{
var expectation = stubMessageBus.GetExpectationFor(Request.GetType());
if (expectation == null)
{
var response = (Response)Activator.CreateInstance(Request.GetResponseType());
response.CorrelationGuid = Request.CorrelationGuid;
Response = GetSerializedVersion(response);
}
else
{
Response = GetSerializedVersion(expectation.Execute(Request));
}
if(Request.IsSideEffectFree)
{
try
{
var options = new ProxyGenerationOptions(new NonVirtualCheckProxyGenerationHook());
var proxyGen = new ProxyGenerator();
proxyGen.CreateClassProxy(Request.GetResponseType(), options);
}
catch (Exception ex)
{
throw new ColomboTestSupportException(string.Format("Response {0} cannot be proxied, probably because one or several of its members are not virtual.", Request.GetResponseType()), ex);
}
}
}
示例2: 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));
}
}
示例3: Generate
public object Generate(IProxyBuilder builder, ProxyGenerationOptions options, IInterceptor[] interceptors)
{
var type = GetProxyType(builder);
var instance = GetProxyInstance(type,interceptors);
var method = GetInvokeDelegate(instance);
return method;
}
示例4: GenerateProxy
private static ProxyGeneratorResult GenerateProxy(
Type typeOfProxy,
ProxyGenerationOptions options,
IEnumerable<Type> additionalInterfacesToImplement,
IEnumerable<object> argumentsForConstructor,
IFakeCallProcessorProvider fakeCallProcessorProvider)
{
Guard.AgainstNull(typeOfProxy, nameof(typeOfProxy));
Guard.AgainstNull(additionalInterfacesToImplement, nameof(additionalInterfacesToImplement));
Guard.AgainstNull(fakeCallProcessorProvider, nameof(fakeCallProcessorProvider));
if (typeOfProxy.GetTypeInfo().IsValueType)
{
return GetProxyResultForValueType(typeOfProxy);
}
if (typeOfProxy.GetTypeInfo().IsSealed)
{
return new ProxyGeneratorResult(DynamicProxyResources.ProxyIsSealedTypeMessage.FormatInvariant(typeOfProxy));
}
GuardAgainstConstructorArgumentsForInterfaceType(typeOfProxy, argumentsForConstructor);
return CreateProxyGeneratorResult(typeOfProxy, options, additionalInterfacesToImplement, argumentsForConstructor, fakeCallProcessorProvider);
}
示例5: CanCreateMixinWithClassInterceptors
public void CanCreateMixinWithClassInterceptors()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new Dictionary<int, int>());
var builder = new ContainerBuilder();
builder.RegisterType<C>().EnableClassInterceptors(options);
builder.RegisterType<AddOneInterceptor>();
builder.RegisterType<AddTenInterceptor>();
var container = builder.Build();
var i = 10;
var cpt = container.Resolve<C>(TypedParameter.From(i));
var dict = cpt as IDictionary<int, int>;
Assert.NotNull(dict);
dict.Add(1, 2);
Assert.Equal(2, dict[1]);
dict.Clear();
Assert.Empty(dict);
}
开发者ID:jango2015,项目名称:Autofac.Extras.DynamicProxy,代码行数:25,代码来源:ClassInterceptorsWithOptionsFixture.cs
示例6: TrySetActionInvoker
internal static IController TrySetActionInvoker(this IController iController, IGlimpseLogger logger)
{
var controller = iController as Controller;
if (controller == null)
{
//TODO: Add Logging
return iController;
}
var actionInvoker = controller.ActionInvoker;
if (actionInvoker.CanSupportDynamicProxy(logger))
{
var proxyConfig = new Dictionary<string, IInterceptor>
{
{"GetFilters", new GetFiltersInterceptor()},
{"InvokeActionResult", new InvokeActionResultInterceptor()},
{"InvokeActionMethod",new InvokeActionMethodInterceptor()}
};
var proxyGenerator = new ProxyGenerator();
var proxyGenOptions = new ProxyGenerationOptions(new SimpleProxyGenerationHook(logger, proxyConfig.Keys.ToArray())) { Selector = new SimpleInterceptorSelector(proxyConfig) };
var newInvoker = (ControllerActionInvoker)proxyGenerator.CreateClassProxy(actionInvoker.GetType(), proxyGenOptions, proxyConfig.Values.ToArray());
controller.ActionInvoker = newInvoker;
}
return controller;
}
示例7: GetFieldInterceptionProxy
public override object GetFieldInterceptionProxy()
{
var proxyGenerationOptions = new ProxyGenerationOptions();
var interceptor = new LazyFieldInterceptor();
proxyGenerationOptions.AddMixinInstance(interceptor);
return ProxyGenerator.CreateClassProxy(PersistentClass, proxyGenerationOptions, interceptor);
}
示例8: Register
public void Register(ContainerBuilder builder)
{
Assembly assembly = Assembly.GetExecutingAssembly();
ProxyGenerationOptions ProxyOption = new ProxyGenerationOptions()
{
Selector = new InterceptorSelector()
};
//注册所有服务
builder.RegisterAssemblyTypes(assembly)
.Where(type => type.FullName.EndsWith("ApplicationService") && type.IsClass)
.AsImplementedInterfaces()
.EnableInterfaceInterceptors(ProxyOption)
;//.InterceptedBy(typeof(MethodInvacationValidateInterceptor), typeof(UnitOfWorkInterceptor));
//注册DbContext
builder.RegisterType<SampleEntities>().As<DbContext>();
//注册所有泛型仓储
var finder = new WebAppTypeFinder();
var allEntityTypes = finder.FindClassOfType(typeof(IEntity));
foreach (var entityType in allEntityTypes)
{
var genericRepositoryType = typeof(IRepository<>).MakeGenericType(entityType);
var implType = typeof(EfRepository<,>).MakeGenericType(typeof(DbContext), entityType);
builder.RegisterType(implType).As(genericRepositoryType);
}
builder.RegisterType<CallCurrentUnitOfWorkProvider>().As<ICurrentUnitOfWorkProvider>();
builder.RegisterType<UnitOfWorkDbContextProvider<DbContext>>().As<IDbContextProvider<DbContext>>();
builder.RegisterType<EfDbContextProvider<DbContext>>().Keyed<IDbContextProvider<DbContext>>("Default");
builder.RegisterType<UnitOfWorkManager>().As<IUnitOfWorkManager>();
builder.RegisterType<EfUnitOfWork>().As<IUnitOfWork>();
}
示例9: Create
/// <inheritdoc />
public object Create(object target, Type typeOfProxy,
Type[] additionalInterfacesOfProxy)
{
Check.MustNotNull("target", "target");
Check.MustNotNull("typeToProxy", "typeToProxy");
var aspects = _aspectsFinder.FindAspects(target.GetType());
if (!aspects.Any())
{
return target;
}
var options = new ProxyGenerationOptions
{
Selector = new PointcutAspectInterceptorSelector()
};
foreach (var instance in this.GetMixinInstances(target.GetType(), aspects))
{
options.AddMixinInstance(instance);
}
var interceptors = this.GetInterceptors(target.GetType(), aspects);
if (typeOfProxy.IsInterface)
{
return _proxyGenerator.CreateInterfaceProxyWithTarget(typeOfProxy,
additionalInterfacesOfProxy,
target, options, interceptors);
}
else
{
return _proxyGenerator.CreateClassProxyWithTarget(typeOfProxy,
additionalInterfacesOfProxy,
target, options, interceptors);
}
}
示例10: CreateInterfaceProxyTypeWithoutTarget
public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
{
AssertValidType(interfaceToProxy);
AssertValidTypes(additionalInterfacesToProxy);
var generator = new InterfaceProxyWithoutTargetGenerator(scope, interfaceToProxy) { Logger = logger };
return generator.GenerateCode(typeof(object), additionalInterfacesToProxy, options);
}
示例11: GetMethodGenerator
// 重载 InterfaceProxyWithTargetInterfaceContributor 中的方法,以指定使用扩展的 InvocationType 生成方法执行类
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
{
if (!method.Proxyable)
{
return new MinimialisticMethodGenerator(method, overrideMethod);
}
return new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), this.GetInvocationType(method, @class, options), this.getTargetExpression, overrideMethod, null);
}
示例12: ReleaseHook
private void ReleaseHook(ProxyGenerationOptions proxyGenOptions, IKernel kernel)
{
if (proxyGenOptions.Hook == null)
{
return;
}
kernel.ReleaseComponent(proxyGenOptions.Hook);
}
示例13: CreateClassProxy
public virtual Type CreateClassProxy(Type theClass, ProxyGenerationOptions options)
{
AssertValidType(theClass);
ClassProxyGenerator generator = new ClassProxyGenerator(scope, theClass);
return generator.GenerateCode(null, options);
}
示例14: CreateClassProxyType
public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
{
AssertValidType(classToProxy);
AssertValidTypes(additionalInterfacesToProxy);
var generator = new ClassProxyGenerator(scope, classToProxy) { Logger = logger };
return generator.GenerateCode(additionalInterfacesToProxy, options);
}
示例15: CreateInterfaceProxyTypeWithTargetInterface
public Type CreateInterfaceProxyTypeWithTargetInterface(Type theInterface, ProxyGenerationOptions options)
{
AssertValidType(theInterface);
InterfaceProxyWithTargetInterfaceGenerator generator =
new InterfaceProxyWithTargetInterfaceGenerator(scope, theInterface);
return generator.GenerateCode(theInterface, null, options);
}