Skip to content

Commit 7a9dcfa

Browse files
authored
reimplement remoting check with .NET standard-compatible reflection (#1170)
1 parent ec424bb commit 7a9dcfa

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/runtime/converter.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ internal static IntPtr ToPython<T>(T value)
113113
return ToPython(value, typeof(T));
114114
}
115115

116+
private static readonly Func<object, bool> IsTransparentProxy = GetIsTransparentProxy();
117+
118+
private static bool Never(object _) => false;
119+
120+
private static Func<object, bool> GetIsTransparentProxy()
121+
{
122+
var remoting = typeof(int).Assembly.GetType("System.Runtime.Remoting.RemotingServices");
123+
if (remoting is null) return Never;
124+
125+
var isProxy = remoting.GetMethod("IsTransparentProxy", new[] { typeof(object) });
126+
if (isProxy is null) return Never;
127+
128+
return (Func<object, bool>)Delegate.CreateDelegate(
129+
typeof(Func<object, bool>), isProxy,
130+
throwOnBindFailure: true);
131+
}
132+
116133
internal static IntPtr ToPython(object value, Type type)
117134
{
118135
if (value is PyObject)
@@ -162,15 +179,8 @@ internal static IntPtr ToPython(object value, Type type)
162179
var pyderived = value as IPythonDerivedType;
163180
if (null != pyderived)
164181
{
165-
#if NETSTANDARD
166-
return ClassDerivedObject.ToPython(pyderived);
167-
#else
168-
// if object is remote don't do this
169-
if (!System.Runtime.Remoting.RemotingServices.IsTransparentProxy(pyderived))
170-
{
182+
if (!IsTransparentProxy(pyderived))
171183
return ClassDerivedObject.ToPython(pyderived);
172-
}
173-
#endif
174184
}
175185

176186
// hmm - from Python, we almost never care what the declared

0 commit comments

Comments
 (0)