如何建议用户参数的类型 [英] How suggest to user the parameter's type

查看:58
本文介绍了如何建议用户参数的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在c#中编写一个应用程序,它试图在PLC中写入。我有一个接收两个参数的写入方法:

要写入的标记路径和要在标记中写入的值。当我获得与PLC的连接时,我动态地知道每个标签的类型,因此我建议指定标签用户应该传递给方法的值类型。示例1:如果用户调用通过PATH A的方法,我会编译器向用户建议VALUE TYPE INT,因为INT是PLC中标记的类型。示例2:如果用户调用传递PATH B的方法,我会编译器向用户建议VALUE OF TYPE STRINT,因为STRING是PLC中标签的类型

。我能这样做吗?非常感谢

Hi,
i'm writing an application in c#, framework 4.0, that tries to write in a PLC. I have a method of writing that receives two parameters:
the path of tag in which to write and the value to write in the tag. When i get the connection to PLC, I dynamically know the type of every tag, so i would suggest for specified tag the value type that the user should pass to the method. Example 1: if user call the method passing PATH A, i would that compiler suggests to the user the VALUE OF TYPE INT because INT is the type of the tag in PLC. Example 2: if user call the method passing PATH B, i would that compiler suggests to the user the VALUE OF TYPE STRINT because STRING is the type of the tag in PLC
. Can i do this? Thanks a lot

推荐答案

您可以在C#中使用
nameof(yourVariable)

结构。不确定这是否适用于您拥有的代码,但它是一个起点。

structure in C#. Not sure if that will work with the code you have, but it's a starting point.


如果标记值(PATH)的数量很小,则公开方法为每个人写入值。这些将在方法签名中具有正确类型的值参数。不要公开带有任意标记字符串的方法。

例如:
If the number of tag values (PATHs) is small, then expose a method for each of them to write the value. These would have the correct type of value parameter in the method signature. Don't expose the method that takes an arbitrary tag string.
E.g.:
const string PathA="A";
const string PathB="B";
public bool WritePathA(int value)
{
  return WritePLC(PathA, value);
}
public bool WritePathB(string value)
{
  return WritePLC(PathB, value);
}
private bool WritePLC(string tag, int value)
{
  // send value somehow
  return true; // success?
}
private bool WritePLC(string tag, string value)
{
  // send value somehow
  return true; // success?
}



这也假设路径和值类型之间的关联在编译时是已知的。你暗示这是你想要的,然而你还说明了

When i get the connection to PLC, I dynamically know the type of every tag

这意味着,在您连接到PLC之前,类型信息已知。在这种情况下,编译器显然不可能知道在运行时才知道的内容!

which implies to me that the type information is not known until you're connected to the PLC. In this case it is obviously impossible for the compiler to know what will not be known until run time!


这篇关于如何建议用户参数的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆