代理类分为普通代理和泛型代理

This commit is contained in:
Falcon 2024-10-24 11:24:38 +08:00
parent 59cf1928f8
commit d32e95cdff
2 changed files with 59 additions and 11 deletions

View File

@ -6,30 +6,25 @@ namespace Falcon.SugarApi.Proxy
/// 对象代理接口 /// 对象代理接口
/// </summary> /// </summary>
/// <typeparam name="T">代理的对象类型</typeparam> /// <typeparam name="T">代理的对象类型</typeparam>
public abstract class Proxy<T> public abstract class Proxy<T>:ProxyBase where T:class
{ {
/// <summary> /// <summary>
/// 通过传入代理对象生成代理 /// 通过传入代理对象生成代理
/// </summary> /// </summary>
/// <param name="target">要代理的对象</param> /// <param name="target">要代理的对象</param>
/// <exception cref="ArgumentNullException">代理对象为空</exception> /// <exception cref="ArgumentNullException">代理对象为空</exception>
public Proxy(T target) { public Proxy(T target):base(target) {}
if(target == null) {
throw new ArgumentNullException(nameof(target));
}
Target = target;
}
/// <summary> /// <summary>
/// 代理对象 /// 代理对象
/// </summary> /// </summary>
public T Target { get; } public new T? Target => base.Target as T;
/// <summary> /// <summary>
/// 获取对象属性的代理 /// 获取对象属性的代理
/// </summary> /// </summary>
/// <param name="prop">属性名称</param> /// <param name="prop">属性名称</param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="PropNotFoundException">没有找到属性</exception> /// <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); var p = typeof(T).GetProperty(prop) ?? throw new PropNotFoundException(prop);
return p.GetValue(this.Target); return p.GetValue(this.Target);
} }
@ -39,7 +34,7 @@ namespace Falcon.SugarApi.Proxy
/// <param name="prop">属性名称</param> /// <param name="prop">属性名称</param>
/// <param name="value">属性值</param> /// <param name="value">属性值</param>
/// <exception cref="PropNotFoundException">没有找到属性</exception> /// <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); var p = typeof(T).GetProperty(prop) ?? throw new PropNotFoundException(prop);
p.SetValue(this.Target,value); p.SetValue(this.Target,value);
} }
@ -50,7 +45,7 @@ namespace Falcon.SugarApi.Proxy
/// <param name="args">传入的参数</param> /// <param name="args">传入的参数</param>
/// <returns>方法返回值</returns> /// <returns>方法返回值</returns>
/// <exception cref="MethodNotFoundException">没有找到方法</exception> /// <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); var m = typeof(T).GetMethod(name) ?? throw new MethodNotFoundException(name);
return m.Invoke(this.Target,args); return m.Invoke(this.Target,args);
} }

View 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);
}
}
}