我在2个不同的文件中有2个类。调用基于ID的字典中存储的方法使用c#
类
InvokeMethod
:从Main()
接收字节数组(换算成这里诠释actionID
)和应该调用存储在基于所述actionID
字典中的方法。这个类应该只有通用代码,以便它可以接受任何数量的方法和对象。类
ListOfMethods
:添加接口实现中的所有方法,并返回带有键值对的字典。Main
方法应该只有im.checkID(dataFromString("00 00 00 00"));
我已经做了字节数组转换字符串(即为什么我没有把它放在这里)。
类InvokeMethod
应该调用存储在词典基础上actionID
方法。
的InvokeMethod:
public class InvokeMethod
{
public void checkID(byte[] data)
{
int actionID = BitConverter.ToInt32(data, 0);
Console.WriteLine("action id {0}", actionID);
ListOfMethods GetMethods = new ListOfMethods();
Dictionary<int, dynamic> methodList = GetMethods.ReturnMethods(actionID);
//here it should call the method from dictionary based on actionID.
}
}
ListOfMethods:
public class ListOfMethods
{
Dictionary<int, dynamic> MethodDictionary = new Dictionary<int, dynamic>();
Control ControlImplObj = new ControlImpl();
Type ControlType = typeof(ControlImpl);
public Dictionary<int, dynamic> ReturnMethods(int actionID)
{
var methods = ControlImplObj.GetType().GetMethods();
foreach (var item in methods.Select((value, index) => new { index, value }))
{
if (item.value.DeclaringType == typeof(ControlImpl))
{
MethodDictionary.Add(item.index, item.value);
}
}
return MethodDictionary;
}
}
控制接口
interface Control
{
string SetTime();//0
string Nop();//1
}
控制接口实现
public class ControlImpl : Control
{
public string SetTime()
{
Console.WriteLine("inside SetTime ");
return null;
}
public string Nop()
{
Console.WriteLine("inside Nop ");
return null;
}
}
主要
static void Main(string[] args)
{
InvokeMethod im= new InvokeMethod();
im.checkID(dataFromString("00 00 00 00"));
im.checkID(dataFromString("00 00 00 01"));
Console.ReadLine();
}
随意发布尽可能多的新问题,但您可能希望在问题中包含所有必需的详细信息。将一个_open_问题作为一个论坛主题并不是通常的工作方式。 –
@AdrianoRepetti我根据你以前的答案得到了解决方案。但你的答案似乎已被删除。该线程现在可以关闭。 –