Friday, October 22, 2010

SharpDevelop :- ReferencePath

Adding Referencepath property to the SharpDevelop project property page programmatically. This is the topic of this blog. I wanted to add runtime generated reference path to the project property page. SharpDevelop provide Project service for managing the project related funtionality. ProjectService class contains many static method and events that can be used to load the new project to the solution or add exsisting project to the solution. ProjectService also exposes many events that are raised when project is added.removed or projectitem are added or removed.

I decided to add the dynamically generated reference path to the project after the project is loaded inside the SharpDevelop. For this I subscribed to the ProjectCreated event of ProjectService class .

ProjectService.ProjectCreated -= new ProjectEventHandler(ProjectService_ProjectCreated);

So whenever a new project is added to the solution , i can get the event and add my reference path to the project property. When we get the ProjectCreated event we get the ProjectEventArgs as the parameter. ProjectEventArgs contains the property Project which is of type IProject . We have to cast the Project property to MSBuildBasedProject .

MSBuildBasedProject is class that is responsible for the generating and setting the MSBuild items and properties from the in memory representation of project and project items.

Now we have to call the SetProperty method of MSBuildBasedProject with proper property /value and call the Save method on the MSBuildBasedProject.

void ProjectService_ProjectCreated(object sender, ProjectEventArgs e){
MSBuildBasedProject msProject = e.Project as MSBuildBasedProject;
msProject.SetProperty("ReferencePath", myRefPath);
msProject.Save();
}

No comments:

Post a Comment