Open-Systems-Pharmacology / rSharp

R package for communication with .NET Core
http://www.open-systems-pharmacology.org/rSharp/
GNU General Public License v2.0
1 stars 0 forks source link

`NetObject$get` and `NetObject$set` should be able to get/set the static fields #118

Closed PavelBal closed 6 months ago

PavelBal commented 7 months ago

Right now, accessing static fields of an object only works by calling the setStatic function. It should also be supported by calling object$get.

newInstance <- newObjectFromName("rSharp.Examples.SampleInstanceClass")
# show static members:
newInstance$getStaticFields() # "StaticString"

# get the value via getStatic:
getStatic("rSharp.Examples.SampleInstanceClass", "StaticString")

# Try to get it via $set
newInstance$get("NewStaticString")
Error in do.call(".External", c(list("r_call_static_method", typename,  : 
  Type:    System.ArgumentException
Message: Field or property name 'NewStaticString' not found on object of type 'SampleInstanceClass', for binding flags 'Instance, Public'
Method:  System.Object internalGetFieldOrProperty(System.Type, System.String, System.Reflection.BindingFlags, System.Object)
Stack trace:
   at ClrFacade.Internal.internalGetFieldOrProperty(Type t, String name, BindingFlags b, Object objOrNull)
   at ClrFacade.Internal.GetFieldOrProperty(Object obj, String name)
   at ClrFacade.ClrFacade.GetFieldOrProperty(Object obj, String name)
   at InvokeStub_ClrFacade.GetFieldOrProperty(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
4.
do.call(".External", c(list("r_call_static_method", typename,
methodName), args, PACKAGE = rSharpEnv$nativePkgName)) at utilities-static.R#60
3.
callStatic(rSharpEnv$clrFacadeTypeName, "GetFieldOrProperty",
type, name) at utilities-static.R#74
2.
getStatic(self$pointer, name) at net-object.R#271
1.
newInstance$get("NewStaticString")
PavelBal commented 7 months ago
   public static object GetFieldOrProperty(object obj, string name)
   {
      obj = convertSpecialObject(obj);
      const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
      var t = obj.GetType();
      return internalGetFieldOrProperty(t, name, bindingFlags, obj);
   }

Extend bindingFlags for static fields?

PavelBal commented 6 months ago

After discussion we decided that this should not be supported.