Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

C# - It's never to late, for late binding

I' am currently learning C# and just asked me
how to call an Method dynamicly.



In Flash-Actionscript it is so simple, because we
are dealing with an scripting-language that does
not have bind all method-calls at runtime, so we
can simply write
this.myMethod = function () { trace("Dynamicly called, yes!") };
this["myMethod"]();

Realy cool and easy.
In C# we have to use some Methods from the
System- and System.Reflection-Namespace for
doing that.
using System;
using System.Reflection;
namespace TestReflectionDynamicMethodCall
{
   class TestClass
   {
      public TestClass ()
      {
        String[] asArgs = {"eins", "zwei"};
        Type myType = this.GetType();
        MethodInfo myMethodInfo = myType.GetMethod("TestMethod"); //<--
        myMethodInfo.Invoke(this, asArgs);
      }
      public void TestMethod (string eins, string zwei)
      {
        Console.WriteLine("TestMethod wurd dynamisch aufgerufen " + eins + " " + zwei);
      }
   }
   /// <summary>
   /// Zusammenfassung für Class1.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// Der Haupteinstiegspunkt für die Anwendung.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
        TestClass testObj = new TestClass();
        Console.ReadLine();
      }
   }
}

Yes, it works we only have a little bit more typing.
In mostly cases it is not an good practise to do something
like that because you can't see comfortable who is
calling who...;-)
(what a sentence)

Posted by hOk at August 20, 2003 07:37 PM

Comments (8)

Hi Holger,
Thank you for the article. System.Reflection and System.CodeDom are very interesting namespaces when it comes to dynamic code execution, compilation, code generation. It is even possible to invent your own scripting language.
We will see if ActionScript follows JScript .NET and to what extent it will affect the language. Will it be a rigid static execution model, a hybrid model, or we will have compiler switches?

Posted by Ahmet Zorlu at August 21, 2003 07:31 AM

Uh, ah, how ugly. Wouldn't it be nice to have the best of both worlds.

Posted by bokel at August 21, 2003 10:01 AM

@bokel
Hehe, yes this would be nice
but if you take a look at the
types: "Type, MethodInfo etc."
you will like it in same way as
me and Ahmed.
@Ahmed:
Thanks for telling something
of the power of the Namespaces.
I must realy say, that the .net-
framework is pretty cool. But also
i have to say that i did not know
Java, so maybe it has a compareable
power for developpers.

regards, Holger

Posted by hOk at August 21, 2003 03:05 PM

thx for posting this little trick, Holger.
I also fell in love with .NET recently. I don´t think it´s ugly, it´s very consistent and logical. btw. if you want the best of both worlds, take JScript.NET and compile without fast directive. It supports early *and* late binding, pretty cool.

hm... Holger, I think you should work a bit on improving your english :D

Posted by Florian Krüsch at August 22, 2003 03:37 PM

:-)
hm... you're right but you
havent read my chinese:D,
did any body know a good howto
for english grammer...;-)

Posted by hOk at August 22, 2003 04:06 PM

Grammar no problem is the best I know so far.

explore delegates, if you hadn't already... :)

Posted by marco at September 1, 2003 10:41 AM

Nice, to here from you, and thanks for
the good book-tip, regards, Holger

Posted by hOk at September 9, 2003 06:52 PM

For some reason I coulnd't get this to compile. Maybe I missing some libraries.

Posted by Online Coupons at November 1, 2003 10:20 PM


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place