From aa79dba48e0b891d083f1c103838f2e487c28a7d Mon Sep 17 00:00:00 2001 From: Arnav Das Date: Mon, 29 Apr 2019 20:19:14 +0530 Subject: [PATCH 1/2] update hasattr and getattr --- src/TensorFlowNET.Core/Python.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/TensorFlowNET.Core/Python.cs b/src/TensorFlowNET.Core/Python.cs index 6289124c..1f066583 100644 --- a/src/TensorFlowNET.Core/Python.cs +++ b/src/TensorFlowNET.Core/Python.cs @@ -163,25 +163,36 @@ namespace Tensorflow return (__memberobject__.Length > 0) ? true : false; } public delegate object __object__(params object[] args); - public static __object__ getattr(object obj, string key) + public static __object__ getattr(object obj, string key, params Type[] ___parameter_type__) { var __dyn_obj__ = obj.GetType().GetMember(key); if (__dyn_obj__.Length == 0) - throw new Exception("The object \"" + nameof(obj) + "\" doesn't have a defination \"" + key + "\""); + throw new Exception("The object \"" + nameof(obj) + "\" doesnot have a defination \"" + key + "\""); var __type__ = __dyn_obj__[0]; if (__type__.MemberType == System.Reflection.MemberTypes.Method) { - var __method__ = obj.GetType().GetMethod(key); - return (__object__)((object[] args) => __method__.Invoke(obj, args)); + try + { + var __method__ = (___parameter_type__.Length > 0) ? obj.GetType().GetMethod(key, ___parameter_type__) : obj.GetType().GetMethod(key); + return (__object__)((object[] args) => __method__.Invoke(obj, args)); + } + catch (System.Reflection.AmbiguousMatchException ex) + { + throw new Exception("AmbigousFunctionMatchFound : (Probable cause : Function Overloading) Please add parameter types of the function."); + } } else if (__type__.MemberType == System.Reflection.MemberTypes.Field) { var __field__ = (object)obj.GetType().GetField(key).GetValue(obj); return (__object__)((object[] args) => { return __field__; }); } + else if (__type__.MemberType == System.Reflection.MemberTypes.Property) + { + var __property__ = (object)obj.GetType().GetProperty(key).GetValue(obj); + return (__object__)((object[] args) => { return __property__; }); + } return (__object__)((object[] args) => { return "NaN"; }); } - } public interface IPython : IDisposable From ecb6dd99da437a891a8cd876bf8a0f31305129d2 Mon Sep 17 00:00:00 2001 From: Arnav Das Date: Mon, 29 Apr 2019 20:23:01 +0530 Subject: [PATCH 2/2] Added hasattr and getattr tests --- .../TensorFlowNET.UnitTest/PythonBaseTests.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/TensorFlowNET.UnitTest/PythonBaseTests.cs diff --git a/test/TensorFlowNET.UnitTest/PythonBaseTests.cs b/test/TensorFlowNET.UnitTest/PythonBaseTests.cs new file mode 100644 index 00000000..2a9c6af6 --- /dev/null +++ b/test/TensorFlowNET.UnitTest/PythonBaseTests.cs @@ -0,0 +1,34 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.UnitTest +{ + [TestClass] + public class PythonBaseTests : PythonTest + { + [TestMethod] + public void hasattr_getattr() + { + var s1 = "Tensorflow v0.1"; + var f = "Tensorflow"; + var r = "Tensorflow.NET"; + var res = s1.Replace(f, r); + + // Test 1 + Assert.IsTrue(hasattr(s1, "Replace")); + + // Test 2 + var o = getattr( s1, "Replace", typeof(string), typeof(string)); + Assert.AreEqual(res, o(f, r)); + + // Test 3 + var l = getattr(s1, "Length"); + Assert.AreEqual(s1.Length, l()); + + } + } +} +