O
O.B.
I'm trying to do a static_cast at runtime in C# and as I understand it,
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.
/* TestA.cs */
namespace TEST {
class TestA {
public ushort val1;
public ushort val2;
public ushort val3;
}
}
/* Main Program */
namespace TEST {
class Program {
static void Main(string[] args) {
TestA test = null;
byte[] testArray = new byte[6];
for (int i = 0; i < testArray.Length; i++) {
testArray = (byte)(i + 1);
}
// Trying to do a C++-style static_cast
// Compilation error:
// Cannot convert type 'byte[]' to 'TEST.TestA'
// via a built-in conversion
test = testArray as TestA;
}
}
}
What am I doing wrong?
"as" is the keyword to use. Unfortunately, the compiler is trying to do
the cast a compilation time. See below.
/* TestA.cs */
namespace TEST {
class TestA {
public ushort val1;
public ushort val2;
public ushort val3;
}
}
/* Main Program */
namespace TEST {
class Program {
static void Main(string[] args) {
TestA test = null;
byte[] testArray = new byte[6];
for (int i = 0; i < testArray.Length; i++) {
testArray = (byte)(i + 1);
}
// Trying to do a C++-style static_cast
// Compilation error:
// Cannot convert type 'byte[]' to 'TEST.TestA'
// via a built-in conversion
test = testArray as TestA;
}
}
}
What am I doing wrong?