[Mono-osx] Statics and Singletons with Dumbarton
R. Tyler Ballance
tyler at bleepconsulting.com
Tue Jan 2 16:50:02 EST 2007
On Jan 2, 2007, at 2:55 PM, Allan Hsu wrote:
>
> On Jan 2, 2007, at 12:55 AM, R. Tyler Ballance wrote:
>
> Sorry about the delay in replying, but I've been busy working/
> traveling. If you need to call/access managed static methods, you
> should either instantiate a DBMonoClassRepresentation around your
> MonoClass* or use the C-style function calls defined in DBInvoke.h.
> Neither of these should require an object instance.
>
> Here's the convention I use... If I need static functionality in a
> DBMonoObjectRepresentation subclass, I keep around a static
> MonoClass* or DBMonoClassRepresentation (depending on situation)
> that gets set in the ObjC class initializer. Any class methods in
> my subclass call through using either the DBMonoClassRepresentation
> object or the DBMonoClassX() functions in DBMonoInvoke.m. This is
> sort of what it looks like, but I'm writing it on the spot, so it
> may not work exactly right:
>
> static MonoClass *_monoClass = NULL;
>
> + (void)initialize {
> if([self class] != [MyClass class])
> return;
>
> _monoClass = (whatever you need to do to get your MonoClass *,
> probably involves +[DBMonoEnvironment monoClassWithName:
> fromAssembly:]);
> }
>
> + (NSString *)someStaticProperty {
> MonoString *monoString = DBMonoClassGetProperty(_monoClass,
> "someStaticProperty");
> return [NSString stringWithMonoString:monoString];
> }
>
> Note that the method invocation functions in DBInvoke are vararg-
> based, so it may be easier to use a DBMonoClassRepresentation for
> your purposes, but the idea is the same.
I had an inkling the "suggested" solution would be somewhat along
those lines, I wasn't sure how kosher it was to be calling the
seemingly internal DBMonoClass*() C functions from Objective-C, I was
more under the assumption of pushing as much as possible through the
objective-C methods/accessors.
I personally like to keep my Cocoa code as bracketed as possible, but
it's completely your prerogative as to whether or not you accept that
patch in lieu of the method you described above, otherwise, i'll move
my code over to using DBMonoClassGetProperty and DBMonoClassInvoke
tomorrow :P
---I just got your other email----
I'll take the examples with a grain of salt ;)
Here's the updated patch, I'm a fan of convenience methods ;)

R. Tyler Ballance: Custom Mac and Linux Development at bleep. consulting
contact: tyler at bleepconsulting.com | jabber: tyler at jabber.geekisp.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20070102/9e1bc9f1/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DBMonoObjectRepresentation.patch
Type: application/octet-stream
Size: 2364 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20070102/9e1bc9f1/attachment.obj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20070102/9e1bc9f1/attachment-0001.html
More information about the Mono-osx
mailing list