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.Rowsy"prodtype".ToString());

int huilu_num = int.Parse(rttb1_1_tiaoguangs.Rowsy"huilu_num".ToString());

byte mod_addr = byte.Parse(rttb1_1_tiaoguangs.Rowsy"modbus_addr".ToString());

string Name1 = rttb1_1_tiaoguangs.Rowsy"Name".ToString();

online_state_info drv1 = adminQueryServices.get_drv(rttb1_1_tiaoguangs.Rowsy"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>";

}*/

}

}

相关推荐
雨落倾城夏未凉4 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫5 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫6 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6256 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902116 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠7 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫9 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech9 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf11 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m62511 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#