1

如果在引用的 dll 中使用 Application.StartupPath,则路径指向 IDE 的路径。

反正有没有得到实际应用程序的路径?

需要明确的是,这是在设计时。

ETA:我在下面发布了解决方案:

ETA2:

因为它是相关的,所以我想我会发布另一个有用的设计时服务的片段。与下面的解决方案一样,此示例适用于 UITypeEditor:

Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

    Dim typeDiscovery As ITypeDiscoveryService = TryCast(provider.GetService(GetType(ITypeDiscoveryService)), ITypeDiscoveryService)
    Dim types As ICollection = typeDiscovery.GetTypes(GetType(MyType), False)

End Function

types 将包含从 MyType 派生的所有类型。将第二个参数更改为 True 以排除搜索 GAC。将 Nothing 作为第一个参数传递以获取所有类型的列表。

4

3 回答 3

0

这是从 UITypeEditor 执行此操作的方法。

ETA:原始代码有一个额外的不需要的步骤。我忘了我们有服务提供商,所以不需要看网站。精简后的代码是:

Public Class MyEditor
    Inherits UITypeEditor

    Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

        Dim typeRes As ITypeResolutionService = TryCast(provider.GetService(GetType(ITypeResolutionService)), ITypeResolutionService)
        Dim ass As System.Reflection.AssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName()
        MessageBox.Show(ass.CodeBase, "Design-time Path")
        MessageBox.Show(typeRes.GetPathOfAssembly(ass), "Run-time Path")

    End Function

End Class

原始代码:

Public Class MyEditor
    Inherits UITypeEditor

    Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object

        Dim component As IComponent = TryCast(context.Instance, IComponent)
        Dim site As ISite = component.Site
        Dim typeRes As ITypeResolutionService = TryCast(site.GetService(GetType(ITypeResolutionService)), ITypeResolutionService)
        Dim ass As System.Reflection.AssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName()
        MessageBox.Show(ass.CodeBase, "Design-time Path")
        MessageBox.Show(typeRes.GetPathOfAssembly(ass), "Run-time Path")

    End Function

End Class

此解决方案基于How to: Access Design-Time Services中的代码,您可以在其中找到大量信息。

于 2010-01-24T12:28:29.700 回答
-2

你不能在设计时做到这一点!在运行时,您可以,也就是说,当您构建应用程序并运行它时,或者在 VS 中按 F5 或单击绿色箭头。为什么你想在设计时知道?这无关紧要,因为可执行文件及其相关的 DLL 并未真正加载到内存中并执行,此外,如果对代码进行了更改,则必须再次重建整个项目。

希望这会有所帮助,最好的问候,汤姆。

于 2010-01-24T02:57:15.083 回答