Skip to content

Less-strongly-typed FilePathMarshaler #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions LibGit2Sharp/Core/FilePathMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,29 @@ public IntPtr MarshalManagedToNative(Object managedObj)
}

var filePath = managedObj as FilePath;
if (null != filePath)
{
return FromManaged(filePath);
}

if (null == filePath)
var expectedType = typeof(FilePath);
var actualType = managedObj.GetType();

if (actualType.FullName == expectedType.FullName)
{
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));
var posixProperty = actualType.GetProperty("Posix");
if (posixProperty != null && posixProperty.PropertyType == typeof(string))
{
var reflectedFilePath = (string)posixProperty.GetValue(managedObj, null);
return FromManaged(reflectedFilePath);
}
}

return FromManaged(filePath);
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)
Expand All @@ -111,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)
Expand Down