WinForms PropertyGrid如何在外部库中找到自定义类型转换器? [英] How WinForms PropertyGrid finds custom type converters in external libraries?

查看:188
本文介绍了WinForms PropertyGrid如何在外部库中找到自定义类型转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个自定义控件.该控件的某些属性使用自定义类型转换器和UI类型编辑器,它们在单独的设计时DLL中实现.这些类型转换器使用 TypeConverter的字符串语法定义. Editor 属性,例如:

We have a custom control. Some properties of this control use custom type converters and UI type editors, and they are implemented in a separate design-time DLL. These type converters are defined using the string syntax of the TypeConverter and Editor attributes, for example:

[TypeConverter("<full class name>, <library name>, Version=<version>")]
public CustomType Property1
{
    // property implementation
}

当我们在已编译应用程序的WinForms包中的标准PropertyGrid控件中显示自定义控件的属性时,只有将我们的DLL放置在包含应用程序exe的文件夹.由于某些原因,我们不想在此文件夹中复制设计时DLL.还有其他方法可以告诉PropertyGrid控件在哪里可以找到以这种方式引用的设计时DLL吗?

When we display the properties of our custom control in the standard PropertyGrid control from the WinForms package in a compiled app, the corresponding type converters and UI type editors from our design-time DLL are found only when we place this DLL in the folder containing the application exe. We do not want to duplicate the design-time DLL in this folder for some reasons. Is there any other way to tell the PropertyGrid control where it can find the design-time DLL referenced this way?

推荐答案

您可以使用以下任一选项:

You can use either of the following options:

  • 在GAC中安装程序集,并像这样装饰属性(使用程序集的完全限定名称).正如汉斯在评论中已经提到的那样,我也认为这是最体面的方式:

  • Install the assembly in GAC and decorate the property like this (use your assembly fully qualified name). As already mentioned in the comments by Hans, I also believe it's the most decent way:

[TypeConverter("MyAssembly.MyClassTypeConverter, MyAssembly, Version=1.0.0.0," +
    " Culture=neutral, PublicKeyToken=8ac69aab03bb290e")]
public MyClass MyClass { get; set; }

  • 将程序集复制到您的应用程序文件夹并像这样装饰属性.

  • Copy the assemblies to your application folder and decorate the property like this.

    [TypeConverter("MyAssembly.MyClassTypeConverter, MyAssembly")]
    public MyClass MyClass { get; set; }
    

  • 如果知道程序集的位置,则可以处理

  • In case of having a known location for assemblies, you can handle AppDomain.AssemblyResolve event and load the assembly. For example, assuming there is an assemblies folder under your application folder which contains the assembly, you can add the following code to your main method before Application.Run:

    AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
        Assembly.LoadFrom(Path.Combine(Application.StartupPath,
            "assemblies", $"{e.Name}.dll"));
    

    我假设您具有属性声明:

    I assume you have the property declaration:

    [TypeConverter("MyAssembly.MyClassTypeConverter, MyAssembly")]
    public MyClass MyClass { get; set; }
    

  • 正如TnTinMn的评论中已经提到的那样,如果具有程序集的已知位置,则还可以通过使用 codebase 标记.例如,假设您的应用程序文件夹下有一个assemblies文件夹,其中包含程序集:

  • As already mentioned in the comments by TnTinMn, In case of having a known location for assemblies, you can also load assemblies without writing code, by registering the known folder in your app.config using probing tag or codebase tag. For example, assuming there is an assemblies folder under your application folder which contains the assembly:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="assemblies"/>
      </assemblyBinding>
    </runtime>
    

    我假设您具有属性声明:

    I assume you have the property declaration:

    [TypeConverter("MyAssembly.MyClassTypeConverter, MyAssembly")]
    public MyClass MyClass { get; set; }
    

  • 注意:以上所有选项均不需要向包含类型转换器的程序集添加引用.

    Note: None of the above options need adding a reference to the assembly which contains the type converter.

    这篇关于WinForms PropertyGrid如何在外部库中找到自定义类型转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆