概述 由於工作需要,需要通過數據類型和方法名控制方法走向 用到的數據類型有8種(string,Int16,Int32,Int64,Boolean,Byte,Single,Double) 讀取的方法(參數一致,但是數據不同的泛型方法,返回值也是泛型)暫時只有11種,但肯定的是,後續一定會增加 原本計劃 ...
概述
由於工作需要,需要通過數據類型和方法名控制方法走向
用到的數據類型有8種(string,Int16,Int32,Int64,Boolean,Byte,Single,Double)
讀取的方法(參數一致,但是數據不同的泛型方法,返回值也是泛型)暫時只有11種,但肯定的是,後續一定會增加
原本計劃排列組合,寫個88行代碼,但是總覺得重覆代碼過多,且後續維護極其繁瑣
例如:新增一個讀取方法,需要額外新增8行數據類型選擇代碼,繁瑣暫且不說,主要是容易出現紕漏
網路上一翻搜尋,找到了反射,完美解決了我的問題,現在想把這個反射分享給大家:
排列組合
沒錯,我最開始就是排列組合
int datatype = default(int) ; // 決定值類型
int GetType = default(int) ; // 決定通過什麼方法讀取
//類型選擇
switch (datatype)
{
case 2: res[0] += "'" + Convert.ToString(ReadString(dataaddress , datalen , gettype)) + "',"; break;
case 4: res[0] += Convert.ToString(ReadInt(dataaddress , datalen , gettype)) + ","; break;
case 6: res[0] += "'" + Convert.ToString(ReadString(dataaddress , datalen , gettype)) + "',"; break;
case 7: res[0] += Convert.ToString(ReadUshort(dataaddress , datalen , gettype)) + ","; break;
case 8: res[0] += Convert.ToString(ReadUint(dataaddress , datalen , gettype)) + ","; break;
case 9: res[0] += Convert.ToString(ReadULong(dataaddress , datalen , gettype)) + ","; break;
case 10: res[0] += Convert.ToString(ReadInt(dataaddress , datalen , gettype)) + ","; break;
case 11: res[0] += Convert.ToString(ReadShort(dataaddress , datalen , gettype)) + ","; break;
case 12: res[0] += Convert.ToString(ReadByte(dataaddress , datalen , gettype)) + ","; break;
case 13: res[0] += Convert.ToString(ReadFloat(dataaddress , datalen , gettype)) + ","; break;
case 14: res[0] += Convert.ToString(ReadDouble(dataaddress , datalen , gettype)) + ","; break;
case 15: res[0] += Convert.ToString(ReadDouble(dataaddress , datalen , gettype)) + ","; break;
case 20: res[0] += Convert.ToString(ReadBool(dataaddress , datalen , gettype)).ToLower() + ","; break;
case 31: res[0] += Convert.ToString(ReadLong(dataaddress , datalen , gettype)) + ","; break;
default: res[0] += Convert.ToString(ReadUshort(dataaddress , datalen , gettype)) + ","; break;
}
//方法選擇
//string
public string ReadString(string StartAddress , int Length , int GetType)
{
switch (GetType)
{
case 1: return ModbusTcpRead<string>(StartAddress , Length);
case 2: return ModbusRtuRead<string>(StartAddress , Length);
case 3: return ModbusRtuOverTcpRead<string>(StartAddress , Length);
case 5: return InovanceTcpNetRead<string>(StartAddress , Length);
case 6: return KeyenceMcNetRead<string>(StartAddress , Length);
case 7: return MelsecMcNetRead<string>(StartAddress , Length);
case 8: return OmronFinsNetRead<string>(StartAddress , Length);
case 9: return PanasonicMcNetRead<string>(StartAddress , Length);
case 10: return SiemensS7NetRead<string>(StartAddress , Length);
case 11: return MelsecFxSerialOverTcpRead<string>(StartAddress , Length);
case 12: return KeyenceMcAsciiNetRead<string>(StartAddress , Length);
default: return ModbusTcpRead<string>(StartAddress , Length);
}
}
//Bool
public bool ReadBool(string StartAddress , int Length , int GetType)
{
switch (GetType)
{
case 1: return ModbusTcpRead<bool>(StartAddress , Length);
case 2: return ModbusRtuRead<bool>(StartAddress , Length);
case 3: return ModbusRtuOverTcpRead<bool>(StartAddress , Length);
case 5: return InovanceTcpNetRead<bool>(StartAddress , Length);
case 6: return KeyenceMcNetRead<bool>(StartAddress , Length);
case 7: return MelsecMcNetRead<bool>(StartAddress , Length);
case 8: return OmronFinsNetRead<bool>(StartAddress , Length);
case 9: return PanasonicMcNetRead<bool>(StartAddress , Length);
case 10: return SiemensS7NetRead<bool>(StartAddress , Length);
case 11: return MelsecFxSerialOverTcpRead<bool>(StartAddress , Length);
case 12: return KeyenceMcAsciiNetRead<bool>(StartAddress , Length);
default: return ModbusTcpRead<bool>(StartAddress , Length);
}
}
......(就不全部列出來了,排列組合,懂得吧?)
或許你發現了什麼異常,感覺我這麼寫不太對,應該先控制方法,在控制值類型,但,沒區別,還是排列組合,只不過分散了而已
以上代碼也不難看出,重覆代碼非常多,幾乎就是copy一份,然後改一下泛型傳入
所以我在想,能不能吧參數轉換成泛型填入,然後顯然不行,後來找到了反射這個法寶
反射(正片開始)
int datatype = default(int) ; // 決定值類型
int GetType = default(int) ; // 決定通過什麼方法讀取
//方法名
string methodName = "ModbusTcpRead";
switch (gettype)
{
case 1: methodName = "ModbusTcpRead"; break;
case 2: methodName = "ModbusRtuRead"; break;
case 3: methodName = "ModbusRtuOverTcpRead"; break;
case 5: methodName = "InovanceTcpNetRead"; break;
case 6: methodName = "KeyenceMcNetRead"; break;
case 7: methodName = "MelsecMcNetRead"; break;
case 8: methodName = "OmronFinsNetRead"; break;
case 9: methodName = "PanasonicMcNetRead"; break;
case 10: methodName = "SiemensS7NetRead"; break;
case 11: methodName = "MelsecFxSerialOverTcpRead"; break;
case 12: methodName = "KeyenceMcAsciiNetRead"; break;
default: methodName = "ModbusTcpRead"; break;
}
//數據值類型
string type = "System.Int32";
switch (datatype)
{
case 2: type = "System.String"; break;
case 4: type = "System.Int32"; break;
case 6: type = "System.String"; break;
case 7: type = "System.UInt16"; break;
case 8: type = "System.UInt32"; break;
case 9: type = "System.UInt64"; break;
case 10: type = "System.Int32"; break;
case 11: type = "System.Int16"; break;
case 12: type = "System.Byte"; break;
case 13: type = "System.Single"; break;//float
case 14: type = "System.Double"; break;
case 15: type = "System.Double"; break;
case 20: type = "System.Boolean"; break;
case 31: type = "System.Int64"; break;
default: type = "System.UInt16"; break;
}
MethodInfo method = typeof(PLCOper).GetMethod(methodName , BindingFlags.Instance | BindingFlags.Public);
MethodInfo genericMethod = method.MakeGenericMethod(Type.GetType(type , false));
object result = genericMethod.Invoke(this , new object[] { dataaddress , datalen });
//這裡有幾點要提醒
//1、typeof(PLCOper) 括弧里的類名必須是調用方法的父類名
//2、GetMethod 第二個參數,前者用於過濾方法的屬性,比如是靜態還是非靜態,我方法全是非靜態,所以需要BindingFlags.Instance 參數
//3、Invoke 第一個參數,如果調用靜態方法,傳入null就好,實例內的方法,需要傳入this
//4、Invoke 第二個參數,是方法的入參
是不是簡潔非常多?(看起來沒區別或許是因為排列組合我沒全部寫出來?)
以後新增方法名也只需要在這裡改一次就可以了
非常完美!
結束
感謝看到這裡。
本文來自博客園,作者:HookDing,轉載請註明原文鏈接:https://www.cnblogs.com/HookDing/p/18326960