增加对象代理

This commit is contained in:
Falcon 2024-10-23 17:28:51 +08:00
parent bba31988a4
commit 59cf1928f8
4 changed files with 181 additions and 0 deletions

View File

@ -0,0 +1,22 @@
using System;
namespace Falcon.SugarApi.Proxy
{
/// <summary>
/// 方法没有找到的异常
/// </summary>
public class MethodNotFoundException:Exception
{
/// <summary>
/// 构造属性没有找到的异常
/// </summary>
/// <param name="methodName">属性名称</param>
public MethodNotFoundException(string methodName) {
MethodName = methodName;
}
/// <summary>
/// 属性名称
/// </summary>
public string MethodName { get; }
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace Falcon.SugarApi.Proxy
{
/// <summary>
/// 属性没有找到的异常
/// </summary>
public class PropNotFoundException:Exception
{
/// <summary>
/// 构造属性没有找到的异常
/// </summary>
/// <param name="propName">属性名称</param>
public PropNotFoundException(string propName) {
PropName = propName;
}
/// <summary>
/// 属性名称
/// </summary>
public string PropName { get; }
}
}

View File

@ -0,0 +1,58 @@
using System;
namespace Falcon.SugarApi.Proxy
{
/// <summary>
/// 对象代理接口
/// </summary>
/// <typeparam name="T">代理的对象类型</typeparam>
public abstract class Proxy<T>
{
/// <summary>
/// 通过传入代理对象生成代理
/// </summary>
/// <param name="target">要代理的对象</param>
/// <exception cref="ArgumentNullException">代理对象为空</exception>
public Proxy(T target) {
if(target == null) {
throw new ArgumentNullException(nameof(target));
}
Target = target;
}
/// <summary>
/// 代理对象
/// </summary>
public T Target { get; }
/// <summary>
/// 获取对象属性的代理
/// </summary>
/// <param name="prop">属性名称</param>
/// <returns></returns>
/// <exception cref="PropNotFoundException">没有找到属性</exception>
public virtual object? GetFunc(string prop) {
var p = typeof(T).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 = typeof(T).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 = typeof(T).GetMethod(name) ?? throw new MethodNotFoundException(name);
return m.Invoke(this.Target,args);
}
}
}

View File

@ -0,0 +1,79 @@
## 对象代理
通过代理对象,可以在获取、设置或运行对象方法时执行代理操作。
被代理的类型
~~~
public class TestObj
{
public string Name { get; set; }
public string func(string str) {
return str + " run";
}
public void Action() {
this.Name = "123";
}
}
~~~
要实现代理首先应继承Proxy泛型类泛型类型参数为要代理的对象类型。
~~~c#
public class TestObjProxy:Proxy<TestObj>
{
public TestObjProxy(TestObj target) : base(target) {
}
public override object? GetFunc(string prop) {
if(prop == "Name") {
Console.WriteLine($"GetFunc{this.Target.Name}");
return base.GetFunc(prop);
}
return "propName error";
}
public override void SetAction(string prop,object? value) {
if(prop == "Name") {
Console.WriteLine($"SetAction{this.Target.Name}");
base.SetAction(prop,value);
}
}
public override object? Invoke(string name,params object[] args) {
if(name == "func") {
Console.WriteLine($"run invoke:{name}");
return base.Invoke(name,args);
}
if(name== "Action") {
Console.WriteLine($"run invoke:{name}");
base.Invoke(name,args);
return null;
}
if(name== "ToString") {
Console.WriteLine($"run invoke:{name}");
base.Invoke(name,args);
}
return base.Invoke(name,args);
}
}
~~~
实例化代理类型,同时传入要代理的对象。
~~~c#
var obj = new TestObj();
var proxy = new TestObjProxy(obj);
~~~
然后通过代理获取或设置属性或者运行方法。
~~~c#
proxy.SetAction("Name","Tom");
var r = proxy.GetFunc("Name");
Console.WriteLine(r);
r = proxy.Invoke("func","paraaaaa");
Console.WriteLine(r);
proxy.Invoke("Action");
Console.WriteLine(proxy.GetFunc("Name"));
Console.WriteLine(proxy.Invoke("ToString"));
~~~