C#插件化架构(Plugin Architecture)或 可插拔架构,根据产品类型编码的不同自动路由到目标函数,而无需为每个产品都编码相应的代码!!

using HttpServer;

using MySql.Data.MySqlClient.Memcached;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using NPOI.SS.Formula.Functions;

using NPOI.SS.UserModel;

using Org.BouncyCastle.Bcpg;

using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Data.Entity.Core.Metadata.Edm;

using System.Diagnostics;

using System.Diagnostics.Contracts;

using System.IO;

using System.Linq;

using System.Net;

using System.Net.Sockets;

using System.Reflection;

using System.Runtime.InteropServices;

using System.Threading;

using System.Web.Http.Results;

using System.Web.Http.Tracing;

//using MySql.Data.MySqlClient.Memcached;

using static ai_light_server.adminQueryServices;

namespace ai_light_server

{

public partial class Work

{

public static string get_a_config_value_in_Codes_Dirs(string prodtype, string config_name)

{

// 获取当前程序的程序集

Assembly assembly = Assembly.GetExecutingAssembly();

Type[] types = assembly.GetTypes();

string ret_str = "";

foreach (Type type in types)

{

// 获取所有静态字段

FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

foreach (FieldInfo field in fields)

{

if (field.IsStatic) // 确保是静态字段

{

if (field.Name == "my_prodtype")

{

if (((int)field.GetValue(null)).ToString() == prodtype)

{

foreach (FieldInfo field002 in fields)

{

if (field002.Name == "class_is_a_Config")

{

if (((int)field002.GetValue(null)) == 1)

{

foreach (FieldInfo field003 in fields)

{

if (field003.Name == config_name)

{

return field003.GetValue(null).ToString();

}

}

break;

}

}

}

break;

}

}

}

}

}

return "";

}

public static void test1()

{

if (get_a_config_value_in_Codes_Dirs("33333".ToString(), "class_is_a_Config") == "1")

{

call_a_function_in_Codes_Dirs("33333".ToString(), "jiaoshi", new object[] { "111", new ctrl_works_item() { fail_msg="123456"} });

}

}

public static object call_a_function_in_Codes_Dirs(string prodtype, string func_name,params object[] parms_in)

{

// 获取当前程序的程序集

Assembly assembly = Assembly.GetExecutingAssembly();

Type[] types = assembly.GetTypes();

string ret_str = "";

foreach (Type type in types)

{

// 获取所有静态字段

FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

foreach (FieldInfo field in fields)

{

if (field.IsStatic) // 确保是静态字段

{

if (field.Name == "my_prodtype")

{

if (((int)field.GetValue(null)).ToString() == prodtype)

{

MethodInfo[] methods1 = type.GetMethods();

foreach (MethodInfo method1 in methods1)

{

if(method1.Name== func_name)

{

if (method1.IsStatic)

{

return method1.Invoke(null,parms_in);

}

else

{

object instance1 = Activator.CreateInstance(type,null);

return method1.Invoke(instance1, parms_in);

}

}

}

}

}

}

}

}

return null;

}

static void get_all_class_names()

{

// 获取当前程序的程序集

Assembly assembly = Assembly.GetExecutingAssembly();

Type[] types = assembly.GetTypes();

string classnames = "========================================================================\r\n";

foreach (Type type in types)

{

// classnames += (type.FullName + "\r\n");

// 获取所有静态字段

FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

foreach (FieldInfo field in fields)

{

if (field.IsStatic) // 确保是静态字段

{

if(field.Name== "my_prodtype")

{

classnames += (type.FullName + "\r\n");

if ((int)field.GetValue(null) == 33333)

{

classnames += ("----------------------------------\r\n");

classnames += ( "my_prodtype==33333\r\n");

classnames += ("----------------------------------\r\n");

}

}

}

}

}

tools1.fvdou_append_error_to_log_txtH("classnames", classnames);

}

}

}

==========================================================

using HttpServer;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using NPOI;

using NPOI.HSSF.Model;

using NPOI.HSSF.UserModel;

using NPOI.HSSF.UserModel;

using NPOI.HSSF.Util;

using NPOI.POIFS.Crypt.Dsig;

using NPOI.SS.Formula.Functions;

using NPOI.SS.UserModel;

using NPOI.XSSF.UserModel;

using NPOI.XWPF.UserModel;

using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Diagnostics;

using System.IO;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Web;

using System.Text;

using System.Threading;

using System.Web.UI.WebControls;

using static ai_light_server.adminQueryServices;

namespace ai_light_server

{

public class lib_1_33333

{

public static int my_prodtype = 33333;

public static string jiaoshi(string netgate_id, ctrl_works_item cur_task, byte mod_addr) //返回1=ok,返回0=超时,返回-1=网关不在线

{

return Work.jiaoshi_comm1(netgate_id, cur_task, mod_addr);

}

public static string ctrl_mod(string Name1, int switch_ctrl_enable,int tiaoguang_ctrl_enable,string netgate_id, ctrl_works_item cur_task, byte mod_addr,int wait_time_ms_1,int is_changjing_cmd_优先于面板主动发包) //返回1=ok,返回0=超时,返回-1=网关不在线

{

return Work.ctrl_mod_comm(Name1, switch_ctrl_enable, tiaoguang_ctrl_enable, netgate_id, cur_task, mod_addr, wait_time_ms_1, is_changjing_cmd_优先于面板主动发包);

}

}

}

======================================================

using HttpServer;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using NPOI;

using NPOI.HSSF.Model;

using NPOI.HSSF.UserModel;

using NPOI.HSSF.UserModel;

using NPOI.HSSF.Util;

using NPOI.POIFS.Crypt.Dsig;

using NPOI.SS.Formula.Functions;

using NPOI.SS.UserModel;

using NPOI.XSSF.UserModel;

using NPOI.XWPF.UserModel;

using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Diagnostics;

using System.IO;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Web;

using System.Text;

using System.Threading;

using System.Web.UI.WebControls;

using static ai_light_server.adminQueryServices;

namespace ai_light_server

{

public class lib_1_config_33333

{

public static int my_prodtype = 33333 ;

public static int class_is_a_Config = 1;

public static string is_can_Mod_Jiaoshi = "1";

public static string is_a_light_ctrl_Mod = "1";

}

}

==============================================

int prodtype = int.Parse(rttb1_1_tiaoguangs.Rows[y]["prodtype"].ToString());

int huilu_num = int.Parse(rttb1_1_tiaoguangs.Rows[y]["huilu_num"].ToString());

byte mod_addr = byte.Parse(rttb1_1_tiaoguangs.Rows[y]["modbus_addr"].ToString());

string Name1 = rttb1_1_tiaoguangs.Rows[y]["Name"].ToString();

online_state_info drv1 = adminQueryServices.get_drv(rttb1_1_tiaoguangs.Rows[y]["id_str"].ToString());

int wait_time_ms_1 = 6000;

if (drv1 != null)

{

if (drv1.is_online == -1 || drv1.is_online == 1)

{

wait_time_ms_1 = 8000;

}

else

{

wait_time_ms_1 = 3000;

}

}

Ext_prodtype_list_info extprodtype1 = Work.ref_this.Get_prod_type_info_by_prodtype1(prodtype.ToString());

if (extprodtype1 != null && extprodtype1.V2_JiaGou_Ver == 2)

{

if (get_a_config_value_in_Codes_Dirs(prodtype.ToString(), "is_a_light_ctrl_Mod") != "1")

{

if (Work.G_use_chs_0_or_english_1 == 0)

cur_task.works_msg += "[" + Name1 + "]mod无法控制...\n<br>";

if (Work.G_use_chs_0_or_english_1 == 1)

cur_task.works_msg += "[" + Name1 + "]mod is not a ctrl_able mod ...\n<br>";

}

else

{

string is_ok = call_a_function_in_Codes_Dirs(prodtype.ToString(), "ctrl_mod", new object[] {Name1, switch_ctrl_enable, tiaoguang_ctrl_enable, item11netgate, cur_task, mod_addr, wait_time_ms_1, is_changjing_cmd_优先于面板主动发包 }).ToString();

/*

if (is_ok == "0" || is_ok == "-1")

{

if (is_ok == "0")

{

if (Work.G_use_chs_0_or_english_1 == 0)

cur_task.works_msg += "校时[" + Name1 + "]mod时无返回数据...\n<br>";

if (Work.G_use_chs_0_or_english_1 == 1)

cur_task.works_msg += "time calibration for [" + Name1 + "]mod, No data return ...\n<br>";

}

else

{

if (Work.G_use_chs_0_or_english_1 == 0)

cur_task.works_msg += "[" + item11netgate + "]网关不在线...\n<br>";

if (Work.G_use_chs_0_or_english_1 == 1)

cur_task.works_msg += "[" + item11netgate + "]gateway not online...\n<br>";

}

}

else

{

if (Work.G_use_chs_0_or_english_1 == 0)

cur_task.works_msg += "控制[" + Name1 + "]mod时ok...\n<br>";

if (Work.G_use_chs_0_or_english_1 == 1)

cur_task.works_msg += "ctrl for [" + Name1 + "]mod ok! ...\n<br>";

}*/

}

}

相关推荐
水深00安东尼3 小时前
C#猜数字小游戏
开发语言·c#
福客AI智能客服3 小时前
电商智能客服机器人:AI客服聊天系统如何重构用户沟通方式
人工智能·重构·机器人
ai产品老杨4 小时前
协议破壁与流媒体重构:基于 GB28181/RTSP 的企业级视频统一接入方案
重构·音视频
无风听海6 小时前
.NET10之C# Extension Members深入分析
大数据·c#·.net·extensionmember
唐青枫6 小时前
C#.NET 分布式事务 深入解析:TCC、Saga、Outbox 与落地取舍
c#·.net
人工智能AI技术6 小时前
ML.NET + 1-bit LLM:在 C# 上位机实现仅 1GB 内存的本地 AI 推理
人工智能·c#
cch89187 小时前
PHP vs C#:语言对比与实战选型
开发语言·c#·php
无风听海7 小时前
NET10之C# Primary Constructor 深度指南
开发语言·c#·.net10
余衫马8 小时前
在 IIS 部署 .NET6 WebApi 应用
运维·c#·iis·.net·发布