[Mono-osx] Statics and Singletons with Dumbarton
Allan Hsu
allan at counterpop.net
Tue Jan 2 15:55:08 EST 2007
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.
-Allan
> 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?
--
Allan Hsu <allan at counterpop dot net>
1E64 E20F 34D9 CBA7 1300 1457 AC37 CBBB 0E92 C779
More information about the Mono-osx
mailing list