Imported Upstream version 5.20.0.180

Former-commit-id: ff953ca879339fe1e1211f7220f563e1342e66cb
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-02-04 20:11:37 +00:00
parent 0e2d47d1c8
commit 0510252385
3360 changed files with 83827 additions and 39243 deletions

View File

@@ -1897,5 +1897,72 @@ namespace System.Runtime.InteropServices
throw new NotSupportedException();
}
#endif
internal class MarshalerInstanceKeyComparer : IEqualityComparer<(Type, string)> {
public bool Equals ((Type, string) lhs, (Type, string) rhs) {
return lhs.CompareTo(rhs) == 0;
}
public int GetHashCode ((Type, string) key) {
return key.GetHashCode ();
}
}
internal static Dictionary<(Type, string), ICustomMarshaler> MarshalerInstanceCache;
internal static readonly object MarshalerInstanceCacheLock = new object ();
internal static ICustomMarshaler GetCustomMarshalerInstance (Type type, string cookie) {
var key = (type, cookie);
LazyInitializer.EnsureInitialized (
ref MarshalerInstanceCache,
() => new Dictionary<(Type, string), ICustomMarshaler> (new MarshalerInstanceKeyComparer ())
);
ICustomMarshaler result;
bool gotExistingInstance;
lock (MarshalerInstanceCacheLock)
gotExistingInstance = MarshalerInstanceCache.TryGetValue (key, out result);
if (!gotExistingInstance) {
MonoMethod getInstanceMethod;
try {
getInstanceMethod = (MonoMethod)type.GetMethod (
"GetInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
null, new Type[] { typeof(string) }, null
);
} catch (AmbiguousMatchException) {
throw new ApplicationException ($"Custom marshaler '{type.FullName}' implements multiple static GetInstance methods that take a single string parameter.");
}
if ((getInstanceMethod == null) ||
(getInstanceMethod.ReturnType != typeof (ICustomMarshaler))) {
throw new ApplicationException ($"Custom marshaler '{type.FullName}' does not implement a static GetInstance method that takes a single string parameter and returns an ICustomMarshaler.");
}
Exception exc;
try {
result = (ICustomMarshaler)getInstanceMethod.InternalInvoke (null, new object[] { cookie }, out exc);
} catch (Exception e) {
// FIXME: mscorlib's legacyUnhandledExceptionPolicy is apparently 1,
// so exceptions are thrown instead of being passed through the outparam
exc = e;
result = null;
}
if (exc != null) {
var edi = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture (exc);
edi.Throw ();
}
if (result == null)
throw new ApplicationException ($"A call to GetInstance() for custom marshaler '{type.FullName}' returned null, which is not allowed.");
lock (MarshalerInstanceCacheLock)
MarshalerInstanceCache[key] = result;
}
return result;
}
}
}