[DesignerSerializer(typeof(CustomCodeDomSerializer), typeof(CodeDomSerializer))]
public class BaseUserControl : UserControl
{
}
public class CustomCodeDomSerializer : CodeDomSerializer
{
public override object Serialize(IDesignerSerializationManager manager, object value)
{
var baseClassSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(BaseUserControl).BaseType, typeof(CodeDomSerializer));
var codeObject = baseClassSerializer.Serialize(manager, value);
if (codeObject is CodeStatementCollection collection)
{
for (var i = 0; i < collection.Count; i++)
{
var st = collection[i] as CodeVariableDeclarationStatement;
if (st?.Type.BaseType == typeof(ComponentResourceManager).FullName)
{
var ctr = new CodeTypeReference(typeof(MyCustomComponentResourceManager));
st.Type = ctr;
st.InitExpression = new CodeObjectCreateExpression(ctr, new CodeTypeOfExpression(manager.GetName(value)));
}
}
}
return codeObject;
}
/// <inheritdoc />
public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
{
CodeDomSerializer baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(BaseUserControl).BaseType, typeof(CodeDomSerializer));
return baseSerializer.Deserialize(manager, codeObject);
}
}
public class MyCustomComponentResourceManager : ComponentResourceManager
{
public MyCustomComponentResourceManager() : base()
{
}
public MyCustomComponentResourceManager(Type type) : base(type)
{
}
}
You can swap the ComponentResourceManager with your own one. This way, you can get your images from the database at runtime. The designer will still generate the .resx files with the images and everything else inside it, but it's not a problem since, at design time, you still want to show some default images and whatnot. If you don't like the .resx files, I think you can define your own implementation with the IResourceService and save your stuff to a database instantly, but I didn't test it. So I might be wrong. I had a similar problem, but with localization. If you are interested, you can read my answer Here. Note that the form designer only works with the BASE class. You need to inherit the BaseUserControl to make the custom CodeDomSerializer work. The BaseUserControl still uses the "old" CodeDomSerializer. Last I recommend that you read this answer about how the Designer works Here