How WinForms PropertyGrid finds custom type converters in external libraries?

核能气质少年 提交于 2019-12-25 01:13:44

问题


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
}

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?


回答1:


You can use either of the following options:

  • 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; }
    
  • 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.



来源:https://stackoverflow.com/questions/56189207/how-winforms-propertygrid-finds-custom-type-converters-in-external-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!