Article View: borland.public.csharpbuilder.language.csharp
Article #138Re: More about "class of" in C#
From: Borland
Date: Tue, 19 Jun 2007 20:22
Date: Tue, 19 Jun 2007 20:22
150 lines
5079 bytes
5079 bytes
My apologies to the group, (and Borland) - I didn't even notice that Outlook
Express had re-christened me. I think I have now changed my identity
(File|Identity), but won't know for sure until I post this reply.
In the meantime, many thanks for your comprehensive reply - it was more than
I dared hope for.
I'm a beginner when it comes to C#, and am struggling a bit. I am perhaps
biased - but Delphi does have a certain elegance when it comes to this sort
of stuff that is still hard to beat.
Cheers,
David.
"Joanna Carter [TeamB]" <joanna@nospam.for.me> wrote in message
news:4677b67f@newsgroups.borland.com...
> Borland a οΏ½crit :
>
>> Referring to that posting from December last year re "class of"... I
>> understand the use of Activator.CreateInstance(ClassType) as a
>> replacement for the Delphi "class of" for use in a C# abstract class
>> factory, but how would I use that ClassType reference to call a static
>> method (eg public static DoIt()) which has been declared in the Class
>> class?
>>
>> In Delphi I would simply retrieve the "class of" reference, (eg
>> ClassClass - class of TClass), and call the function normally - eg
>> TClass(ClassClass).DoIt;
>
> First, can I ask you to change your posting name from Borland ?
>
> Now, to the matter in hand.
>
> Don't forget that, in C#, static methods cannot be virtual. Therefore, you
> have to redlclare the method in each and every derived class but simply
> calling the static method on the base type will only call that method, not
> the derived one.
>
> However, here is a simple example fo how to create a factory class that
> will call the correct method, depending on the type passed in.
>
> public class Base
> {
> public static void DoIt(int arg) { }
> }
>
> public class Derived : Base
> {
> public static void DoIt(int arg) { }
> }
>
> public static class Factory
> {
> public static void DoIt(Type type, int arg)
> {
> if (!typeof(Base).IsAssignableFrom(type))
> throw new Exception("Type must derive from Base");
>
> Type[] argTypes = new Type[] { typeof(int) };
>
> MethodInfo mi = type.GetMethod("DoIt", argTypes);
>
> object[] args = new object[] { arg };
>
> mi.Invoke(null, args);
> }
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> Type type = typeof(Derived);
>
> Factory.DoIt(type, 123);
> }
>
> You can also use the idea of a metaclass to achieve a more Delphi-like
> syntax.
>
> public class Base
> {
> #region metaclass
>
> public class ClassReference
> {
> #region private members
>
> private Type internalType;
>
> #endregion
>
> public void DoIt(int value)
> {
> MethodInfo methodInfo = internalType.GetMethod("DoIt",
> BindingFlags.Public | BindingFlags.Static);
>
> object[] args = new object[] { value };
>
> methodInfo.Invoke(null, args);
> }
>
> #endregion
>
> #region constructors
>
> // default constructor
> private ClassReference(Type type)
> {
> internalType = type;
> }
>
> #endregion
>
> #region implicit conversion operator
>
> public static implicit operator ClassReference(Type input)
> {
> if (!typeof(Shape).IsAssignableFrom(input))
> throw new ArgumentException(String.Format("Type {0} must derive
> from {1}", input.Name, typeof(Base).Name), "input");
>
> return new ClassReference(input);
> }
>
> #endregion
> }
>
> #endregion
>
> public static void DoIt(int value) {...}
> }
>
> public class Derived : Base
> {
> public static void DoIt() {...}
> }
>
> This then allows the following calling syntax :
>
> {
> Base.ClassReference classRef = typeof(Derived);
>
> classRef.DoIt(123);
> }
>
> Is that what you are looking for ?
>
> Joanna
>
> --
> Joanna Carter [TeamB]
> Consultant Software Engineer
Technical Details
Message-ID:
<46783afa$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<467757d6$1@newsgroups.borland.com> <4677b67f@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,77a80b77cf3a9a3e X-Google-Attributes: gid1efe35,public,usenet X-Google-Language: ENGLISH,ASCII From: "Borland" <fallasd@sysmex.co.nz> Newsgroups: borland.public.csharpbuilder.language.csharp References: <467757d6$1@newsgroups.borland.com> <4677b67f@newsgroups.borland.com> Subject: Re: More about "class of" in C# Date: Wed, 20 Jun 2007 08:22:15 +1200 Lines: 152 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 NNTP-Posting-Host: 121.72.17.114 Message-ID: <46783afa$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1182284538 121.72.17.114 (19 Jun 2007 13:22:18 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail