我有一个从UserControl派生的用户控件类。
[ComVisible(true)]
public class MyUserControl : UserControl它包含我在另一个类中名为Initialize():public void Initialize()的方法,我需要使用MyUserControl,但是我想声明和使用泛型UserControl对象。这样,即使使用一个新的和不同的用户控件(比如MyUserControl2),我也可以重用这个类。
所以我宣布这个班的一名成员
private static UserControl _userControl;这里是构造函数
public CTPManager(UserControl userControl, string Title, MsoCTPDockPosition Position)
{
//stuff
_userControl = userControl;
_title = Title;
_position = Position;
}第一个问题:以后是否可以用以下方法实例化该类:
MyUserControl newControl = new MyUserControl();
CTPManager newCTP = new CTPManager(newControl, "window title", etc.);如果是这样的话,我是否可以调用MyUserControl newControl的Initialize()方法,因为我只需要在CTPManager类中执行这两个调用:
CustomTaskPaneFactory.CreateCustomTaskPane(typeof(UserControl), _title, _EXCELApp.ActiveWindow)); //-> this one will be ok because of CreateCustomTaskPane signature
_userControl.Initialize //-> that is what I would like to be able to do !非常感谢您的回答或建议。
发布于 2016-01-20 19:39:44
您可以使用MethodInfo:
//Get the method information using the method info class
MethodInfo mi = _userControl.GetType().GetMethod("Initialize");
//Invoke the method
mi.Invoke(_userControl, null);发布于 2016-01-20 19:44:04
使用Initialize()方法创建一个接口。实现接口。将控件保存在CTPManager中作为该接口。或者,如果要将其存储为UserControl,则可以将其转换为所需的类型:
var init = (InitializerInterface)_userControl;
if (init != null) ...https://stackoverflow.com/questions/34908875
复制相似问题