Skip to content

Commit 0832c43

Browse files
koubaaAlexCatarino
authored andcommitted
reimplement remoting check with .NET standard-compatible reflection (pythonnet#1170)
1 parent d38f864 commit 0832c43

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
@@ -146,6 +146,23 @@ internal static IntPtr ToPython<T>(T value)
146146
return ToPython(value, typeof(T));
147147
}
148148

149+
private static readonly Func<object, bool> IsTransparentProxy = GetIsTransparentProxy();
150+
151+
private static bool Never(object _) => false;
152+
153+
private static Func<object, bool> GetIsTransparentProxy()
154+
{
155+
var remoting = typeof(int).Assembly.GetType("System.Runtime.Remoting.RemotingServices");
156+
if (remoting is null) return Never;
157+
158+
var isProxy = remoting.GetMethod("IsTransparentProxy", new[] { typeof(object) });
159+
if (isProxy is null) return Never;
160+
161+
return (Func<object, bool>)Delegate.CreateDelegate(
162+
typeof(Func<object, bool>), isProxy,
163+
throwOnBindFailure: true);
164+
}
165+
149166
internal static IntPtr ToPython(object value, Type type)
150167
{
151168
if (value is PyObject)
@@ -198,15 +215,8 @@ internal static IntPtr ToPython(object value, Type type)
198215
var pyderived = value as IPythonDerivedType;
199216
if (null != pyderived)
200217
{
201-
#if NETSTANDARD
202-
return ClassDerivedObject.ToPython(pyderived);
203-
#else
204-
// if object is remote don't do this
205-
if (!System.Runtime.Remoting.RemotingServices.IsTransparentProxy(pyderived))
206-
{
218+
if (!IsTransparentProxy(pyderived))
207219
return ClassDerivedObject.ToPython(pyderived);
208-
}
209-
#endif
210220
}
211221

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

0 commit comments

Comments
 (0)