Hi Joe,
You can use the singleton pattern to ensure that only a single instance of
your server component may be used:
sealed class GlobalServer
{
public static readonly GlobalServer Instance = new GlobalServer();
public object GlobalState
{
get { return globalState; }
set { globalState = value; }
}
private static object globalState;
// remove the private constructor (or add a public one)
// if you want to allow creation of instances that aren't
// shared (see comments below)
private GlobalServer() { }
}
In your "client" components, you don't have to assign them any reference.
Just use GlobalServer.Instance directly or wrap it in a property:
class Client : Component
{
public GlobalServer Server
{
get
{
return GlobalServer.Instance;
}
}
}
If you need to be able to assign different instances of GlobalServer to
your Client component then simply add a "set" accessor with a local field
in which to store the reference. To assign an instance in the Form
designer using the Properties window, your GlobalInstance component would
have to derive from Component as well and must be added to the Form, but
then it will no longer be a shared (global) instance. You'll also have to
indicate to the PropertyGrid which Types are allowable for your Server
property. You can use a base editor that I made for one of my projects:
// 2.0 framework code
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Windows.Forms;
namespace YourLibrary
{
public abstract class ComponentSelectorEditor<T> : ObjectSelectorEditor
where T : class, IComponent
{
public ComponentSelectorEditor()
{
}
protected override void FillTreeWithData(Selector selector,
System.ComponentModel.ITypeDescriptorContext context, IServiceProvider
provider)
{
selector.Clear();
selector.AddNode("(none)", null, null);
Type baseType = typeof(T);
IReferenceService referenceService =
provider.GetService(typeof(IReferenceService)) as IReferenceService;
foreach (IComponent component in context.Container.Components)
{
if (baseType.IsAssignableFrom(component.GetType()))
{
string name = null;
if (component != null && component.Site != null)
name = component.Site.Name;
else if (referenceService != null)
name = referenceService.GetName(component);
else
throw new InvalidOperationException("There is no service available to
retrieve the name of one or more components.");
selector.AddNode(name, component, null);
}
}
}
}
}
With this base editor you can derive an editor that will allow properties
to bind to "GlobalServer" components:
public sealed class ServerSelectorEditor
: ComponentSelectorEditor<GlobalServer> // base component Type
{
// no implementation required
}
Note: GlobalServer should be renamed to Server since it's not global
anymore
The editor above can be assigned to your Server property like this:
class Client : Component
{
private GlobalServer server;
[Editor(typeof(ServerSelectorEditor),
typeof(System.Drawing.Design.UITypeEditor))]
[DefaultValue(null)] // not req. but recommended
public GlobalServer Server
{
get { return server; }
set { server = value; }
}
}
NOTE: You may need to build the solution and then close and restart VS for
the changes to take affect, or else you might not see any GlobalServer
components in the list even though some have been added to the Form
designer.
You could code instances of GlobalServer to access only static resources,
so in a sense you could actually do what you wanted using the editor
above, however I'd recommend using the first scenario for that and
foregoing the designer support if you don't actually need instances of
GlobalServer.
--
Dave Sexton
Joe said:
Is it possible to have a component which is global to the entire
application? I need to have a single component act sort of like a server
which components in any of the forms can access.
For example if I drop a component on Form1 & Form2 and that component has
a property called Server, at design time I would like to be able to
assign the global component to the Server property.
I'm sure I'm asking for way too much...
Thanks for any help,
Joe