Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显式结构布局不可从接口分配?_C# - Fatal编程技术网

C# 显式结构布局不可从接口分配?

C# 显式结构布局不可从接口分配?,c#,C#,在下面的示例中,为什么isAssignable为false public struct Bar { public int A; } [StructLayout(LayoutKind.Explicit)] public struct Foo<T> : IMyInterface where T : struct { [FieldOffset(0)] public int A; [FieldOffset(4)] public T B; }

在下面的示例中,为什么isAssignable为false

public struct Bar
{
    public int A;
}

[StructLayout(LayoutKind.Explicit)]
public struct Foo<T> : IMyInterface
    where T : struct
{
    [FieldOffset(0)]
    public int A;

    [FieldOffset(4)]
    public T B;
}

public interface IMyInterface {}

...

var isAssignable = typeof(IMyInterface).IsAssignableFrom(typeof(Foo<Bar>));
公共结构栏
{
公共INTA;
}
[StructLayout(LayoutKind.Explicit)]
公共结构Foo:IMyInterface
其中T:struct
{
[字段偏移量(0)]
公共INTA;
[现场偏移(4)]
公共旅游局;
}
公共接口IMyInterface{}
...
var isAssignable=typeof(IMyInterface).IsAssignableFrom(typeof(Foo));
如果删除布局类型或将其更改为LayoutType.Sequential,则isAssignable为true


编辑:将其变成一个泛型以进一步匹配我的实际代码。

不会为我重新编程…复制/粘贴您的代码并
isAssignable==true
您可以提供有关运行时环境的更多信息吗?在内部,这将调用
RuntimeTypeHandle.CanCastTo
,它是非托管的,或者。typeof(Foo).GetInterfaces()是否包含IMyInterface?运行时环境在Unity内的Mono中使用C#7。这甚至可以编译吗?我得到“无法从程序集“…”加载类型“Foo”1,因为泛型类型不能具有显式布局”。