如何创建具有默认构造函数的记录类 [英] How to create record class with default constructor

查看:198
本文介绍了如何创建具有默认构造函数的记录类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构得到默认构造函数,如我做

structures got default constructors like if I do

type tagONEDEV_FlowRec =
    struct
        .......
    end

我可以做 new DeviceModel。 tagonEDEV_FlowRec()但不适用于此:

I can do new DeviceModel.tagONEDEV_FlowRec() but it doesn't work with this :

let (<++|) device bytes size =
    let unmanagedPtr = Marshal.AllocHGlobal(size : int)
    Marshal.Copy( (bytes : byte array), 0, unmanagedPtr, size)
    Marshal.PtrToStructure(unmanagedPtr, (device : obj)) // Here
    Marshal.FreeHGlobal(unmanagedPtr)



这里需要一个记录类

I need a record class here alike

[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
type tagONEDEV_FlowRec = { 
    mutable ....;}

type tagONEDEV_FlowRec =
    class
        .......
    end

但是这里没有默认构造函数,结构非常大,我可以使用这样的类与默认构造函数?

but there is no default constructor here and structures is very big to zero init them manually, so how can I use such classes with default constructors ?

如果我不会找到解决方案,我认为将更快的重新编码这部分在C#或甚至在VB。净。听起来像crutch解决方案,但看起来像我不能拨打F#OOP部分。

If I will not find the solution I think that will be faster for me to recode this part on C# or even on VB.NET. Sounds alike crutch-solution but looking like I can't dial with F# OOP part yet.

另外:我不想键入的东西是:

an addition: the thing I don't want to type is :

               {TimeRec     = 0; 
                Num         = 0us;
                FlagErr     = 0us;
                C6          = 0.0;
                C2H6        = 0.0;
                C3H8        = 0.0;
                CH4         = 0.0;
                CO2         = 0.0;
                iC4H10      = 0.0;
                iC5H12      = 0.0;
                neoC5H12    = 0.0;
                N2          = 0.0;
                nC5H12      = 0.0;
                O2          = 0.0;
                nC4H10      = 0.0;
                He          = 0.0;
                H2          = 0.0;
                H2O         = 0.0;
                id          = 0us; }

< - 这是我想要的默认,因为我有很多因为jpalmer试图解释一个记录已经被初始化的参数,所以这个和写这样的constuctors是邪恶的。

<- that is what I want to have by default, because I've got much lager structures then this and writing such constuctors is wicked.

推荐答案

我想你应该尝试使用

type MyStruct =
    struct
        val mutable myInt : int
        val mutable myString : string
    end

请参阅: MSDN文档显示字段

是的,我知道有更多的代码行和一个额外的结构,结束和一些val,但这是真的这么难吗?如果你在你的类型上使用1000个属性,你应该重新思考你的代码。

yeah I know there are more lines of code and an additional "struct", "end" and some "val"s but is this really this hard? If you are using 1000 properties on your type you should rethink your code anyhow.

这篇关于如何创建具有默认构造函数的记录类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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