I am currently wrapping Un4seen BASS audio library in .Net
BASS functions returning a bool
value, require checking LastError
to know the Error that occurred if false is returned.
So I implemented a Return<T>
class to make it object-oriented:
using ManagedBass.Dynamics;
namespace ManagedBass
{
/// <summary>
/// Wraps a Bass Error in a function return value
/// </summary>
/// <typeparam name="T">The Type of the function return value</typeparam>
public class Return<T>
{
public Errors ErrorCode { get; }
Return(T Value)
{
ErrorCode = Bass.LastError;
this.Value = Value;
}
public T Value { get; }
public static implicit operator T(Return<T> e) => e.Value;
public static implicit operator Return<T>(T e) => new Return<T>(e);
public bool Success => ErrorCode == Errors.OK;
}
}
Now for methods like:
[DllImport(DllName, EntryPoint = "BASS_StreamFree")]
public static extern bool StreamFree(int Handle);
I would instead want to return a Return<bool>
so that the user can easily check the ErrorCode
in object-oriented way.
Now, you may say that I create another method like:
// bool implicitly converts to Return<bool>
public static Return<bool> StreamFree2(int Handle) => StreamFree(Handle)
But, the problem is that there are already thousands of DllImports and doing this for all of them would be a nightmare.
So, I tried:
[DllImport(DllName, EntryPoint = "BASS_StreamFree")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern Return<bool> StreamFree(int Handle);
It compiled, but didn't work at all.
Can anyone please point me out where I am wrong or is something like this even possible.
Thanks in Advance.