代理类分为普通代理和泛型代理
This commit is contained in:
		
							parent
							
								
									59cf1928f8
								
							
						
					
					
						commit
						d32e95cdff
					
				@ -6,30 +6,25 @@ namespace Falcon.SugarApi.Proxy
 | 
			
		||||
    /// 对象代理接口
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <typeparam name="T">代理的对象类型</typeparam>
 | 
			
		||||
    public abstract class Proxy<T>
 | 
			
		||||
    public abstract class Proxy<T>:ProxyBase where T:class
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 通过传入代理对象生成代理
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="target">要代理的对象</param>
 | 
			
		||||
        /// <exception cref="ArgumentNullException">代理对象为空</exception>
 | 
			
		||||
        public Proxy(T target) {
 | 
			
		||||
            if(target == null) {
 | 
			
		||||
                throw new ArgumentNullException(nameof(target));
 | 
			
		||||
            }
 | 
			
		||||
            Target = target;
 | 
			
		||||
        }
 | 
			
		||||
        public Proxy(T target):base(target) {}
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 代理对象
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public T Target { get; }
 | 
			
		||||
        public new T? Target => base.Target as T;
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 获取对象属性的代理
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="prop">属性名称</param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        /// <exception cref="PropNotFoundException">没有找到属性</exception>
 | 
			
		||||
        public virtual object? GetFunc(string prop) {
 | 
			
		||||
        public override object? GetFunc(string prop) {
 | 
			
		||||
            var p = typeof(T).GetProperty(prop) ?? throw new PropNotFoundException(prop);
 | 
			
		||||
            return p.GetValue(this.Target);
 | 
			
		||||
        }
 | 
			
		||||
@ -39,7 +34,7 @@ namespace Falcon.SugarApi.Proxy
 | 
			
		||||
        /// <param name="prop">属性名称</param>
 | 
			
		||||
        /// <param name="value">属性值</param>
 | 
			
		||||
        /// <exception cref="PropNotFoundException">没有找到属性</exception>
 | 
			
		||||
        public virtual void SetAction(string prop,object? value) {
 | 
			
		||||
        public override void SetAction(string prop,object? value) {
 | 
			
		||||
            var p = typeof(T).GetProperty(prop) ?? throw new PropNotFoundException(prop);
 | 
			
		||||
            p.SetValue(this.Target,value);
 | 
			
		||||
        }
 | 
			
		||||
@ -50,7 +45,7 @@ namespace Falcon.SugarApi.Proxy
 | 
			
		||||
        /// <param name="args">传入的参数</param>
 | 
			
		||||
        /// <returns>方法返回值</returns>
 | 
			
		||||
        /// <exception cref="MethodNotFoundException">没有找到方法</exception>
 | 
			
		||||
        public virtual object? Invoke(string name,params object[] args) {
 | 
			
		||||
        public override object? Invoke(string name,params object[] args) {
 | 
			
		||||
            var m = typeof(T).GetMethod(name) ?? throw new MethodNotFoundException(name);
 | 
			
		||||
            return m.Invoke(this.Target,args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										53
									
								
								Falcon.SugarApi/Proxy/ProxyBase.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								Falcon.SugarApi/Proxy/ProxyBase.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,53 @@
 | 
			
		||||
namespace Falcon.SugarApi.Proxy
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 对象代理接口
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public abstract class ProxyBase
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 对象代理接口
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="target">代理对象</param>
 | 
			
		||||
        public ProxyBase(object target) {
 | 
			
		||||
            Target = target;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 代理对象
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public virtual object Target { get; }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 获取对象属性的代理
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="prop">属性名称</param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        /// <exception cref="PropNotFoundException">没有找到属性</exception>
 | 
			
		||||
        public virtual object? GetFunc(string prop) {
 | 
			
		||||
            var p = this.Target.GetType().GetProperty(prop) ?? throw new PropNotFoundException(prop);
 | 
			
		||||
            return p.GetValue(this.Target);
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 设置对象属性的代理
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="prop">属性名称</param>
 | 
			
		||||
        /// <param name="value">属性值</param>
 | 
			
		||||
        /// <exception cref="PropNotFoundException">没有找到属性</exception>
 | 
			
		||||
        public virtual void SetAction(string prop,object? value) {
 | 
			
		||||
            var p = this.Target.GetType().GetProperty(prop) ?? throw new PropNotFoundException(prop);
 | 
			
		||||
            p.SetValue(this.Target,value);
 | 
			
		||||
        }
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 执行代理对象方法
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="name">方法名称</param>
 | 
			
		||||
        /// <param name="args">传入的参数</param>
 | 
			
		||||
        /// <returns>方法返回值</returns>
 | 
			
		||||
        /// <exception cref="MethodNotFoundException">没有找到方法</exception>
 | 
			
		||||
        public virtual object? Invoke(string name,params object[] args) {
 | 
			
		||||
            var m = this.Target.GetType().GetMethod(name) ?? throw new MethodNotFoundException(name);
 | 
			
		||||
            return m.Invoke(this.Target,args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user