Page 1 of 1

How to Dynamically obtain the Constructors of a Class ?

Posted: Wed Aug 03, 2011 2:47 pm
by PGilbert
According to this Article it is possible to obtain the constructors of a class at run-time using reflection. From my understanding this should work:

Code: Select all

      ⎕USING←,⊂'System'
      DT←⎕NEW DateTime (2000 1 1)

      DT.GetType.GetConstructors
 #.[System.RuntimeType] . ∇GetConstructors


I should get a 'ConstructorInfo' structure but instead I am getting the message: #.[System.RuntimeType] . ∇GetConstructors

What is the meaning of this message ? What is the correct procedure with reflection ? Is there is a way to get that directly from APL using a quad ?

Thanks,

Pierre Gilbert

Re: How to Dynamically obtain the Constructors of a Class ?

Posted: Wed Aug 03, 2011 4:54 pm
by RossHale
Hi Pierre,

DT.GetType.GetConstructors is niladic, in C#
DT.GetType.GetConstructors()
and in Dyalog APL with a nil ⍬ argument
DT.GetType.GetConstructors⍬

Just from imperfect memory, sometimes we need an explicit ⍬ to invoke niladic C# functions from APL
and sometimes we don't. Maybe when the function is a Property or there is not an overload,
the ⍬ is not needed -- like DateTime.Now

Code: Select all


        DT.GetType.GetConstructors⍬
 Void .ctor(Int64)  Void .ctor(Int64, System.DateTimeKind)  Void .ctor(Int32, Int32, Int32)  Void .ctor(Int32, Int32, Int32, System.Globalization.Calendar)  Void .ctor(
      Int32, Int32, Int32, Int32, Int32, Int32)  Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, System.DateTimeKind)  Void .ctor(Int32, Int32, Int32, Int32, Int32
      , Int32, System.Globalization.Calendar)  Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32)  Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32,
      System.DateTimeKind)  Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar)  Void .ctor(Int32, Int32, Int32, Int32, Int32, In
      t32, Int32, System.Globalization.Calendar, System.DateTimeKind)


       ⍴ DT.GetType.GetConstructors⍬
11

    11 1  ⍴ DT.GetType.GetConstructors⍬
 Void .ctor(Int64)                                                                                               
 Void .ctor(Int64, System.DateTimeKind)                                                                         
 Void .ctor(Int32, Int32, Int32)                                                                                 
 Void .ctor(Int32, Int32, Int32, System.Globalization.Calendar)                                                 
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32)                                                           
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, System.DateTimeKind)                                       
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar)                             
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32)                                                     
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.DateTimeKind)                               
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar)                     
 Void .ctor(Int32, Int32, Int32, Int32, Int32, Int32, Int32, System.Globalization.Calendar, System.DateTimeKind)


The InvokeOverload function posts and Generic Type construction may help some.

Ross

Re: How to Dynamically obtain the Constructors of a Class ?

Posted: Thu Aug 04, 2011 12:43 am
by RossHale
Note that one apparently needs to construct an instance of a class first in order to use Reflection
to dynamically find the constructors or methods or properties. Once you have an instance of a class,
you can use Reflection flags to discriminate public, instance or static methods, et cetera.

Given a class capable of constructing instances, this is all fine and good.

But how can I dynamically determine nicely the methods of a "static class"?

Static classes are widely used to implement "extension methods" in the .Net arena.
The C# compiler will search out an appropriate static class when it handles a class instance
with an undefined method. Given a static class, DateExtensions, defining static methods
such as Yesterday and Tomorrow each with a first argument of type System.DateType
then in C#, dt.Yesterday would be a valid expression.

Static extension classes are frequently available in dll's,
so it is no problem to reference by ⎕using.

There are tools like .Net Reflector or Telerik's JustDecompile
that produce readable (C# code) from dll's so this can obviously be done.

How to dynamically determine nicely the methods of a "static class"?