C#

InitializeComponent for multiple assembly versions (WPF)

In the code behind of WPF controls the constructor loads the corresponding XAML1 file (code). The code for loading the XAML is automatically generated2 when calling InitializeComponent(), which one can find in virtually every control’s constructor. Unfortunately it might not play well, break, when there are multiple versions loaded of the same assembly containing the control (in the same AppDomain). The solution is simple but, alas, not well documented. ...

Using ResourceManger with a CustomAssembly

ResourceMangers provide convenient access to culture-specific resources at run time. Resources may reside in the current or external assemblies. A ResourceManager can be created by specifying the base name of the resource file and assembly where it resides. However one constraint it not documented, the provided assembly must be of the type “ RuntimeAssembly” (not public). While most of the time you will, without knowing, be dealing with RuntimeAssemblies it is possible to end up with System.Reflection.Context.Custom.CustomAssembly instead which is not a RuntimeAssembly. The trouble begins… ...

Workaround for string representation of types in attribute constructor parameters (CS0182)

Attributes are a powerful way in C# of adding meta-data to your code which can be queried at runtime through reflection (or statically from the binary). Unfortunately they are constrained by which constructor parameter types they can use. Only constant expressions, most primitives, types, public enums or single dimensional array of the previous types are allowed. For more information see the official documentation. My use-case was to pass a string containing type information to an attribute and while those strings can be constructed manually, for maintainability reasons, i wanted to use typeof(MyType) this is where the trouble started… ...

© Nelis Oostens