我的项目中有以下类,出于测试目的,每次访问 my_byte 属性时,我都希望其元素交替。
namespace my_namespace
{
public class my_class
{
public byte[] my_byte { get; set; } = { 0x00, 0x00, 0x00 };
}
}
我的意思是当 my_byte 被访问时,它现在返回 0x00, 0x00, 0x00
。我想要的是下次调用它时它会返回 0xFF, 0x00, 0x00
,而另一次又返回 0x00, 0x00, 0x00
等等......所以它会交替。
这甚至可能吗?
编辑:
在主类中,该方法被调用为:
namespace my_namespace
{
public class Main_Class
{
Class_X obj = new Class_X()
Thread.Sleep(1000);
byte[] my_received_data= obj.my_method(a, b, c);
}}
这个 my_method 在一个类中:
namespace my_namespace
{
class Class_X
{
public byte[] my_method(byte[] a, int b, bool c)
{
//here method works properly
}
}
然后我有一个界面
namespace my_namespace
{
public interface IMy_Interface
{
byte[] another_method(byte[] a, bool b, Class_Z c);
}
}
然后是上面接口的一个类:
namespace my_namespace
{
public class Class_Q:IMy_Interface
{
my_class obj_x = new my_class();
public byte[] another_method(byte[] a, bool b, Class_Z c)
{
//this calles properties from my_class
}
}
}
这是带有相关道具的类:
namespace my_namespace
{
public class my_class
{
public byte[] my_byte { get; set; } = { 0x00, 0x00, 0x00 };
}
}
正如您在每次重新调整道具之前看到的那样,创建了新的 obj_x,它不会让布尔状态交替 :(
EDIT2:
namespace my_namespace
{
class my_class
{
const int index = 2;
private bool alternate = true;
public byte[] SendT(byte[] t_sent, bool test_port)
{
byte[] returnValue = null;
if (t_sent[2] == 0xD3)
{
if (alternate)
{
returnValue = {0x00, 0x00, 0x00 };
}
else
{
returnValue = {0xFF, 0x00, 0x00 };
}
alternate = !alternate;
}
else
{
return null;
}
return returnValue;
}
}
}
答案 0 :(得分:1)
你会想要你的 2 字节数组和一个布尔值,每次访问属性时都会来回翻转。
namespace my_namespace
{
public class AlternatePropertyClass
{
public byte[] AlternatingBytes
{
get
{
// Get the alternating array value
//
return AlternatingByteArray();
}
}
// The byte arrays to alternate between
//
private byte[] bytes1 = new byte[] { 0x00, 0x00, 0x00 };
private byte[] bytes2 = new byte[] { 0xFF, 0x00, 0x00 };
// The state variable indicating which array to return
//
private bool isFirstByteArray = false;
// The alternating function which changes the state variable back and forth
//
private byte[] AlternatingByteArray()
{
// Flip the value of the boolean
//
isFirstByteArray = !isFirstByteArray;
// Return the appropriate array based on the state of isFirstByteArray as a ternary operation
//
return isFirstByteArray ? bytes1 : bytes2;
}
}
}
测试:
var c = new AlternatePropertyClass();
Console.WriteLine(c.AlternatingBytes[0].ToString());
Console.WriteLine(c.AlternatingBytes[0].ToString());
Console.WriteLine(c.AlternatingBytes[0].ToString());
Console.WriteLine(c.AlternatingBytes[0].ToString());
结果:
0
255
0
255
这是一个可以从任何地方调用的静态版本。
public static class AlternatingBytes
{
private static byte[] bytes1 = new byte[] { 0x00, 0x00, 0x00 };
private static byte[] bytes2 = new byte[] { 0xFF, 0x00, 0x00 };
private static bool isFirstByteArray = false;
public static byte[] GetAlternatingBytes()
{
isFirstByteArray = !isFirstByteArray;
return isFirstByteArray ? bytes1 : bytes2;
}
}
根据您的代码,您可以这样称呼它:
var bytes = AlternatingBytes.GetAlternatingBytes();
答案 1 :(得分:0)
很有可能。您可以编写自己的 get
方法并使用字段(或其他属性)来控制交替。举个例子:
namespace my_namespace
{
public class my_class
{
private bool alternate = false;
public byte[] My_byte
{
get
{
byte[] returnValue;
if (alternate)
{
returnValue = new byte[] { 0x00, 0x00, 0x00 };
}
else
{
returnValue = new byte[] { 0xFF, 0x00, 0x00 };
}
alternate = !alternate;
return returnValue;
}
}
}
}
我还建议您花一些时间阅读 MS Docs。 Properties 上的页面可能有用。
编辑 - 这是一个重复获取和打印值的主要内容:
public static void Main()
{
var x = new my_class();
for (int i = 0; i < 10; i++)
{
foreach (var y in x.My_byte) {
Console.WriteLine(y);
}
Console.WriteLine("=====");
Thread.Sleep(1000);
}
}
示例输出:
255 0 0 ===== 0 0 0 ===== 255 0 0 ===== 0 0 0 =====