locked
How to access method inside user control using Activator.CreateInstance(myType) RRS feed

  • Question

  • i am using runtime usercontrol access, using below code,

    i want to access public method which declare inside user contorl?

    Assembly a = Assembly.Load(Assembly.GetEntryAssembly().ToString());
                  string ucControl = ((CustomRibbon)(e.Command)).UserControlName;
                  string ucTitle = ((CustomRibbon)(e.Command)).UserControlTitle;
                  string ucContentName = ((CustomRibbon)(e.Command)).UserControlContentName; // this is contentName
                  bool bucRefresh = ((CustomRibbon)(e.Command)).RefreshScreen;
                  string ucScreens = ((CustomRibbon)(e.Command)).AssociatedScreen;
                  Type myType = a.GetType(ucControl);
                  // Create an instance.
                  Control objControl = Activator.CreateInstance(myType) as Control;


    Ashok

    Wednesday, July 18, 2012 9:37 PM

Answers

  • Type myType = a.GetType(ucControl);
    
    // Create an instance.
    Control objControl = Activator.CreateInstance(myType) as Control;
    if (objControl != null)
    {
        var method = myType.GetMethod("Refresh");
        method.Invoke(objControl, null); // Call the Refresh() method
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Reed Copsey, JrMVP Wednesday, July 18, 2012 11:14 PM
    • Marked as answer by Sheldon _Xiao Thursday, July 19, 2012 5:32 AM
    • Unmarked as answer by Jumpingboy Thursday, July 19, 2012 9:01 PM
    • Marked as answer by Jumpingboy Thursday, July 19, 2012 9:02 PM
    • Unmarked as answer by Jumpingboy Thursday, July 19, 2012 9:02 PM
    • Marked as answer by Jumpingboy Thursday, July 19, 2012 9:02 PM
    Wednesday, July 18, 2012 10:53 PM

All replies

  • You can use Type.GetMethod to get the method, and then call .Invoke on the MethodInfo you retrieve to call it.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Reed Copsey, JrMVP Wednesday, July 18, 2012 11:14 PM
    • Unproposed as answer by Jumpingboy Thursday, July 19, 2012 9:01 PM
    Wednesday, July 18, 2012 9:39 PM
  • Thanks,

    can you snip code by taking my example, suppose we have refresh method inside control.

    Thanks


    Ashok

    Wednesday, July 18, 2012 10:14 PM
  • Type myType = a.GetType(ucControl);
    
    // Create an instance.
    Control objControl = Activator.CreateInstance(myType) as Control;
    if (objControl != null)
    {
        var method = myType.GetMethod("Refresh");
        method.Invoke(objControl, null); // Call the Refresh() method
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Reed Copsey, JrMVP Wednesday, July 18, 2012 11:14 PM
    • Marked as answer by Sheldon _Xiao Thursday, July 19, 2012 5:32 AM
    • Unmarked as answer by Jumpingboy Thursday, July 19, 2012 9:01 PM
    • Marked as answer by Jumpingboy Thursday, July 19, 2012 9:02 PM
    • Unmarked as answer by Jumpingboy Thursday, July 19, 2012 9:02 PM
    • Marked as answer by Jumpingboy Thursday, July 19, 2012 9:02 PM
    Wednesday, July 18, 2012 10:53 PM
  • Cool ,

    Thanks Reed.


    Ashok

    Wednesday, July 18, 2012 10:55 PM