Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Reflection : GetConstructors() problem.

Back to Forum: Tech Off
  • User profile image
    phreaks

    I am using reflection to load an instance of a class via a factory.

    The issue I am facing is that some of the classes that I want an instance of have constructors and some do not.

    I figured I could use GetConstructors() to easily call CreateInstance () with the appropriatte args[].

    Unfortunately for me, it seems that GetConstructors isn't returning my ctor params.

    Any Ideas?

    String nspace = DEFAULT_RUNNER_NAMESPACE;
    System.Reflection.
    Assembly asm;

    string className = nspace + "." + name;

    asm = Assembly.LoadFrom(myExecutableFolderPath + "\\" + LIBRARY_ASSEMBLY_NAME);

    Type t = asm.GetType(className, true, true);

    ConstructorInfo[] ctxInfo = t.GetConstructors(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

    if (ctxInfo.Length > 0)
    {
    Console.WriteLine("Name={0} Type={1}", ctxInfo[0].Name, ctxInfo[0].GetType().ToString());

    }


    t returns the correct type, but t.GetConstructors() does not return any of the constructors, wtf?

    Could it be that t is a type of a concrete class, whose base class has no constructors?

    If so, what can I do to inspect the constructor args?

  • User profile image
    Human​Compiler

    What you're trying to do "should" work.  I didn't try to run the code myself to figure it out though.  Can you post what the class looks like?  Is it abstract?

  • User profile image
    phreaks

    HumanCompiler wrote:
    What you're trying to do "should" work.  I didn't try to run the code myself to figure it out though.  Can you post what the class looks like?  Is it abstract?


    No, the concrete class is not abstract, but the derived base class is.

    Here is the verbose class signature of one of the classes whose contructors I am trying to inspect.

    I have looked at 25 differnt samples and read all the documentation, I don't understand what I am doing wrong.

    public class DisparityCalcRunner : BaseRunner
    {

       private DS.DisparityCalc ds;
       private String _beginDate;
       private String _endDate;

       public DisparityCalcRunner(string beginDate, string endDate)
          : base()
       {
          this._beginDate = beginDate;
          this._endDate = endDate;
       }

       public override void Run()
       {
          dc.Retrieve.GetDisparityCalcDataCommand   myCommand = new dc.Retrieve.GetDisparityCalcDataCommand(_beginDate, _endDate);

          myCommand.Execute();

          ds = (DS.DisparityCalc)myCommand.Results;

          Reports.DisparityCalcReport rpt = new Reports.DisparityCalcReport(ds, _beginDate, _endDate);

          rpt.ShowPreview();

          this._report = rpt; //abstract

       }

    }

  • User profile image
    Rossj

    phreaks wrote:

    I figured I could use GetConstructors() to easily call CreateInstance () with the appropriatte args[].

    ConstructorInfo[] ctxInfo = t.GetConstructors(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);



    The docs for the GetContructors methods mentions the following:

    You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

    Highlighting is mine.  Try removing the BindingFlags.Static as you can't do much with that contructor anyway..

  • User profile image
    phreaks

    Rossj wrote:
    
    phreaks wrote:
    I figured I could use GetConstructors() to easily call CreateInstance () with the appropriatte args[].

    ConstructorInfo[] ctxInfo = t.GetConstructors(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);



    The docs for the GetContructors methods mentions the following:

    You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

    Highlighting is mine.  Try removing the BindingFlags.Static as you can't do much with that contructor anyway..


    You are the man.

    /Rossj is my hero of the week

  • User profile image
    Rossj

    Shucks Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.