How to create a record class with a standard constructor

Structures

got default constructors for example if i do

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

I can do it new DeviceModel.tagONEDEV_FlowRec(), but this will not 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 post class here

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

or

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

but there is no default constructor here, and the structures are very large so that they can be initialized manually, so how can I use such classes with standard constructors?

If I don’t find a solution, I think it will be faster for me to transcode this part to C # or even to VB.NET. Sounds like a crutch solution, but it looks like I still can’t dial the F # OOP number.

addition: the thing I do not want to print 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; }

<, this is what I want to have by default, because I have a lot of lager structures, then this, and writing such conductors will be evil.

+5
5

jpalmer , . ,

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

. : MSDN

, , "struct", "end" "val", ? 1000 , - .

+2

, :

[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>]
type tagONEDEV_FlowRec =
    class
        [<DefaultValue>] val RecTime         : int;
        [<DefaultValue>] val FlowTime        : int;
        [<DefaultValue>] val AbsPress        : single;
        [<DefaultValue>] val T               : single;
        [<DefaultValue>] val DP_V_FlowRate   : single;
        [<DefaultValue>] val Volume          : double;
        [<DefaultValue>] val Energy          : double;
        [<DefaultValue>] val id              : UInt16;
        new() = {} 
    end
+6

F # , ,

type type(arg1,arg2) =
   let v1 = arg1
   let v2 = arg2
   ....

, record , , F # - http://msdn.microsoft.com/en-us/library/dd233184.aspx

, , , - . , /, .

, , System.Reflection.Assembly.CreateInstance, , ,

+3

- , , " ".

: ( , , , , , , , :).

let rec unsafeDefaultValues (x:System.Type) = 
  if x.IsValueType then System.Activator.CreateInstance(x)
  else if (Microsoft.FSharp.Reflection.FSharpType.IsRecord x) then
    let cntr = x.GetConstructors() |> Array.pick Some
    let values = 
      cntr.GetParameters()
      |> Array.map (fun p -> unsafeDefaultValues p.ParameterType)
    cntr.Invoke(values)
  else if (Microsoft.FSharp.Reflection.FSharpType.IsTuple(x)) then
    let tupleValues = 
      Microsoft.FSharp.Reflection.FSharpType.GetTupleElements(x)
      |> Array.map (unsafeDefaultValues)
    Microsoft.FSharp.Reflection.FSharpValue.MakeTuple(tupleValues, x)
  else if (x.IsArray) then
    box (System.Array.CreateInstance(x.GetElementType(), 0))
  else
    null

let unsafeDefaultValuesFor<'a> = unsafeDefaultValues typeof<'a> :?> 'a

: :

type A = {
  String : string
  Int : int
  StringOption : string Option
}

type B = {
  String : string
  Int : int
  A : A  
}

unsafeDefaultValuesFor<B>

: {String = null; Int = 0; A = {String = null; Int = 0; StringOption = null;};}

+3

CLIMutable .

[<CLIMutable>]
type MyRecord = { MyField: int }

, .

type [<CLIMutable>] MyRecord = { MyField: int }

" " - , , , , , . , - .

0

All Articles