ShP
ShP

Reputation: 1143

C# custom project template

I'm creating my own project template for C# which contains more projects inside.

I added my own wizard in it. And that works great.

But, when I try to put in some of my projects custom param, added in replacement dictionary, in my wizard library I get original value (not replaced) in my project (it stays as "$connectionString$").

For example, if I add this piece of code in RunStarted method:

private string _connectionString = "Lorem ipsum for example";
public void RunStarted(object automationObject, Dictionary<string, string>replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
    replacementsDictionary.Add("$connectionString$", _connectionString);
}

And in my web.config:

<connectionStrings>
    <add name="DAL.Database.Properties.Settings.MyConnectionString" connectionString="$connectionString$" providerName="System.Data.SqlClient" />
</connectionStrings>  

And even in my .vstemplate file I see that this file are marked for checking and modifying paramas:

<ProjectItem ReplaceParameters="true" OpenInEditor="true" TargetFileName="Web.config">Web.config</ProjectItem>

Note: It works only if I put hardcoded value in .vstemplate file like this for example:

<CustomParameters>
    <CustomParameter Name="$connectionString$" Value="Some dummy value" />
</CustomParameters>

But that's not what I want. And now I'm wondering, what can be a problem?

Upvotes: 2

Views: 1265

Answers (1)

ShP
ShP

Reputation: 1143

I finally found a solution for this problem.

To pass your custom parameters from your class library which implements IWizard interface, you will have to create your own dictionary, and there put your custom data.

Then copy data from there your to replacementsDictionary dictionary.

This is the example how can you share the same dictionary filled with values that you want to replace between multiple project templates:

private static Dictionary<string, string> _sharedDictionary = new Dictionary<string, string>();

public void RunStarted(object automationObject,
        Dictionary<string, string> replacementsDictionary,
        WizardRunKind runKind, object[] customParams)
    {
        if (runKind == WizardRunKind.AsMultiProject)
        {
            try
            {                    
                _sharedDictionary.Add("$connectionString$", connectionString);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        if (_sharedDictionary != null)
        {
            foreach (KeyValuePair<string, string> dictItem in _sharedDictionary)
            {
                if (!replacementsDictionary.ContainsKey(dictItem.Key))
                {
                    replacementsDictionary.Add(dictItem.Key, dictItem.Value);
                }
            }
        }
    }

Because the _sharedDictionary are marked as static all instances will share the same dictionary, and values that need to be replaced will be available in all of your project templates.

Also, don't forget to include in all of your linked projects .vstemplate files WizardExtension section.

Upvotes: 1

Related Questions