Generate custom class at design time
I have a component which has a collection of objects (DataProviders). Each DataProvider has a unique name set by the user at design time. How can I make the designer generate a class into an additional file which contains the names of the DataProviders?
namespace MyNamespace
{
public class DpContainer : Component
{
public DataProviderCollection collection = new DataProviderCollection();
private void InitializeComponent()
{
DataProvider dataprovider1 = new DataProvider();
dataprovider1.Name = "Provider 1";
DataProvider dataprovider2 = new DataProvider();
dataprovider2.Name = "Provider 2";
collection.add(dataProvider1);
collection.开发者_运维百科add(dataProvider2);
}
}
}
The generated class should look like this:
namespace DataProviderNamespace
{
public class DataProviderNames
{
public const string Provider1 = "Provider 1";
public const string Provider2 = "Provider 2";
}
}
I would like to add this class as a designer.cs file. Using CodeDom I am able to create this class, but unfortunetly i have to specify full path of file. Additionally I would like to make the class partial. How can I do this?
I'm not sure if it does what you need, but have you looked at Text Template Transformation Toolkit(T4)? We used it to generate code for data access components. Comes with VS so it's not something additional you'd have to purchase.
Ok. I have found a solution (using DTE): in Serialize method of custom CodeDomSetializer:
IDesignerSerializationManager manager;
EnvDTE._DTE dte = (DTE) manager.GetService(typeof(DTE));
from dte I get file name of actual document:
dte.ActiveDocument.FullName
Now I get current project:
ProjectItem proj = (ProjectItem)manager.GetService(typeof(ProjectItem));
and add new file to project:
proj.ProjectItems.AddFromFile(NewFile);
精彩评论