Thread View: borland.public.csharpbuilder.language.csharp
9 messages
9 total messages
Started by Borland
Tue, 19 Jun 2007 04:13
More about "class of" in C#
Author: Borland
Date: Tue, 19 Jun 2007 04:13
Date: Tue, 19 Jun 2007 04:13
9 lines
1441 bytes
1441 bytes
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;
Re: More about "class of" in C#
Author: Joanna Carter [TeamB]
Date: Tue, 19 Jun 2007 10:57
Date: Tue, 19 Jun 2007 10:57
134 lines
4394 bytes
4394 bytes
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
Re: More about "class of" in C#
Author: 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
Re: More about "class of" in C#
Author: Borland
Date: Tue, 19 Jun 2007 20:31
Date: Tue, 19 Jun 2007 20:31
3 lines
1130 bytes
1130 bytes
Obviously, that didn't work - I'm having as much trouble with OE as C#. If this doesn't work, I'll ask for expert assistance.
Re: More about "class of" in C#
Author: David Fallas
Date: Tue, 19 Jun 2007 20:32
Date: Tue, 19 Jun 2007 20:32
1 lines
994 bytes
994 bytes
Right - got it now!
Re: More about "class of" in C#
Author: Joanna Carter [TeamB]
Date: Tue, 19 Jun 2007 20:38
Date: Tue, 19 Jun 2007 20:38
11 lines
1236 bytes
1236 bytes
David Fallas a �crit : > Right - got it now! Yaayy!!! Joanna -- Joanna Carter [TeamB] Consultant Software Engineer
Re: More about "class of" in C#
Author: Joanna Carter [TeamB]
Date: Tue, 19 Jun 2007 20:40
Date: Tue, 19 Jun 2007 20:40
14 lines
1562 bytes
1562 bytes
Borland a �crit : > 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. The great thing about this group is that you don't just get the C#, you get the Delphi experience as to how to do the conversion :-) Joanna -- Joanna Carter [TeamB] Consultant Software Engineer
Re: More about "class of" in C#
Author: David Fallas
Date: Fri, 22 Jun 2007 01:02
Date: Fri, 22 Jun 2007 01:02
152 lines
5252 bytes
5252 bytes
For the sake of completeness for anyone referring to this thread in future -
there is a little bit more to the story if using the first strategy
suggested.
Where there is a requirement to call a static method which has NOT been
overridden in a descendent class, the type.GetMethod() method will not find
it (where type is the descendent's type).
Instead, if it isn't known if the method has been overridden or not, one
needs to check the return from the type.GetMethod("xxxx") call and if null,
call type.BaseType.GetMethod("xxxx") to find the base class' implementation
of the method.
This appears to differ significantly from Delphi which seems to be capable
of finding either a base or overridden method quite happily.
"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
Re: More about "class of" in C#
Author: Joanna Carter [TeamB]
Date: Fri, 22 Jun 2007 06:46
Date: Fri, 22 Jun 2007 06:46
27 lines
2241 bytes
2241 bytes
David Fallas a �crit :
> For the sake of completeness for anyone referring to this thread in future -
> there is a little bit more to the story if using the first strategy
> suggested.
>
> Where there is a requirement to call a static method which has NOT been
> overridden in a descendent class, the type.GetMethod() method will not find
> it (where type is the descendent's type).
>
> Instead, if it isn't known if the method has been overridden or not, one
> needs to check the return from the type.GetMethod("xxxx") call and if null,
> call type.BaseType.GetMethod("xxxx") to find the base class' implementation
> of the method.
Thanks for that. I only omitted that due to lack of time.
> This appears to differ significantly from Delphi which seems to be
> capable of finding either a base or overridden method quite happily.
Delphi can find derived static methods because they are declared
virtual; something that cannot be done in C#.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads