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?
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.