Click here to Skip to main content
15,885,748 members
Articles / .NET

Genuilder Extensibility

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
13 Nov 2011Ms-PL4 min read 33.7K   93   13  
Generate your own code during compilation without MSBuild knowledge
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Genuilder.Extensibility;

namespace InterfaceExtractor.Gen
{
	public class DependencyHelloWorldPlugin : IPlugin
	{
		#region IPlugin Members

		public void Initialize(ICodeRepository repository)
		{
			repository.CodeItemCreated += new CodeItemCreatedHandler(repository_CodeItemCreated);
		}

		void repository_CodeItemCreated(ICodeRepository sender, CodeItem item)
		{
			if(!item.Name.EndsWith("generated.cs"))
			{
				var dependency = item.SourceOf(item.Name + "generated.cs");
				dependency.ShouldUpdateTarget += new CodeDependencyHandler(dependency_ShouldUpdateTarget);
			}
		}

		

		void dependency_ShouldUpdateTarget(CodeDependency sender, CodeItem target)
		{
			target.Content = String.Format("public class {0}", ToClassName(sender.Source.Name)) + "{}";
		}

		private string ToClassName(string fileName)
		{
			return fileName
				.Replace('.', '_')
				.Replace('/', '_')
				.Replace('\\', '_');
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions