简体   繁体   中英

WPF call a method that takes parameters from another dll

How can I run a method that takes parameters from another dll? I import a UserControl from another dll as below but I now either need to call a method within that UserContol or have the ability to set a variable that's contained in that class.

Load UserControl

UserControl ucSupportButton = 
new Bootstrapper().LoadUserControl("SC.Support.dll", "Button");

Code used in Bootstrapper

        public UserControl LoadUserControl(string dllName, string loadType)
    {
        if (File.Exists(Path.Combine(applicationRoot, dllName)))
        {
            Assembly asm = Assembly.LoadFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), dllName));
            Type[] types = asm.GetTypes();

            Type type = types.Where(t => t.Name.Equals(loadType)).FirstOrDefault();

            if (type != null)
            {
                return Activator.CreateInstance(type) as UserControl;
            }
        }
        return null;
    }

@HighCore comment seems like the best way to go. Depending on your design, reflection is another option. You can use reflection to get a method or field in that type and then call or set it.

var method = paymentObjectInstance.GetType().GetMethod("MethodNameHere",
                  BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

 if (method == null)
 {
    return null;
 }

 var result = method.Invoke(paymentObjectInstance, null);

Here's a little overview of reflection courtesy of MSDN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM