小编Cal*_*ter的帖子

使用StructLayout在c#中联合

我有多个结构,都以头结构开头.像这样

public struct BaseProtocol {
    public Header header;
    public Footer footer;
};
Run Code Online (Sandbox Code Playgroud)

标题是

public struct Header {
    public Byte start;
    public Byte group;
    public Byte dest;
    public Byte source;
    public Byte code;
    public Byte status;
};
Run Code Online (Sandbox Code Playgroud)

现在的问题是我需要将它们与Byte []结合起来.我试过这个

[StructLayout( LayoutKind.Explicit, Size=255 )]
public struct RecBuffer {

    [FieldOffset( 0 )]
    public Header header;

    [FieldOffset( 0 )]
    [MarshalAs( UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 255 )]
    public Byte[] buffer;
};
Run Code Online (Sandbox Code Playgroud)

当我用缓冲区填充数据时,我无法从标题中获取数据.如何使c#与c ++中的union相同?

c# union structlayout

3
推荐指数
1
解决办法
1985
查看次数

使用DbContext的TypeLoadException

我有2个项目.第一个项目是一个dll项目,第二个是mvc4网站.在dll中我有一个实体数据模型,我自动生成了DbContext.

在Global.asax中,我使用以下行初始化数据库:

System.Data.Entity.Database.SetInitializer( new DropCreateDatabaseAlways<WebConfigEntities>() );
Run Code Online (Sandbox Code Playgroud)

当我启动网站时,我得到一个TypeLoadException

Server Error in '/' Application.
GenericArguments[0], WebConfigDB.WebConfigEntities, voor System.Data.Entity.IDatabaseInitializer`1[TContext] is in strijd met de beperking van typeparameter TContext.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.TypeLoadException: GenericArguments[0], WebConfigDB.WebConfigEntities, voor System.Data.Entity.IDatabaseInitializer`1[TContext] is in strijd met de beperking van typeparameter TContext.

Source Error:

Line 46: 
Line 47:            BundleTable.Bundles.EnableDefaultBundles();
Line …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework

2
推荐指数
1
解决办法
1677
查看次数

标签 统计

c# ×2

asp.net-mvc ×1

entity-framework ×1

structlayout ×1

union ×1