Tag: struct

C#中的结构数组

我正在尝试使用结构数组从用户那里获得输入然后打印它: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CA4 { class Program { static void Main(string[] args) { StudentDetails[,] student = new StudentDetails[5, 1]; Console.WriteLine(“Please enter the unit code:”); student[0, 0].unitCode = Console.ReadLine(); Console.WriteLine(“Please enter the unit number:”); student[1, 0].unitNumber = Console.ReadLine(); Console.WriteLine(“Please enter first name:”); student[2, 0].firstName = Console.ReadLine(); Console.WriteLine(“Please enter last […]

为什么具有隐式转换运算符的自定义结构上的Assert.AreEqual失败?

我创建了一个自定义结构来表示金额。 它基本上是decimal的包装器。 它有一个隐式转换运算符,可将其强制转换为decimal 。 在我的unit testing中,我断言Amount等于原始十进制值,但测试失败。 [TestMethod] public void AmountAndDecimal_AreEqual() { Amount amount = 1.5M; Assert.AreEqual(1.5M, amount); } 当我使用int(我没有创建转换运算符)时,测试确实成功。 [TestMethod] public void AmountAndInt_AreEqual() { Amount amount = 1; Assert.AreEqual(1, amount); } 当我hoverAreEqual ,它显示第一个解析为 public static void AreEqual(object expected, object actual); 第二个导致 public static void AreEqual(T expected, T actual); 看起来int值1隐式地转换为Amount ,而小数值1.5M则不是。 我不明白为什么会这样。 我本来希望恰恰相反。 第一个unit testing应该能够将decimal转换为Amount 。 […]

有没有办法在不使用构造函数的情况下初始化结构的成员?

我有一个包含两个列表的struct : struct MonthData { public List Frontline; public List Leadership; } 但是,我想在创建结构时初始化它们。 如果我尝试: struct MonthData { public List Frontline = new List(); public List Leadership = new List(); } 然后我得到: Error 23 ‘MonthData.Frontline’: cannot have instance field initializers in structs … 由于结构不能有无参数构造函数,我不能只在构造函数中设置它。 到目前为止,我只能看到以下选项: 在创建MonthData实例时初始化这两个属性 使用类而不是结构 使用参数创建构造函数并使用它 为懒惰初始化它们的属性创建getter和setter。 推荐的方法是什么? 现在,我在想这个课程是最好的主意。

检查结构的默认值

我的DefaultValue()函数有问题。 它总是返回false ,表示结构不是默认值。 为什么这不起作用? [StructLayout(LayoutKind.Sequential)] private struct ArrayItem { public long SrcSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)] public string SrcFile; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)] public string DestFile; } [StructLayout(LayoutKind.Sequential)] private struct MyInfo { public int Count; public int AppOne; public int AppTwo; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100, ArraySubType = UnmanagedType.Struct)] public ArrayItem[] Files; } private bool DefaultValue(T […]

marshal c struct to c#

有没有人可以在c#中编组这部分c / c ++代码? typedef struct { BYTE bCommandCode; BYTE bParameterCode; struct { DWORD dwSize; LPBYTE lpbBody; } Data; } COMMAND, *LPCOMMAND; 非常感谢

使用c#将结构中的字节写入文件

大家,早安! 我在这里遇到了一个新问题。 我必须写下一个来自我在sistem中声明的结构的数据。 我创建的结构只有两个字段,我用它来稍后将其转换为字节。 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct MyStructData { public short Id; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] public string Name; } 我用以下代码转换这个结构的字节: private byte[] getBytes(MyStructData aux) { int length = Marshal.SizeOf(aux); IntPtr ptr = Marshal.AllocHGlobal(length); byte[] myBuffer = new byte[length]; Marshal.StructureToPtr(aux, ptr, true); Marshal.Copy(ptr, myBuffer, 0, length); Marshal.FreeHGlobal(ptr); return myBuffer; […]

如何搜索特定的结构值? 也许更好的方法?

我正在尝试找到我之前创建的具有特定值的结构。 一旦找到它,我想在该结构上设置变量。 我不知道该怎么做。 有更好的方法吗? 也许上课? 结构应该工作吗? 例如,我的结构: public struct MyTest { public string device; public string status; public string revision; public string number; public string ledmo; } 我的测试代码: MyTest thisTest=new MyTest(); thisTest.device=blah; thisTest.number=blah2; MyTest thisTest2=new MyTest(); thisTest2.device=blah5; thisTest2.number=blah6; //Another Part in my code. //Need to find the MyTest Structure that ‘device’ variable = the string […]

System.TypeLoadException:无法从程序集“y”加载类型“x”

我试图将程序从VB6移植到C#,读取二进制文件并解析它。 我没有编译时错误或警告但是当我尝试运行它时,在它进入Main()之前它会抛出exception System.TypeLoadException was unhandled Message=Could not load type ‘Conversion.DataStructures.ClientOld’ from assembly ‘SandboxConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ because it contains an object field at offset 1 that is incorrectly aligned or overlapped by a non-object field. Source=SandboxConsole TypeName=Conversion.DataStructures.ClientOld StackTrace: at sandbox.Program.Main(String[] args) at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() […]

访问列表c#中的对象

代码是: struct m1 { public int a; public int b; } int main() { List mList; m1.initialize; //new. and add some items to it. 现在我想在mList中访问对象,我试过: for each(m1 it in mList) { m1.a = 5; } 但它失败了。 因为每次我在控制台上写了m1.first()。a。 它的初始值不是5。 然后我试了 for (int counter = 0; counter < mList.size(); counter++) { m1 it = mList[counter]; it.a = […]

C#中的值类型类定义?

是否可以创建一个不是结构但是值类型的类,或者类似于值类型,因为它在传递时复制而不是通过引用传递。 编辑: 很抱歉在被问到后需要编辑的问题。 另外,请参阅此问题以获取更多信息。 在不存在的结构布局中循环