From 85846c482d2431209f4117462c047f547c72f5aa Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 26 May 2013 19:56:34 -0500 Subject: [PATCH 1/2] Refactor FilePathMarshaler --- LibGit2Sharp/Core/FilePathMarshaler.cs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/LibGit2Sharp/Core/FilePathMarshaler.cs b/LibGit2Sharp/Core/FilePathMarshaler.cs index ec239f354..aa4a8149d 100644 --- a/LibGit2Sharp/Core/FilePathMarshaler.cs +++ b/LibGit2Sharp/Core/FilePathMarshaler.cs @@ -81,20 +81,19 @@ public IntPtr MarshalManagedToNative(Object managedObj) } var filePath = managedObj as FilePath; - - if (null == filePath) + if (null != filePath) { - var expectedType = typeof(FilePath); - var actualType = managedObj.GetType(); - - throw new MarshalDirectiveException( - string.Format(CultureInfo.InvariantCulture, - "FilePathMarshaler must be used on a FilePath. Expected '{0}' from '{1}'; received '{2}' from '{3}'.", - expectedType.FullName, expectedType.Assembly.Location, - actualType.FullName, actualType.Assembly.Location)); + return FromManaged(filePath); } - return FromManaged(filePath); + var expectedType = typeof(FilePath); + var actualType = managedObj.GetType(); + + throw new MarshalDirectiveException( + string.Format(CultureInfo.InvariantCulture, + "FilePathMarshaler must be used on a FilePath. Expected '{0}' from '{1}'; received '{2}' from '{3}'.", + expectedType.FullName, expectedType.Assembly.Location, + actualType.FullName, actualType.Assembly.Location)); } public Object MarshalNativeToManaged(IntPtr pNativeData) From f3d2b0b4e9521c4e6ee497169d2667dc2c92529c Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 26 May 2013 20:05:15 -0500 Subject: [PATCH 2/2] Use reflection to work around CustomMarshaler bug Fixes #241 --- LibGit2Sharp/Core/FilePathMarshaler.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/Core/FilePathMarshaler.cs b/LibGit2Sharp/Core/FilePathMarshaler.cs index aa4a8149d..222543725 100644 --- a/LibGit2Sharp/Core/FilePathMarshaler.cs +++ b/LibGit2Sharp/Core/FilePathMarshaler.cs @@ -89,6 +89,16 @@ public IntPtr MarshalManagedToNative(Object managedObj) var expectedType = typeof(FilePath); var actualType = managedObj.GetType(); + if (actualType.FullName == expectedType.FullName) + { + var posixProperty = actualType.GetProperty("Posix"); + if (posixProperty != null && posixProperty.PropertyType == typeof(string)) + { + var reflectedFilePath = (string)posixProperty.GetValue(managedObj, null); + return FromManaged(reflectedFilePath); + } + } + throw new MarshalDirectiveException( string.Format(CultureInfo.InvariantCulture, "FilePathMarshaler must be used on a FilePath. Expected '{0}' from '{1}'; received '{2}' from '{3}'.", @@ -110,7 +120,12 @@ public static IntPtr FromManaged(FilePath filePath) return IntPtr.Zero; } - return Utf8Marshaler.FromManaged(filePath.Posix); + return FromManaged(filePath.Posix); + } + + private static IntPtr FromManaged(string posixFilePath) + { + return Utf8Marshaler.FromManaged(posixFilePath); } public static FilePath FromNative(IntPtr pNativeData)