Background and Motivation
Assuming we have such native method:
[DllImport("somelib")]
public static extern void RegisterSomeStrategy(Guid identifier, [MarshalAs(UnmanagedType.LPWStr)] string displayName, delegate* unmanaged<nint, nint, nint, void> callback);
Then someone in team says "can you write a wrapper for RegisterSomeStrategy? other teammates do not want to touch this pinvoke directly, we just want to implements an interface, write strategy details in its method and call a void RegisterStrategy(InterfaceImplType)".
So I write a wrapper like following code
public interface IXXStrategy
{
public static abstract Guid Identifier { get; }
public static abstract string DisplayName { get; }
public static abstract void StrategyImpl(nint para0, nint para1, nint para2);
}
public static void RegisterSomeStragety<TStrategy>() where TStrategy : IXXStrategy
{
unsafe
{
RegisterSomeStrategy(TStrategy.Identifier, TStrategy.DisplayName, &TStrategy.StrategyImpl); // CS8786: Calling convention of 'void IXXStrategy.StrategyImpl(nint para0, nint para1, nint para2)' is not compatible with 'Unmanaged'.
}
}
Then I found there is no way to require every implementation of IXXStrategy must provide a StrategyImpl that is [UnmanagedCallersOnly] and let TStrategy knows it.
- If adding
[UnmanagedCallersOnly] to implementation class, I will get a "CS8786: Calling convention of 'void IXXStrategy.StrategyImpl(nint para0, nint para1, nint para2)' is not compatible with 'Unmanaged'.
" in RegisterSomeStragety<TStrategy>, and a "CS8932: 'UnmanagedCallersOnly' method cannot implement interface member in type" in implementation class ;
- If adding
[UnmanagedCallersOnly] to interface, I will get a "'UnmanagedCallersOnly' can only be applied to ordinary static non-abstract, non-virtual methods or static local functions." in interface.
Proposal
Since method with [UnmanagedCallersOnly] cannot implementation interface, is it part of method signature?
If it is part of method signature, can it be used in static abstract method in interface like following?
public interface IXXStrategy
{
// ...
public static abstract unmanaged void StrategyImpl(nint para0, nint para1, nint para2);
}
Background and Motivation
Assuming we have such native method:
Then someone in team says "can you write a wrapper for RegisterSomeStrategy? other teammates do not want to touch this pinvoke directly, we just want to implements an interface, write strategy details in its method and call a
void RegisterStrategy(InterfaceImplType)".So I write a wrapper like following code
Then I found there is no way to require every implementation of IXXStrategy must provide a
StrategyImplthat is[UnmanagedCallersOnly]and let TStrategy knows it.[UnmanagedCallersOnly]to implementation class, I will get a "CS8786: Calling convention of 'void IXXStrategy.StrategyImpl(nint para0, nint para1, nint para2)' is not compatible with 'Unmanaged'." in
RegisterSomeStragety<TStrategy>, and a "CS8932: 'UnmanagedCallersOnly' method cannot implement interface member in type" in implementation class ;[UnmanagedCallersOnly]to interface, I will get a "'UnmanagedCallersOnly' can only be applied to ordinary static non-abstract, non-virtual methods or static local functions." in interface.Proposal
Since method with
[UnmanagedCallersOnly]cannot implementation interface, is it part of method signature?If it is part of method signature, can it be used in static abstract method in interface like following?