3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
7 using System
.Collections
.Generic
;
8 using System
.Reflection
;
14 public class WvMonikerAttribute
: Attribute
17 public class WvMonikerBase
19 static object lockobj
= new Object();
20 static bool registered
= false;
22 protected static void register_all()
26 // feebly attempt to be threadsafe: prevent registrations from
27 // running except in one thread.
30 if (registered
) return;
33 WvReflection
.find_types(typeof(WvMonikerAttribute
)))
35 t
.InvokeMember("wvmoniker_register",
38 | BindingFlags
.InvokeMethod
,
47 public class WvMoniker
<T
>: WvMonikerBase
49 static List
<WvMoniker
<T
>> registry
= new List
<WvMoniker
<T
>>();
51 Func
<string,object,T
> func
;
53 public static WvMoniker
<T
>
54 register(string prefix
, Func
<string,object,T
> func
)
56 return new WvMoniker
<T
>(prefix
, func
);
59 public WvMoniker(string prefix
, Func
<string,object,T
> func
)
66 // probably nobody will ever call this
67 public void unregister()
69 registry
.Remove(this);
72 public static WvMoniker
<T
> find(string prefix
)
74 foreach (WvMoniker
<T
> m
in registry
)
75 if (m
.prefix
== prefix
)
80 public static T
create(string moniker
, object o
)
84 int pos
= moniker
.IndexOf(':');
85 string prefix
, suffix
;
88 prefix
= moniker
.Substring(0, pos
);
89 suffix
= moniker
.Substring(pos
+1);
97 WvMoniker
<T
> m
= find(prefix
);
101 return m
.func(suffix
, o
);
104 public static T
create(string moniker
)
106 return create(moniker
, null);