πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #111

Re: Replacement for "class of" in C#?

#111
From: Joanna Carter [TeamB]
Date: Fri, 22 Dec 2006 23:44
76 lines
2938 bytes
"Shawn Oster" <soster@colorflex.com> a οΏ½crit dans le message de news: 
458c5f4b@newsgroups.borland.com...

| I'm trying to port a factory I use in Delphi to C# and have hit a snag. 
In
| Delphi I usually make light-weight factories like this:
|
| TGizmo = class(TObject)
| // stuff here
| end;
|
| TGizmoClass = class of TGizmo;
|
| TGizmoFactory = class(TObject)
| private
|   FHashedListOfGizmoClasses: TClassHashList;
| public
|   procedure RegisterClass(const ClassKey: string; ClassType: TGizmoClass);
|   function CreateObject(const ClassKey: string): TGizmo;
| end;
|
| As you can see I pass in a key and a class type, then in CreateObject I
| simply look up the key in the hash, call TGizmoClass.Create and *bam* 
there
| is my new object.
|
| My question is: How can I create a type-safe class type to pass into my
| factory?  All the examples I see require creating a concrete factory for
| every concrete class but that seems like a waste.  I'm sure I'm just
| overlooking something super simple.

Are you using .NET 2.0 ?

If so, use the Dictionary<K,V> instead of your HashList, but a factory 
method can be really simple :

public static class GizmoFactory
{
  public static Gizmo CreateObject<T>() where T : Gizmo, new()
  {
    return new T();
  }
}

This is then used like this :

public class Gizmo
{
}

public class Derived : Gizmo
{
}

void Test()
{
  Gizmo g = GizmoFactory.CreateObject<Derived>();
  ...
}

But then I guess you might as well use the straigtforward constructor call 
anyway :-(

If you want to use a string key for a hash list that has a type value, then 
this becomes a bit more difficult and you will have to lose type safety in 
order to use Activator.CreateInstance() instead to create your instances.

I have created a C# metaclass but it is not typesafe at compile time, if you 
use System.Type as the value part of a Dictionary key, then you can only 
check for type at runtime.

Joanna

-- 
Joanna Carter [TeamB]
Consultant Software Engineer 
Technical Details
Message-ID: <458c6e01@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <458c5f4b@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,454cd7256a34fa43
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII
From: "Joanna Carter [TeamB]" <joanna@not.for.spam>
Newsgroups: borland.public.csharpbuilder.language.csharp
References: <458c5f4b@newsgroups.borland.com>
Subject: Re: Replacement for "class of" in C#?
Date: Fri, 22 Dec 2006 23:44:47 -0000
Lines: 78
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 82.69.239.102
Message-ID: <458c6e01@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1166831105 82.69.239.102 (22 Dec 2006 15:45:05 -0800)
X-Authenticated-User: jcarter
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail