C# 透過反射(Reflection)呼叫不同名泛型方法

HookDing發表於2024-07-27

概述

由於工作需要,需要透過資料型別和方法名控制方法走向
用到的資料型別有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 第二個引數,是方法的入參

是不是簡潔非常多?(看起來沒區別或許是因為排列組合我沒全部寫出來?)

以後新增方法名也只需要在這裡改一次就可以了

非常完美!

結束

感謝看到這裡。

相關文章