安装程序.Install(dictionarystatesaver):期望的字典类型

本文关键字:期望 字典 类型 程序 Install dictionarystatesaver 安装 | 更新日期: 2023-09-27 18:05:33

我正在尝试呼叫安装程序。手动安装:

ProjectInstaller installer = new ProjectInstaller();
installer.Install(new Dictionary<int, int>());

问题:

System.ArgumentException was unhandled.
The value "_reserved_lastInstallerAttempted" is not of type "System.Int32"
and cannot be used in this generic collection.
       at System.ThrowHelper.ThrowWrongKeyTypeArgumentException(Object key, Type targetType)
       at System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.Add(Object key, Object value)
       at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
       at CSShellExtContextMenuHandler.ProjectInstaller.Install(IDictionary stateSaver) in C:'Users'win7pro32bit'Documents'lab'CSShellExtContextMenuHandler'ProjectInstaller.cs:line 40
       at Starter.Program.Main(String[] args) in C:'Users'win7pro32bit'Documents'lab'Starter'Program.cs:line 14

作为参数,我尝试了new Dictionary<int, int>, new Dictionary<string, string>和其他,但没有一个工作。文档没有帮助。期望是什么?

安装程序.Install(dictionarystatesaver):期望的字典类型

通过.NET Reflector运行System.Configuration.Install.Installer类会显示Install方法的以下内部实现:

public virtual void Install(IDictionary stateSaver)
{
    if (stateSaver == null)
    {
        throw new ArgumentException(Res.GetString("InstallNullParameter", new object[] { "stateSaver" }));
    }
    try
    {
        this.OnBeforeInstall(stateSaver);
    }
    catch (Exception exception)
    {
        this.WriteEventHandlerError(Res.GetString("InstallSeverityError"), "OnBeforeInstall", exception);
        throw new InvalidOperationException(Res.GetString("InstallEventException", new object[] { "OnBeforeInstall", base.GetType().FullName }), exception);
    }
    int num = -1;
    ArrayList list = new ArrayList();
    try
    {
        for (int i = 0; i < this.Installers.Count; i++)
        {
            this.Installers[i].Context = this.Context;
        }
        for (int j = 0; j < this.Installers.Count; j++)
        {
            Installer installer = this.Installers[j];
            IDictionary dictionary = new Hashtable();
            try
            {
                num = j;
                installer.Install(dictionary);
            }
            finally
            {
                list.Add(dictionary);
            }
        }
    }
    finally
    {
        stateSaver.Add("_reserved_lastInstallerAttempted", num);
        stateSaver.Add("_reserved_nestedSavedStates", list.ToArray(typeof(IDictionary)));
    }
    try
    {
        this.OnAfterInstall(stateSaver);
    }
    catch (Exception exception2)
    {
        this.WriteEventHandlerError(Res.GetString("InstallSeverityError"), "OnAfterInstall", exception2);
        throw new InvalidOperationException(Res.GetString("InstallEventException", new object[] { "OnAfterInstall", base.GetType().FullName }), exception2);
    }
}

stateSaver参数似乎被传递了一个字符串,int KeyValuePair,和一个字符串,IDicitionary[] KeyValuePair内部:

stateSaver.Add("_reserved_lastInstallerAttempted", num);
stateSaver.Add("_reserved_nestedSavedStates", list.ToArray(typeof(IDictionary)));

我不太确定它是如何做到的。可能因为它们初始化了传递给基础的字典。将方法安装为哈希表?

IDictionary dictionary = new Hashtable();