Forums

Missing behavior for dynamic??

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Missing behavior for dynamic??

Postby nevdelap » Fri Jul 02, 2010 4:42 am

Hi Chuck and gang,

Hopefully this should illustrate what I'm trying to do. I'm expecting dynamic to do something that it doesn't. I want dynamic to be able to call statics on classes so that I don't have to use InvokeMethod to do it when using dynamically loaded types. Or there is another way??

Cheers.

Code: Select all
use System.Reflection

class Test

   def method1
      print '1'

   def method2 is shared
      print '2'

class Program

   def main is shared

      # As expected.
      test1 as dynamic = Test()
      test1.method1
      test1.method2

      # As expected.
      Test.method2

      # Silly looking, but a simple version of what I really want to do below.
      test2 as dynamic = Test
      expect Cobra.Lang.UnknownMemberException # Is not finding the method on Type,
         test2.method2                   # ok, but could/should it look for
                                     # statics on Test as well??

      # Using dynamically loaded assemblies...
      mono = Assembly.load('Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756')

      # ...this is what I really want to do... (minus the expect of course)
      unixFileSystemInfo as dynamic? = mono.getType('Mono.Unix.UnixFileSystemInfo')
      expect Cobra.Lang.UnknownMemberException
         # UnixFileSystemInfo.GetFileSystemEntry() is static.
         fileInfo as dynamic? = unixFileSystemInfo.getFileSystemEntry('/usr/local/bin/cobra')

      # ...to avoid doing this...
      unixFileSystemInfo2 as Type = mono.getType('Mono.Unix.UnixFileSystemInfo')
      fileInfo as dynamic? = unixFileSystemInfo2.invokeMember('GetFileSystemEntry',
                                                BindingFlags.Static | BindingFlags.InvokeMethod,
                                                nil,
                                                nil,
                                                ['/usr/local/bin/cobra'].toArray)

      # Dynamic needs to search for BindingFlags.Static or something?


PS: I've got my tabs=4 and the code block seems to not be indenting correctly. The three comments start "# Is not finding the method on" and the five parameters of invokeMember() should be vertically aligned.

PPS: I tried cobra tags and the tabs were expanded to 8 characters and the lines got chopped off. I guess I need to untabify.
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: Missing behavior for dynamic??

Postby Charles » Fri Jul 02, 2010 6:45 am

I'm about to step away so some quick notes:

-- Don't worry about untabifying. I don't.

-- Lines chopped off? I haven't seen that. Maybe running long and needing horiz scrolling...

-- I hope to eventually fix the <cobra> tags to set tabs to 4 and syntax highlight, but haven't had the chance. Our software is phpBB if anyone wants to take a crack at it.

-- I was playing with dynamic binding earlier and also discovered that it is not seeing static methods, which is disappointing. Would you fill out a Trac ticket?

-- For combining enums, you can say BindingFlags(Static, InvokeMethod)

-- For arrays, you can use the list syntax prefixed by @ as in @['/foo/bar']

-- Until I get dynamic+static fixed, you could make a wrapper class to "encapsulate the ugly". :-D
class UnixFileSys

var _type as Type

cue init(type as Type)
base.init
_type = type

def getFileSystemEntry as dynamic
return _type.invokeMember(...

-- Dynamic should work well with objects and instance methods so once you're getting objects such as from "getFileSystemEntry" it should get easier.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Missing behavior for dynamic??

Postby nevdelap » Fri Jul 02, 2010 7:49 am

Cool. Cheers!
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: Missing behavior for dynamic??

Postby Charles » Fri Jul 02, 2010 7:02 pm

dynamic+static is now fixed in development.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 71 guests