[Mono-osx] Statics and Singletons with Dumbarton

R. Tyler Ballance tyler at bleepconsulting.com
Tue Jan 2 03:55:50 EST 2007


Ok, I apologize for the recent flood of emails, but i've been trying  
to tie some complex Cocoa and C# together recently.

Singletons)

	There seems to be no reasonable means of implementing a singleton  
DBMonoObjectRepresentation as it stands now with Dumbarton. My C#  
singleton is relatively "traditional" in that it has a private  
constructor, and a static Iinstance property (i..e  
Object.Instance.ExecuteMethod()). Implementing a Cocoa singleton  
isn't hard, but initWithSignature and initWithMonoObject aren't  
exactly suitable. I attempted (in vain of course) to use getProperty  
to return a MonoObject to initWithMonoObject, which requires an  
instantiated DBMonoObjectRepresentation to function (d'oh).

	I had another property, and I found that if I used [[SingletonObject  
sharedObject] property]; it would call my default constructor,  
regardless of the private modifier (!).
  Whereas the code would be:

- (NSString *)property
{
	MonoString *monoString = (MonoString*)[self  
getProperty:"someproperty"];
	return [NSString stringWithMonoString:monoString];
}

#pragma mark "Singelton Methods"
+ (SingletonObject *)sharedObject
{
	@synchronized(self)
	{
		if (instance == nil)
		{
			instance = [[self alloc] init];
		}
	}
	return instance;
}


And the C# code is somewhere along the lines of :
		public static SingletonObject Instance
		{
			get
			{
				lock(internalLock)
				{
					if (instance == null)
						instance = new SingletonObject();
						
					Console.WriteLine("Calling Instance Property");	
					return instance;
				}
			}
		}		
		public string someproperty { get { return m_SomeProperty; } }

		private SingletonObject()
		{
			m_SomeProperty = this.GeneratePath();
		}


Dumbarton would violate the access modifier for the constructor, and  
call it, and then when I would call the property method in my Cocoa  
code, it'd function as expected. This seems like a subtle bug, or is  
it something that is "allowed" within the constraints of the Mono  
embedded runtime?



Statics)

	As far as I can tell there's not really a feasible way to call  
static methods through the Dumbarton bridge i.e. I cannot write:
Cocoa:
+ (NSString *)getStringFromCSharp
{
	MonoString *monoString = (MonoString*)[DBClass  
invokeStaticMethod:"returnString" withNumArgs:0]];
	return [NSString stringWithMonoString:monoString];
}

C#:
public static string returnString()
{
	return "OMFG!";
}

I've been looking through the embedding Mono examples to see if  
there's a good means of doing so with the embedded runtime but from  
the best that I can tell, almost all the calls require an instatiated  
MonoObject before invoking a method.

It seems like having statics would fix my problem with singletons  
immediately, is there a piece of the puzzle I'm not seeing?

If not, any pointers on where I could look to add this functionality  
to the bridge?


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/071ffcc1/attachment-0001.html 


More information about the Mono-osx mailing list