开发者

How do I serialize a .NET control to CODE?

开发者 https://www.devze.com 2022-12-19 12:55 出处:网络
I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to

I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to a C# solution and compiled).

What I want to do is very similar to what the WinForm designer does. But instead of having a drag/drop interface for the user, I want to dynamically build the Form/Controls myself at runt开发者_C百科ime.

I was thinking I could just reuse what the WinForm designer is doing.

Is that possible?


This MSDN magazine article should have everything you need.


It's really not as simple as it was pre-.NET as the visual version of the form you see in Visual Studio is actually the result of multiple files.

But in the simplest form you could simply just mirror what .NET does at the start of creating a new form:

  1. Create three files Form.cs, Form.Designer.cs and Form.resx (which is an XML file).
  2. Place the same default content in them that VS does
  3. Mimic the code generated when adding controls, code-behind and resources

It will be a tedious task, but it can be done. Adding resources however will be burdensome.


Yes, you can do achieve this using Compiler Services (compiling c# code) or Emit class if you know building correct MSIL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号