OWASP O2 Platform Blog

Changing a reference/dll value of a VS CSproj file

While automating an VisualStudio based application test (where I inject O2 into the target website (so that I can used it as part of the analysis)), I needed to make changes to the csproj (which I can then compile via msbuild.exe)

Here is the code that does the trick

var oldDllPartialName = "aaaaaaaaaaa";
var newDllName = "O2_TeamMentor_aaaaaaaaaaa.dll";
var csProj =  teamMentor.website_CSProj();
var xRoot = csProj.xRoot();

var references = (from itemGroup in xRoot.elements("ItemGroup".add_xmlns(xRoot))
                  from reference in itemGroup.elements("Reference".add_xmlns(xRoot))
                  select reference).toList();
                
var o2_TM_Reference = references.Where((reference)=> reference.attribute("Include").value()
                                                              .contains(oldDllPartialName))
                                .First();
o2_TM_Reference.attribute("Include").value(newDllName);                                 
o2_TM_Reference.element("HintPath".add_xmlns(xRoot)).value(@"bin\{0}".format(newDllName));

xRoot.saveAs(csProj);  // returns true if the file was correctly saved

return o2_TM_Reference.str();

one problem I had was that the elements search was not taking account the xmlns value, so I had to create an extension method called add_xmlns (use in the code above) to do that.

Using the O2 Scripting environment, this was first created as a local lambda function:

Func<System.Xml.Linq.XElement, string, string> add_xmlns =
    (xElement, name) =>{
                            var xmlns =  xElement.attribute("xmlns").value();
                            return "{" + xmlns + "}" + name;
                        };

which was then converted into an extension method:

        public static string add_xmlns(this string name, XElement xElement)               
        {
            var xmlns =  xElement.attribute("xmlns").value();
            return "{" + xmlns + "}" + name;
        }                       

and for future scalability, we might as well refactor this and create a generic method to prepend attribute values:

        public static string add_xmlns(this string name, XElement xElement)               
        {
            return name.prepend_AttributeValue(xElement, "xmlns");
        }
       
        public static string prepend_AttributeValue(this string name, XElement xElement, string attributeName)
        {
            var xmlns =  xElement.attribute(attributeName).value();
            return "{" + xmlns + "}" + name;
        }

June 27, 2011 - Posted by | .NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: