/// <summary> /// See the <see cref="IProxyGenerator .CreateClassProxy{TClass}(IInterceptor[])"/> documentation. /// </summary> public static TClass CreateClassProxy <TClass>( this IProxyGenerator proxyGenerator, params IAsyncInterceptor[] interceptors) where TClass : class { return(proxyGenerator.CreateClassProxy <TClass>(interceptors.ToInterceptors())); }
/// <summary> /// See the <see cref="IProxyGenerator .CreateClassProxy(Type, IInterceptor[])"/> documentation. /// </summary> public static object CreateClassProxy( this IProxyGenerator proxyGenerator, Type classToProxy, params IAsyncInterceptor[] interceptors) { return(proxyGenerator.CreateClassProxy(classToProxy, interceptors.ToInterceptors())); }
public static object CreateClassProxy(this IProxyGenerator proxyGenerator, Type implementationType, params object[] args) { if (proxyGenerator == null) { throw new ArgumentNullException(nameof(proxyGenerator)); } return(proxyGenerator.CreateClassProxy(implementationType, implementationType, args ?? ArrayUtils.Empty <object>())); }
public static object CreateClassProxy(this IProxyGenerator proxyGenerator, Type serviceType, Type implementationType) { if (proxyGenerator == null) { throw new ArgumentNullException(nameof(proxyGenerator)); } return(proxyGenerator.CreateClassProxy(serviceType, implementationType, ArrayUtils.Empty <object>())); }
public ActionResult <IEnumerable <string> > Get() { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build(); PersonService person = proxyGenerator.CreateClassProxy <PersonService>(); return(new string[] { "value1", "value2", person.SayHello() }); }
public T Create <T>(Type t = null) { var type = t ?? typeof(T); var applicableInterceptors = GetInterceptors(type); var proxy = _generator.CreateClassProxy(type, applicableInterceptors.ToArray()); return((T)proxy); }
public static TImplementation CreateClassProxy <TImplementation>(this IProxyGenerator proxyGenerator, params object[] args) where TImplementation : class { if (proxyGenerator == null) { throw new ArgumentNullException(nameof(proxyGenerator)); } return((TImplementation)proxyGenerator.CreateClassProxy(typeof(TImplementation), typeof(TImplementation), args ?? ArrayUtils.Empty <object>())); }
public TProxy Create <TProxy>() where TProxy : class { var dynamicProxyValueInterceptor = new DynamicProxyPropertyValueInterceptor(_proxyPropertyValueResolver); var dynamicProxy = !typeof(TProxy).IsInterface ? _proxyGenerator.CreateClassProxy <TProxy>(dynamicProxyValueInterceptor) : _proxyGenerator.CreateInterfaceProxyWithoutTarget <TProxy>(dynamicProxyValueInterceptor); return(dynamicProxy); }
static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person person = proxyGenerator.CreateClassProxy <Person>(); person.Say("say!"); } }
public static T CreateServiceProxy <T>(RpcActor localActor, string serviceActorType) { var proxy = _proxyGenerator.CreateClassProxy( typeof(RpcService), new Type[] { typeof(T) }, new ProxyGenerationOptions(), new object[] { localActor }, new IInterceptor[] { new RpcServiceProxyInterceptor(typeof(T), serviceActorType) }); return((T)proxy); }
public void HystrixCommandTest() { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { TestObject p = proxyGenerator.CreateClassProxy <TestObject>(); string text = p.Test1("yepeng"); Assert.AreEqual(text, "降级的方法yepeng"); } }
static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); p.Say("denniswu.cnblogs.com"); } Console.ReadKey(); }
static void Main(string[] args) { ProxyGeneratorBuilder builder = new ProxyGeneratorBuilder(); using (IProxyGenerator generator = builder.Build()) { Person p = generator.CreateClassProxy <Person>(); p.Say("rock"); } Console.ReadKey(); }
static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); string r = p.HelloAsync("rupeng").Result; Console.WriteLine("返回值" + r); } Console.ReadKey(); }
static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); Console.WriteLine(p.HelloAsync("Hello World").Result); Console.WriteLine(p.Add(1, 2)); } Console.ReadKey(); }
/// <summary> /// See the <see cref="IProxyGenerator .CreateClassProxy(Type, ProxyGenerationOptions, object[], IInterceptor[])"/> documentation. /// </summary> public static object CreateClassProxy( this IProxyGenerator proxyGenerator, Type classToProxy, ProxyGenerationOptions options, object[] constructorArguments, params IAsyncInterceptor[] interceptors) { return(proxyGenerator.CreateClassProxy( classToProxy, options, constructorArguments, interceptors.ToInterceptors())); }
public async Task <IEnumerable <string> > Get() { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); Console.WriteLine(await p.HelloAsync("yzk")); //Console.WriteLine(p.Add(1, 2)); } return(new string[] { "value1", "value2" }); }
/// <summary> /// See the <see cref="IProxyGenerator .CreateClassProxy(Type, Type[], ProxyGenerationOptions, IInterceptor[])"/> documentation. /// </summary> public static object CreateClassProxy( this IProxyGenerator proxyGenerator, Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, params IAsyncInterceptor[] interceptors) { return(proxyGenerator.CreateClassProxy( classToProxy, additionalInterfacesToProxy, options, interceptors.ToInterceptors())); }
static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person person = proxyGenerator.CreateClassProxy <Person>(); person.Say("davy yang"); Console.WriteLine(person.GetType()); Console.WriteLine(person.GetType().BaseType); } Console.WriteLine("Hello World!"); }
private static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); //注入 p.Say("rupeng.com"); Console.WriteLine(p.GetType()); Console.WriteLine(p.GetType().BaseType); } Console.ReadKey(); }
private static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { //注意p指向的对象是AspectCore生成的Person的动态子类的对象,直接new Person是无法被拦截的。 Person p = proxyGenerator.CreateClassProxy <Person>(); p.Say("Hello World"); Console.WriteLine(p.GetType()); Console.WriteLine(p.GetType().BaseType); } Console.ReadKey(); }
private static object Create(Type targetType, RemoteWorker worker) { try { return(proxyGenerator.CreateInterfaceProxyWithoutTarget( targetType, worker)); } catch { return(proxyGenerator.CreateClassProxy( targetType, worker)); } }
static void Main(string[] args) { ProxyGeneratorBuilder builder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxygenerator = builder.Build()) { //不是person类对象 而是person类子类的对象 Person p = new Person()是不进行拦截的 大概就是 person子类 override父类的方法 在override的方法中调用父类的方法 base.Say() Person p = proxygenerator.CreateClassProxy <Person>(); Console.WriteLine(p.GetType()); Console.WriteLine(p.GetType().BaseType); p.Say("rupeng.com"); } Console.WriteLine("程序ok"); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("开始程序"); //User user = new User(); //user.AddAsync(); ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { User p = proxyGenerator.CreateClassProxy <User>(); p.AddAsync(); //p.Error1(); } Console.ReadLine(); }
static void Main(string[] args) { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); Console.WriteLine(p.HelloAsync("yzk").Result); Console.WriteLine(p.Add(1, 2)); while (true) { Console.WriteLine(p.HelloAsync("yzk").Result); Thread.Sleep(100); } } }
static void Main(string[] args) { //为了使用熔断降级 ,代码不至于与业务代码在一起 ,使用aop //国产Aop Install-Package AspectCore.Core -Version 0.5.0 //版本更新比较快,注意使用 //使用Aop封装降级 见项目 MyHystrix //方法调用 ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); p.Say("大海"); } Console.ReadKey(); }
private static void Main(string[] args) { try { ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()) { Person p = proxyGenerator.CreateClassProxy <Person>(); Console.WriteLine(p.HelloAsync("yzk").Result); //Console.WriteLine(p.Add(1, 2)); } } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadKey(); }
public object CreateClassProxy(Type serviceType, Type implementationType, object[] args) { return(_proxyGenerator.CreateClassProxy(serviceType, implementationType, args)); }
private object?ProxyValue(Type returnType, object value, PropertyInfo?property) { try { Current = this; // Interface if (returnType.IsInterface && (returnType.GetInterface("IEnumerable") != null || returnType.GetInterface("IList") != null)) { dynamic result = typeof(List <>).MakeGenericType(returnType.GenericTypeArguments) .GetConstructor(Array.Empty <Type>())?.Invoke(Array.Empty <object?>()) ?? throw new ApplicationException(); if (value is not IList <object> list) { if (property != null && property.GetCustomAttribute <AsSingularAttribute>() != null) { result.Add(ProxyValue(returnType.GenericTypeArguments[0], value, property)); } else { throw new ApplicationException("Array expected!"); } } else { foreach (var item in list) { var val = ProxyValue(returnType.GenericTypeArguments[0], item); result.Add((dynamic?)val); } } return(result); } if (value is string s) { var repositories = returnType.GetCustomAttributes <RepositoryAttribute>().ToArray(); if (repositories != null && repositories.Any()) { foreach (var repository in repositories) { var obj = TryGetFromRepository(repository.RepositoryPath, s); if (obj != null) { return(obj); } } throw new ApplicationException($"Object with key '{s}' not found in repository!"); } } var builderAttr = returnType.GetCustomAttribute <BuilderAttribute>(false); if (builderAttr != null) { var builder = builderAttr.BuilderType == null?returnType.GetMethod("Build") : builderAttr.BuilderType.GetMethod("Build"); if (builder != null) { return(builder.Invoke(null, new[] { value }) ?? throw new NullReferenceException("Build() should never return null values!")); } else { throw new ApplicationException("Builder's Build() method not found!"); } } if (returnType.IsInterface && !(value is string)) { return(_generator.CreateInterfaceProxyWithoutTarget(returnType, new Interceptor(this, value))); } if (returnType.IsAbstract) { return(_generator.CreateClassProxy(returnType, new Interceptor(this, value))); } var ctor = returnType.GetConstructor(new[] { typeof(DynamicMetaObject) }); if (ctor != null) { return(ctor.Invoke(new[] { value })); } // presumably some system type var underlyingType = Nullable.GetUnderlyingType(returnType); if (underlyingType != null) { if (value == null) { return(null); } else { returnType = underlyingType; } } if (returnType.GetInterface("IConvertible") != null && value is IConvertible convertible) { return(convertible.ToType(returnType, CultureInfo.InvariantCulture)); } if (value is ExpandoObject) { throw new ApplicationException("Can't proxy value!"); } if (!returnType.IsInstanceOfType(value)) { throw new ApplicationException("Invalid value type!"); } return(value); }