diff --git a/LibGit2Sharp/Core/ArrayMarshaler.cs b/LibGit2Sharp/Core/ArrayMarshaler.cs index b831f6dbc..13a50c1ad 100644 --- a/LibGit2Sharp/Core/ArrayMarshaler.cs +++ b/LibGit2Sharp/Core/ArrayMarshaler.cs @@ -13,7 +13,7 @@ public ArrayMarshaler(T[] objs) for (var i = 0; i < objs.Length; i++) { - IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T))); + IntPtr ptr = Marshal.AllocHGlobal(MarshalPortable.SizeOf()); ptrs[i] = ptr; Marshal.StructureToPtr(objs[i], ptr, false); } diff --git a/LibGit2Sharp/Core/GitOdbBackend.cs b/LibGit2Sharp/Core/GitOdbBackend.cs index ce2d1119b..a3f5777fe 100644 --- a/LibGit2Sharp/Core/GitOdbBackend.cs +++ b/LibGit2Sharp/Core/GitOdbBackend.cs @@ -8,7 +8,7 @@ internal struct GitOdbBackend { static GitOdbBackend() { - GCHandleOffset = Marshal.OffsetOf(typeof(GitOdbBackend), nameof(GCHandle)).ToInt32(); + GCHandleOffset = MarshalPortable.OffsetOf(nameof(GCHandle)).ToInt32(); } public uint Version; diff --git a/LibGit2Sharp/Core/GitOdbBackendStream.cs b/LibGit2Sharp/Core/GitOdbBackendStream.cs index 1bd6439ba..d2771f598 100644 --- a/LibGit2Sharp/Core/GitOdbBackendStream.cs +++ b/LibGit2Sharp/Core/GitOdbBackendStream.cs @@ -15,7 +15,7 @@ internal class GitOdbBackendStream { static GitOdbBackendStream() { - GCHandleOffset = Marshal.OffsetOf(typeof(GitOdbBackendStream), nameof(GCHandle)).ToInt32(); + GCHandleOffset = MarshalPortable.OffsetOf(nameof(GCHandle)).ToInt32(); } public IntPtr Backend; diff --git a/LibGit2Sharp/Core/GitSmartSubtransport.cs b/LibGit2Sharp/Core/GitSmartSubtransport.cs index 73345e511..9c545d3c0 100644 --- a/LibGit2Sharp/Core/GitSmartSubtransport.cs +++ b/LibGit2Sharp/Core/GitSmartSubtransport.cs @@ -8,7 +8,7 @@ internal class GitSmartSubtransport { static GitSmartSubtransport() { - GCHandleOffset = Marshal.OffsetOf(typeof(GitSmartSubtransport), nameof(GCHandle)).ToInt32(); + GCHandleOffset = MarshalPortable.OffsetOf(nameof(GCHandle)).ToInt32(); } public action_callback Action; diff --git a/LibGit2Sharp/Core/GitSmartSubtransportStream.cs b/LibGit2Sharp/Core/GitSmartSubtransportStream.cs index 22dc6564f..90853b415 100644 --- a/LibGit2Sharp/Core/GitSmartSubtransportStream.cs +++ b/LibGit2Sharp/Core/GitSmartSubtransportStream.cs @@ -8,7 +8,7 @@ internal class GitSmartSubtransportStream { static GitSmartSubtransportStream() { - GCHandleOffset = Marshal.OffsetOf(typeof(GitSmartSubtransportStream), nameof(GCHandle)).ToInt32(); + GCHandleOffset = MarshalPortable.OffsetOf(nameof(GCHandle)).ToInt32(); } public IntPtr SmartTransport; diff --git a/LibGit2Sharp/Core/MarshalPortable.cs b/LibGit2Sharp/Core/MarshalPortable.cs new file mode 100644 index 000000000..034675004 --- /dev/null +++ b/LibGit2Sharp/Core/MarshalPortable.cs @@ -0,0 +1,35 @@ +using System; +using System.Runtime.InteropServices; + +namespace LibGit2Sharp.Core +{ + internal static class MarshalPortable + { + internal static int SizeOf() + { +#if NET40 + return Marshal.SizeOf(typeof(T)); +#else + return Marshal.SizeOf(); +#endif + } + + internal static IntPtr OffsetOf(string fieldName) + { +#if NET40 + return Marshal.OffsetOf(typeof(T), fieldName); +#else + return Marshal.OffsetOf(fieldName); +#endif + } + + internal static T PtrToStructure(IntPtr ptr) + { +#if NET40 + return (T)Marshal.PtrToStructure(ptr, typeof(T)); +#else + return Marshal.PtrToStructure(ptr); +#endif + } + } +} diff --git a/LibGit2Sharp/Filter.cs b/LibGit2Sharp/Filter.cs index 050d4a83c..723b50359 100644 --- a/LibGit2Sharp/Filter.cs +++ b/LibGit2Sharp/Filter.cs @@ -261,7 +261,7 @@ int StreamCreateCallback(out IntPtr git_writestream_out, GitFilter self, IntPtr Marshal.StructureToPtr(state.thisStream, state.thisPtr, false); state.nextPtr = git_writestream_next; - state.nextStream = (GitWriteStream)Marshal.PtrToStructure(state.nextPtr, typeof(GitWriteStream)); + state.nextStream = MarshalPortable.PtrToStructure(state.nextPtr); state.filterSource = FilterSource.FromNativePtr(filterSourcePtr); state.output = new WriteStream(state.nextStream, state.nextPtr); diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index c662a478c..1f42d82dc 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -239,7 +239,7 @@ private unsafe Blob CreateBlob(Stream stream, string hintpath, long? numberOfByt } IntPtr writestream_ptr = Proxy.git_blob_create_fromstream(repo.Handle, hintpath); - GitWriteStream writestream = (GitWriteStream)Marshal.PtrToStructure(writestream_ptr, typeof(GitWriteStream)); + GitWriteStream writestream = MarshalPortable.PtrToStructure(writestream_ptr); try {