[Mono-osx] Statics and Singletons with Dumbarton [Solved with patch]

R. Tyler Ballance tyler at bleepconsulting.com
Tue Jan 2 07:32:32 EST 2007


On Jan 2, 2007, at 5:34 AM, Andreas Färber wrote:

> No idea, I don't use Dumbarton in my Cocoa app. But even when using  
> Dumbarton you can use the embedding functions of Mono directly like  
> Dumbarton as a wrapper does internally, or if it's missing in  
> Dumbarton patch it in.
> The initWithMonoObject:NULL above will not lead to success as Mono  
> cannot retrieve a MonoClass* from a NULL reference. Logically I  
> would expect such ObjC messages at a class wrapper, say result =  
> [myClass staticProperty("MyProperty")];

Taking all of this into consideration I have another patch that I  
have tested with my singleton method, and some other miscellaneous  
static properties being bridged between Dumbarton and Cocoa.

This patch adds the following methods to DBMonoObjectRepresentation:

+ (MonoObject *)invokeStaticMethod:(const char *)methodName  
withNumArgs:(int)numArgs varArgList:(va_list)va_args;
+ (MonoObject *)invokeStaticMethod:(const char *)methodName  
withNumArgs:(int)numArgs, ...;
+ (MonoObject *)getStaticProperty:(const char *)propertyName;
+ (void)setStaticProperty:(const char *)propertyName valueObject: 
(MonoObject *)valueObject;


In the singleton example I cited earlier, the following code will  
properly funnel singleton instantiation through the Instance property  
in C#:

+ (SingletonObject *)sharedObject
{
	@synchronized(self)
	{
		if (instance == nil)
		{
			MonoObject *obj = [SingletonObject getStaticProperty:"Instance"];
			instance = [[self alloc] initWithMonoObject:obj];
		}
	}
	return instance;
}

Then elsewhere in my code I can call static methods like:
[SingletonObject testPrint]; // test static method to print something  
via Console.WriteLine

I'm using Dumbarton with this patch incorporated thus far without any  
problems. My DBMonoObjectRepresentation follows the Cocoa singleton  
pattern, as the actual Mono object it represents follows the C#  
singleton pattern.

Thanks for the help Andreas!

Allan (or whomever it may concern) let me know if I need to fix  
anything or change some patches to allow this to be committed to  
subversion. I was wondering if I should just be using  
DBMonoClassRepresentation, but the examples use  
DBMonoObjectRepresentation, so I have assumed that it is the "defacto  
standard" for bridging C#->Cocoa objects


Cheers


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/45a23676/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DBMonoObjectRepresentation.patch
Type: application/octet-stream
Size: 2372 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20070102/45a23676/attachment.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20070102/45a23676/attachment-0001.html 


More information about the Mono-osx mailing list