Wednesday, December 23, 2009

Flatten array of arrays using linq

If you have list of object that contains list of other objects and you want to collect all children in one list linq can make it simple. For example:

List<int> listOfInt = new List<int>();
List<List<int>> listOfListOfInts = new List<List<int>>();
foreach (List<int> ints in listOfListOfInts)
foreach (int i in ints)

but using linq the following statements become:

List<int> listOfInt = listOfListOfInts.SelectMany(ints => ints).ToList();

Thursday, December 17, 2009

"Missing XML comment..." in service reference file

When I added service reference in my project I have got a lot of "Missing XML comment for public visible type..." warning messages in Reference.cs file. Those warnings didn't allow me to see real important warning messages. To disable this, add:

#pragma warning disable 1591

at the beginning of Reference.cs file and

#pragma warning restore 1591

at the end of Resource.cs file.

And off course you will need to add this every time you update reference.

Use svn revision number for assembly version

If you want to use svn revision number as revision number of assembly version this post can be helpful.
I have used information from and to create macro for this.

Here is the macro:

Public Sub UpdateRevisionNumber()

        Dim proj As Project

        Dim objProj As Object()

        objProj = DTE.ActiveSolutionProjects


        If objProj.Length = 0 Then

            Exit Sub

        End If

        proj = DTE.ActiveSolutionProjects(0) 'gets the path of the web project to write to the web.config

        Dim x As String = proj.ProjectItems.ContainingProject.FullName


        Dim p As New System.Diagnostics.Process

        'first update the root to get the latest revision

        p.StartInfo.UseShellExecute = False

        p.StartInfo.RedirectStandardOutput = True

        p.StartInfo.RedirectStandardError = True

        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

        p.StartInfo.FileName = "svn.exe"

        p.StartInfo.WorkingDirectory = proj.Properties.Item("FullPath").Value()

        p.StartInfo.Arguments = "info"


        Dim strerror As String = p.StandardError().ReadToEnd()

        Dim output As String = p.StandardOutput().ReadToEnd()


        'get the revision

        Dim re As New Regex("Revision:\s+(\d+)\s*")

        Dim ver As String

        ver = re.Match(output).Groups(1).Value

        'add one so the number is not one revision behind

        Dim version As Integer

        version = Convert.ToInt32(ver) + 1


        Dim propPath As String

        propPath = proj.Properties.Item("FullPath").Value() + "Properties\AssemblyInfo.cs"


        Dim globalAssemblyContent As String = File.ReadAllText(propPath)

        Dim rVersionAttribute As Regex = New Regex("\[[\s]*(\/\*[\s\S]*?\*\/)?[\s]*assembly[\s]*(\/\*[\s\S]*?\*\/)?[\s]*:[\s]*(\/\*[\s\S]*?\*\/)?[\s]*AssemblyVersion[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\([\s]*(\/\*[\s\S]*?\*\/)?[\s]*\""([0-9]+)\.([0-9]+)(.([0-9]+))?(.([0-9]+))?\""[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\)[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\]")

        Dim rVersionInfoAttribute As Regex = New Regex("\[[\s]*(\/\*[\s\S]*?\*\/)?[\s]*assembly[\s]*(\/\*[\s\S]*?\*\/)?[\s]*:[\s]*(\/\*[\s\S]*?\*\/)?[\s]*AssemblyFileVersion[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\([\s]*(\/\*[\s\S]*?\*\/)?[\s]*\""([0-9]+)\.([0-9]+)(.([0-9]+))?(.([0-9]+))?\""[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\)[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\]")


        'Find Version Attribute for Updating Build Number

        Dim mVersionAttributes As MatchCollection = rVersionAttribute.Matches(globalAssemblyContent)

        Dim mVersionAttribute As Match = GetFirstUnCommentedMatch(mVersionAttributes, globalAssemblyContent)

        Dim gBuildNumber As Group = mVersionAttribute.Groups(11)

        Dim newBuildNumber As String


        'Replace Version Attribute for Updating Build Number

        If (gBuildNumber.Success) Then

            newBuildNumber = version.ToString()

            globalAssemblyContent = globalAssemblyContent.Substring(0, gBuildNumber.Index) + newBuildNumber + globalAssemblyContent.Substring(gBuildNumber.Index + gBuildNumber.Length)

        End If


        'Find Version Info Attribute for Updating Build Number

        Dim mVersionInfoAttributes As MatchCollection = rVersionInfoAttribute.Matches(globalAssemblyContent)

        Dim mVersionInfoAttribute As Match = GetFirstUnCommentedMatch(mVersionInfoAttributes, globalAssemblyContent)

        Dim gBuildNumber2 As Group = mVersionInfoAttribute.Groups(11)


        'Replace AssemblyFileVersion

        If (gBuildNumber2.Success) Then

            If String.IsNullOrEmpty(newBuildNumber) Then

                newBuildNumber = version.ToString()

            End If


            globalAssemblyContent = globalAssemblyContent.Substring(0, gBuildNumber2.Index) + newBuildNumber + globalAssemblyContent.Substring(gBuildNumber2.Index + gBuildNumber2.Length)

        End If


        File.WriteAllText(propPath, globalAssemblyContent)

    End Sub



    Private Function GetFirstUnCommentedMatch(ByRef mc As MatchCollection, ByVal content As String) As Match

        Dim rSingleLineComment As Regex = New Regex("\/\/.*$")

        Dim rMultiLineComment As Regex = New Regex("\/\*[\s\S]*?\*\/")


        Dim mSingleLineComments As MatchCollection = rSingleLineComment.Matches(content)

        Dim mMultiLineComments As MatchCollection = rMultiLineComment.Matches(content)


        For Each m As Match In mc

            If m.Success Then

                For Each singleLine As Match In mSingleLineComments

                    If singleLine.Success Then

                        If m.Index >= singleLine.Index And m.Index + m.Length <= singleLine.Index + singleLine.Length Then

                            GoTo NextAttribute

                        End If

                    End If



                For Each multiLine As Match In mMultiLineComments

                    If multiLine.Success Then

                        If m.Index >= multiLine.Index And m.Index + m.Length <= multiLine.Index + multiLine.Length Then

                            GoTo NextAttribute

                        End If

                    End If



                Return m

            End If




        Return Nothing


    End Function