我有一个类,我想添加相当于C函数指针。 [英] I have a class that I would like to add the equivalent of a C function pointer.
问题描述
我希望能够将C#等效的C函数指针添加到下面的Class'clientInfo'中。这将使我能够在处理传入数据时取消switch语句。 'clientInfo'数据被初始化并放置在'clientInfo'列表中,从中可以处理传入数据。
公共类dataFormat
{
public string channelName;
public byte dataPosition;
public byte lengthbytes;
public FormatType格式;
//我想在这里添加等效的函数指针
}
公共类clientInfo
{
公共字符串描述;
公共UInt16地址;
公共字节pCode;
public List< dataformat>数据;
}
列表< clientinfo> clientList = new List< clientinfo>();
//用于测试的虚拟数据
byte [] tooldesc = new byte [] { 0x5A,0x55,0x55,TOOL_INFO_READ,0x10,0x00,0x30,0x31,0x40,0x01,0x02,0x03,0x54,0x65,0x73,0x74,0x20,0x54,0x6F,0x6F,0x6C,0xFF,0xFF,0xA5}; < br $>
public MainForm()
{
InitializeComponent();
InitialiseTools(); //设置工具数据
//现在只是我使用tooldesc定义处理传入数据的示例
//以上。
List< byte []> bufferr = new List< byte []>();
bufferr.Add(tooldesc);
int n = 0;
for (int clientIdx = 0; clientIdx< clientList.Count; clientIdx ++)
{
if(clientList [clientIdx] .address == 0x5555)
{
//而不是下面的开关语句,函数
//指针将能够调用指定的函数
//例如ProcessGammaCCL()等
int type =(int)(clientList [clientIdx] .pCode);
switch(type )
{
case 0xB8:
ProcessGammaCCl(clientIdx,bufferr [n]);
break;
案例0x89:
Up dateToolTable(clientIdx,bufferr [n]);
break;
默认值:
break;
} < br $>
}
}
}
private void InitialiseTools()
{
//理想情况下将数据从ini文件读入工具数据并添加到客户端
// list
//可以为相关的函数调用添加函数ptr
clientInfo工具=新的clientInfo();
tool.description =工具描述;
tool.address = 0x5555;
tool.pCode = 0x89;
var channelInfo = new List< dataformat>
{
new dataFormat
{
channelName =地址,
dataPosition = 1,
lengthbytes = 2,
format = FormatType .s16lsb
},
new dataFormat
{
channelName =Description,
dataPosition = 12,
lengthbytes = 4,
format = FormatType.str
},
};
tool.data = channelInfo;
clientList.Add(tool);
// Gamma CCL工具
clientInfo tool1 = new clientInfo();
tool1.description =Gamma GCL ;
tool1.address = 0x5555;
tool1.pCode = 0xB8;
channelInfo = new List< dataformat>
{
new dataFormat
{
channelName =时间戳,
dataPosition = 6,
lengthbytes = 4,
format = FormatType.u32lsb
},
新dataFormat
{
channelName =CCL,
dataPosition = 10,
lengthbytes = 2,
format = FormatType.s12bitlsb
},
new dataFormat
{
channelName =HV OK Status,
dataPosition = 11,
lengthbytes = 1,
format = FormatType.bitMask0x10
},
new dataFormat
{
channelName =Gamma Count,
dataPosition = 12,
lengthbytes = 2,
format = FormatType.u16lsb
}
};
tool1.data = channelInfo;
clientList.Add(tool1);
} $ / $
private void ProcessGammaCCl(int i,byte [] buffer)
{
.... ...
int bufferPos = clientList [i] .data [0] .dataPosition;
.......
}
我尝试了什么:
我用google搜索这个toopic和找到了使用委托和Func<>的参考资料,但没有发现任何我能理解和理解的内容ent!
I would like to be able to add the c# equivalent of a C function pointer to the Class 'clientInfo' below. This will enable me to do away with the switch statement when processing incoming data. The 'clientInfo' data is initialised and placed in a 'clientInfo' List from which the incoming data can be processed.
public class dataFormat
{
public string channelName;
public byte dataPosition;
public byte lengthbytes;
public FormatType format;
// I want to add an equivalent of a function pointer here
}
public class clientInfo
{
public string description;
public UInt16 address;
public byte pCode;
public List<dataformat> data;
}
List<clientinfo> clientList = new List<clientinfo>();
// dummy data for testing
byte[] tooldesc = new byte[] {0x5A, 0x55, 0x55, TOOL_INFO_READ, 0x10, 0x00, 0x30, 0x31, 0x40, 0x01, 0x02, 0x03, 0x54, 0x65, 0x73, 0x74, 0x20, 0x54, 0x6F, 0x6F, 0x6C, 0xFF, 0xFF, 0xA5};
public MainForm()
{
InitializeComponent();
InitialiseTools(); // Set up the tool data
// Now just an example of how I am processing the incoming data
// using tooldesc defined above.
List<byte[]> bufferr = new List<byte[]>();
bufferr.Add(tooldesc);
int n = 0;
for (int clientIdx = 0; clientIdx < clientList.Count; clientIdx++)
{
if (clientList[clientIdx].address == 0x5555)
{
// Instead of having the switch statement below, the function
// pointer will be able to call the assigned function
// eg ProcessGammaCCL() etc.
int type = (int)(clientList[clientIdx].pCode);
switch (type)
{
case 0xB8:
ProcessGammaCCl(clientIdx, bufferr[n]);
break;
case 0x89:
UpdateToolTable(clientIdx, bufferr[n]);
break;
default:
break;
}
}
}
}
private void InitialiseTools()
{
// Ideally read data from an ini file into tool data and add to client
// list
// Could add function ptr for the relevant function call
clientInfo tool = new clientInfo();
tool.description = "Tool Description";
tool.address = 0x5555;
tool.pCode = 0x89;
var channelInfo = new List<dataformat>
{
new dataFormat
{
channelName = "Address",
dataPosition = 1,
lengthbytes = 2,
format = FormatType.s16lsb
},
new dataFormat
{
channelName = "Description",
dataPosition = 12,
lengthbytes = 4,
format = FormatType.str
},
};
tool.data = channelInfo;
clientList.Add(tool);
// Gamma CCL Tool
clientInfo tool1 = new clientInfo();
tool1.description = "Gamma GCL";
tool1.address = 0x5555;
tool1.pCode = 0xB8;
channelInfo = new List<dataformat>
{
new dataFormat
{
channelName = "Time Stamp",
dataPosition = 6,
lengthbytes = 4,
format = FormatType.u32lsb
},
new dataFormat
{
channelName = "CCL",
dataPosition = 10,
lengthbytes = 2,
format = FormatType.s12bitlsb
},
new dataFormat
{
channelName = "HV OK Status",
dataPosition = 11,
lengthbytes = 1,
format = FormatType.bitMask0x10
},
new dataFormat
{
channelName = "Gamma Count",
dataPosition = 12,
lengthbytes = 2,
format = FormatType.u16lsb
}
};
tool1.data = channelInfo;
clientList.Add(tool1);
}
private void ProcessGammaCCl(int i, byte[] buffer)
{
.......
int bufferPos = clientList[i].data[0].dataPosition;
.......
}
What I have tried:
I have googled this toopic and found references to using delegates and Func<>, but found nothing that I can understand and imlement!
推荐答案
我演示了如何使用 Action< T>
取消这个 [ ^ ]博文。如果您下载附加的代码示例,您应该能够跟踪它的工作原理。
I demonstrated how to use Action<T>
to do away with switch statements in this[^] blog post. If you download the attached code sample, you should be able to trace through how it works.
代理是有效的函数指针,一旦您了解它们,它们就非常简单。 br />
从头开始:代表(C#编程指南)| Microsoft Docs [ ^ ]
然后转到代表集合: c# - 如何在列表中存储代理 - 堆栈溢出 [ ^ ]并考虑使用Dictionary而不是您的开关。
Delegates are effectively function pointers, and they are pretty simpel once you get your head around them.
Start at the beginning: Delegates (C# Programming Guide) | Microsoft Docs[^]
Then move on to a collection of delegates: c# - How to store delegates in a List - Stack Overflow[^] and consider a Dictionary instead of your switch.
这篇关于我有一个类,我想添加相当于C函数指针。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!