Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Generic Type Implicit Conversion?

lets say i have the follow code....


public void SetValue<t>(t Val)
{
BitConverter.GetBytes((t)Val);
}

since BC won't accept Generic vars as valid args, how would i efficiently be able to do a implicit conversion with t?

without having to do a if --> typeof().

thanks;
you could use reflection
Code
public byte[] SetValue<t>(t Val)
{
    return (byte[])typeof(BitConverter).GetMethod("GetBytes", new Type[] { typeof(t) }).Invoke(null, new object[] { Val });
}

or

Code
public static byte[] SetValue<t>(t Val)
{
    byte[] returnBytes = new byte[Marshal.SizeOf(Val)];
    var handle = GCHandle.Alloc(Val, GCHandleType.Pinned);
    Marshal.Copy(handle.AddrOfPinnedObject(), returnBytes, 0, returnBytes.Length);
    handle.Free();
    return returnBytes;
}
Lotta blood to let, peace to make, give a fuk about a check
Correct me if I am wrong but can't you do this:
Code
public void SetValue<t>(t Val) where Val : TypeYouWant
{
BitConverter.GetBytes((t)Val);
}
Or am I understanding the problem wrong.
The only way you would be able to do this is via reflection (as BahNahNah gives the example of). The marshaling example is interesting, however.
New to trading cryptocurrencies but don't know where to start? Join Binance here! Binance is my favourite exchange for cryptocurrencies w/ low fees.

[Image: 793854efd3c9ee7d90ddd413ae9deed6.png]
(Sep 30, 2016 08:11 PM)BahNahNah Wrote:  you could use reflection
Code
public byte[] SetValue<t>(t Val)
{
    return (byte[])typeof(BitConverter).GetMethod("GetBytes", new Type[] { typeof(t) }).Invoke(null, new object[] { Val });
}

or

Code
public static byte[] SetValue<t>(t Val)
{
    byte[] returnBytes = new byte[Marshal.SizeOf(Val)];
    var handle = GCHandle.Alloc(Val, GCHandleType.Pinned);
    Marshal.Copy(handle.AddrOfPinnedObject(), returnBytes, 0, returnBytes.Length);
    handle.Free();
    return returnBytes;
}
thanks.
(Sep 30, 2016 08:11 PM)BahNahNah Wrote:  you could use reflection
Code
public byte[] SetValue<t>(t Val)
{
    return (byte[])typeof(BitConverter).GetMethod("GetBytes", new Type[] { typeof(t) }).Invoke(null, new object[] { Val });
}

or

Code
public static byte[] SetValue<t>(t Val)
{
    byte[] returnBytes = new byte[Marshal.SizeOf(Val)];
    var handle = GCHandle.Alloc(Val, GCHandleType.Pinned);
    Marshal.Copy(handle.AddrOfPinnedObject(), returnBytes, 0, returnBytes.Length);
    handle.Free();
    return returnBytes;
}

I would recommend adding to that:
Code
class GCHandleObject : IDisposable
{
        private bool _handleDisposed;
        private GCHandle _handle;

        public object Target => _handle.Target;
        public bool IsAllocated => _handle.IsAllocated;

        public GCHandleObject(object value, GCHandleType type)
        {
            _handleDisposed = false;
            _handle = GCHandle.Alloc(value, type);
        }

        public IntPtr AddrOfPinnedObject()
        {
            return _handle.AddrOfPinnedObject();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!_handleDisposed)
                {
                    _handleDisposed = true;
                    _handle.Free();
                }
            }
        }
}

public static byte[] GetBytes<T>(T value) where T : struct
{
    byte[] returnBytes = new byte[Marshal.SizeOf(value)];
    using(var obj = new GCHandleObject(value, GCHandleType.Pinned))
    {
        Marshal.Copy(obj.AddrOfPinnedObject(), returnBytes, 0, returnBytes.Length);
    }
    return returnBytes;
}

If not taking on the RAII-like pattern, at least constrain your type parameter to what Marshal.SizeOf() expects it's arguments to be.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by maryousJul 24, 2022 03:35 PM 0 159 Jul 24, 2022 03:35 PM
Last Post: maryous
[C#] by debugDec 22, 2020 04:51 PM 0 226 Dec 22, 2020 04:51 PM
Last Post: debug
[C] by F.A.S.TJul 24, 2018 03:24 PM 7 280 Jul 24, 2018 03:24 PM
Last Post: aeiro