From 59cf1928f822fc0835a37e31252c68b406ba0731 Mon Sep 17 00:00:00 2001
From: Falcon <12919280+falconfly@user.noreply.gitee.com>
Date: Wed, 23 Oct 2024 17:28:51 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E8=B1=A1=E4=BB=A3?=
=?UTF-8?q?=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Proxy/MethodNotFoundException.cs | 22 ++++++
.../Proxy/PropNotFoundException.cs | 22 ++++++
Falcon.SugarApi/Proxy/Proxy.cs | 58 ++++++++++++++
Falcon.SugarApi/Proxy/Readme.md | 79 +++++++++++++++++++
4 files changed, 181 insertions(+)
create mode 100644 Falcon.SugarApi/Proxy/MethodNotFoundException.cs
create mode 100644 Falcon.SugarApi/Proxy/PropNotFoundException.cs
create mode 100644 Falcon.SugarApi/Proxy/Proxy.cs
create mode 100644 Falcon.SugarApi/Proxy/Readme.md
diff --git a/Falcon.SugarApi/Proxy/MethodNotFoundException.cs b/Falcon.SugarApi/Proxy/MethodNotFoundException.cs
new file mode 100644
index 0000000..641f7f2
--- /dev/null
+++ b/Falcon.SugarApi/Proxy/MethodNotFoundException.cs
@@ -0,0 +1,22 @@
+using System;
+
+namespace Falcon.SugarApi.Proxy
+{
+ ///
+ /// 方法没有找到的异常
+ ///
+ public class MethodNotFoundException:Exception
+ {
+ ///
+ /// 构造属性没有找到的异常
+ ///
+ /// 属性名称
+ public MethodNotFoundException(string methodName) {
+ MethodName = methodName;
+ }
+ ///
+ /// 属性名称
+ ///
+ public string MethodName { get; }
+ }
+}
diff --git a/Falcon.SugarApi/Proxy/PropNotFoundException.cs b/Falcon.SugarApi/Proxy/PropNotFoundException.cs
new file mode 100644
index 0000000..d66272d
--- /dev/null
+++ b/Falcon.SugarApi/Proxy/PropNotFoundException.cs
@@ -0,0 +1,22 @@
+using System;
+
+namespace Falcon.SugarApi.Proxy
+{
+ ///
+ /// 属性没有找到的异常
+ ///
+ public class PropNotFoundException:Exception
+ {
+ ///
+ /// 构造属性没有找到的异常
+ ///
+ /// 属性名称
+ public PropNotFoundException(string propName) {
+ PropName = propName;
+ }
+ ///
+ /// 属性名称
+ ///
+ public string PropName { get; }
+ }
+}
diff --git a/Falcon.SugarApi/Proxy/Proxy.cs b/Falcon.SugarApi/Proxy/Proxy.cs
new file mode 100644
index 0000000..bdeb821
--- /dev/null
+++ b/Falcon.SugarApi/Proxy/Proxy.cs
@@ -0,0 +1,58 @@
+using System;
+
+namespace Falcon.SugarApi.Proxy
+{
+ ///
+ /// 对象代理接口
+ ///
+ /// 代理的对象类型
+ public abstract class Proxy
+ {
+ ///
+ /// 通过传入代理对象生成代理
+ ///
+ /// 要代理的对象
+ /// 代理对象为空
+ public Proxy(T target) {
+ if(target == null) {
+ throw new ArgumentNullException(nameof(target));
+ }
+ Target = target;
+ }
+ ///
+ /// 代理对象
+ ///
+ public T Target { get; }
+ ///
+ /// 获取对象属性的代理
+ ///
+ /// 属性名称
+ ///
+ /// 没有找到属性
+ public virtual object? GetFunc(string prop) {
+ var p = typeof(T).GetProperty(prop) ?? throw new PropNotFoundException(prop);
+ return p.GetValue(this.Target);
+ }
+ ///
+ /// 设置对象属性的代理
+ ///
+ /// 属性名称
+ /// 属性值
+ /// 没有找到属性
+ public virtual void SetAction(string prop,object? value) {
+ var p = typeof(T).GetProperty(prop) ?? throw new PropNotFoundException(prop);
+ p.SetValue(this.Target,value);
+ }
+ ///
+ /// 执行代理对象方法
+ ///
+ /// 方法名称
+ /// 传入的参数
+ /// 方法返回值
+ /// 没有找到方法
+ 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);
+ }
+ }
+}
diff --git a/Falcon.SugarApi/Proxy/Readme.md b/Falcon.SugarApi/Proxy/Readme.md
new file mode 100644
index 0000000..76e20ae
--- /dev/null
+++ b/Falcon.SugarApi/Proxy/Readme.md
@@ -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
+{
+ 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"));
+
+~~~
\ No newline at end of file